1.打开idea,创建新项目,选择spring initializr
2.输入artifact
3.勾选web
4.点击finish完成
5.进入项目,可以将以下内容删除
pom.xml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>springbootdemo</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>springbootdemo</name> <description>demo project for spring boot</description> <!--起步依赖--> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 2 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <!--开发web项目相关依赖--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!--springboot单元测试--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <!--maven构建--> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
6.创建一个hellocontroller
1
2
3
4
5
6
7
8
9
10
|
package com.example; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class hellocontroller { @requestmapping ( "/hello" ) public string hello() { return "hello,this is a springboot demo" ; } } |
7.程序自动生成的springbootdemoapplication,会有一个@springbootapplication的注解,这个注解用来标明这个类是程序的入口
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.example; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; //入口 @springbootapplication public class springbootdemoapplication { public static void main(string[] args) { springapplication.run(springbootdemoapplication. class , args); } } |
@springbootapplication开启了spring的组件扫描和springboot的自动配置功能,相当于将以下三个注解组合在了一起
(1)@configuration:表名该类使用基于java的配置,将此类作为配置类
(2)@componentscan:启用注解扫描
(3)@enableautoconfiguration:开启springboot的自动配置功能
8.运行springbootdemoapplication类
测试:
在地址栏中输入http://localhost:8080/hello
9.使用启动jar包的方式启动
(1)首先进入项目所在目录,如果是mac系统在项目上右键,选择reveal in finder,windows系统在项目上右键选择show in explorer,即可打开项目所在目录
(2)打开终端,进入项目所在目录
cd /users/shanml/ideaprojects/springbootdemo
输入mvn install,构建项目
(3)构建成功后,在项目target文件夹下会多出一个jar包
(4)使用java -jar springbootdemo-0.0.1-snapshot.jar
启动jar包即可
参考:
廖师兄:两小时学会springboot
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/lom9357bye/article/details/69677120