可以通过以下三种方式来创建Spring Boot工程
- Spring Initializr(Web界面)
- Spring Boot CLI(命令行工具)
- Spring Boot IDE(Eclipse、IntelliJ IDEA、Spring STS等)
SpringBoot工程从创建到执行大体如下:
(1)Spring Initializr
访问 https://start.spring.io/ 点击Switch to the full Version 可以切换到更详细的设置页面。
可以看到最终下载ZIP文件的URL如下:
引用
https://start.spring.io/starter.zip?type=maven-project&bootVersion=1.5.1.RELEASE&baseDir=spring-boot-demo1&groupId=com.rensanning.springboot&artifactId=spring-boot-demo1&name=spring-boot-demo1&description=Demo+project+for+Spring+Boot&packageName=com.rensanning.springboot&packaging=jar&javaVersion=1.8&language=java&autocomplete=&generate-project=&style=web
解压到 D:\springbootsample\spring-boot-demo1 后通过“mvn spring-boot:run”启动即可。
引用
D:\>cd D:\springbootsample\spring-boot-demo1
D:\springbootsample\spring-boot-demo1>mvn spring-boot:run
D:\springbootsample\spring-boot-demo1>mvn spring-boot:run
启动完成后可以看到以下日志
引用
2017-02-07 15:02:57.115 INFO 3980 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-07 15:02:57.119 INFO 3980 --- [ main] c.r.s.SpringBootDemo1Application : Started SpringBootDemo1Application in 2.226 seconds (JVM running for 4.758)
2017-02-07 15:02:57.119 INFO 3980 --- [ main] c.r.s.SpringBootDemo1Application : Started SpringBootDemo1Application in 2.226 seconds (JVM running for 4.758)
(2)Spring Boot CLI
下载安装Spring Boot CLI
引用
1) 从官网下载spring-boot-cli-1.5.1.RELEASE-bin.zip
http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/
2) 解压到 D:\spring-1.5.1.RELEASE\
3) 将 D:\spring-1.5.1.RELEASE\bin 添加到环境变量PATH里。
4) 确认 D:\springbootsample>spring --version
Spring CLI v1.5.1.RELEASE
http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/
2) 解压到 D:\spring-1.5.1.RELEASE\
3) 将 D:\spring-1.5.1.RELEASE\bin 添加到环境变量PATH里。
4) 确认 D:\springbootsample>spring --version
Spring CLI v1.5.1.RELEASE
创建Spring Boot项目
D:\springbootsample>mkdir spring-boot-demo2 D:\springbootsample>cd spring-boot-demo2 D:\springbootsample\spring-boot-demo2>spring init -d=web -g=com.rensanning.springboot -a=spring-boot-demo2 --package-name=com.rensanning.springboot --name=spring-boot-demo2 -x Using service at https://start.spring.io Project extracted to 'D:\springbootsample\spring-boot-demo2' D:\springbootsample\spring-boot-demo2>mvn spring-boot:run
启动完成后可以看到以下日志
引用
2017-02-07 15:07:08.778 INFO 7624 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-07 15:07:08.785 INFO 7624 --- [ main] c.r.s.SpringBootDemo2Application : Started SpringBootDemo2Application in 2.35 seconds (JVM running for 5.529)
2017-02-07 15:07:08.785 INFO 7624 --- [ main] c.r.s.SpringBootDemo2Application : Started SpringBootDemo2Application in 2.35 seconds (JVM running for 5.529)
spring init 命令参数说明:
引用
-d(dependencies 依赖包)
-g(Group Id)
-a(Artifact Id)
--package-name(Package name)
--name(Project name)
-x(Extract compatible archives)
-g(Group Id)
-a(Artifact Id)
--package-name(Package name)
--name(Project name)
-x(Extract compatible archives)
详细内容查看:
引用
$ spring help init
无论哪种创建方式都是要访问https://start.spring.io来获取模板工程代码,
所以甚至可以使用CURL或HTTPie这些第三方工具来创建,比如:
引用
curl https://start.spring.io/starter.zip
-d dependencies=web,data-jpa,jms,ws
-d packaging=war
-d type=gradle-project
-o SpringBootCurlWebProject.zip
-d dependencies=web,data-jpa,jms,ws
-d packaging=war
-d type=gradle-project
-o SpringBootCurlWebProject.zip
引用
http -v start.spring.io/starter.zip dependencies=web baseDir=demo -d
导入工程
引用
$ mvn eclipse:eclipse
[INFO] Wrote Eclipse project for "demo" to D:\springbootsample\spring-boot-demo2.
[INFO] Wrote Eclipse project for "demo" to D:\springbootsample\spring-boot-demo2.
或者直接使用Eclipse导入工程,Menu - File - Import - Maven - Existing Maven Projects
创建gradle工程
D:\springbootsample>mkdir spring-boot-demo3 D:\springbootsample>cd spring-boot-demo3 D:\springbootsample\spring-boot-demo3>spring init -d=web -g=com.rensanning.springboot -a=spring-boot-demo3 --package-name=com.rensanning.springboot --name=spring-boot-demo3 -x --build=gradle D:\springbootsample\spring-boot-demo3>gradle bootRun
引用
2017-02-07 15:15:14.081 INFO 8128 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-07 15:15:14.086 INFO 8128 --- [ main] c.r.s.SpringBootDemo3Application : Started SpringBootDemo3Application in 2.171 seconds (JVM running for 2.474)
2017-02-07 15:15:14.086 INFO 8128 --- [ main] c.r.s.SpringBootDemo3Application : Started SpringBootDemo3Application in 2.171 seconds (JVM running for 2.474)
也可以打包成jar后执行
引用
D:\springbootsample\spring-boot-demo1>mvn clean package
D:\springbootsample\spring-boot-demo1>java -jar target/spring-boot-demo1-0.0.1-SNAPSHOT.jar
D:\springbootsample\spring-boot-demo1>java -jar target/spring-boot-demo1-0.0.1-SNAPSHOT.jar
引用
D:\springbootsample\spring-boot-demo3>gradle build
D:\springbootsample\spring-boot-demo3>java -jar build/libs/spring-boot-demo3-0.0.1-SNAPSHOT.jar
D:\springbootsample\spring-boot-demo3>java -jar build/libs/spring-boot-demo3-0.0.1-SNAPSHOT.jar
(3)IDE
STS:Spring Tool Suite https://spring.io/tools/sts/all
创建工程
Menu - File - New - Spring Starter Project
运行工程
Menu - Run - Run As - Spring Boot App
访问 http://localhost:8080/
***因为没有任何controller,所以看到的是404页面。
(4)文件夹构成
引用
│ .gitignore
│ mvnw
│ mvnw.cmd
│ pom.xml
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─example
│ │ SpringBootDemo3Application.java
│ └─resources
│ │ application.properties
│ ├─static
│ └─templates
└─test
└─java
└─com
└─example
SpringBootDemo3ApplicationTests.java
│ mvnw
│ mvnw.cmd
│ pom.xml
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─example
│ │ SpringBootDemo3Application.java
│ └─resources
│ │ application.properties
│ ├─static
│ └─templates
└─test
└─java
└─com
└─example
SpringBootDemo3ApplicationTests.java
@SpringBootApplication public class SpringBootDemo4Application { public static void main(String[] args) { SpringApplication.run(SpringBootDemo4Application.class, args); } }
@SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiration
默认扫描的package是启动Application类所在的package。
4.0.0 com.rensanning.springboot spring-boot-demo4 0.0.1-SNAPSHOT jar spring-boot-demo4 Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.1.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
(5)输出“Hello World”
新建一个HelloController类。
@RestController public class HelloController { @RequestMapping("/") public String index() { return "Hello world from Spring Boot!"; } }
再次访问 http://localhost:8080/
(6)Spring Boot工程中如果有多个main()函数,需要指定启动类
com.rensanning.springboot.SpringBootDemoApplication
或者
org.springframework.boot spring-boot-maven-plugin com.rensanning.springboot.SpringBootDemoApplication