第一章 第一個spring boot程序,springboot
環境:
- jdk:1.8.0_73
- maven:3.3.9
- spring-boot:1.2.5.RELEASE(在pom.xml中指定了)
注意:關於spring-boot的支持,
- 最少使用jdk7(jdk6也可以,可能需要額外配置,沒試過,官方推薦jdk8)
- maven至少使用3.2
1、首先進入一個文件夾,例如"~/Desktop/project",然後按照"第一章 企業項目開發--maven+springmvc+spring+mybatis+velocity整合"所說的手工建立一個maven工程,其中的pom.xml如下:

![]()
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.xxx</groupId>
8 <artifactId>myboot</artifactId>
9 <version>1.0-SNAPSHOT</version>
10
11 <!--
12 引入spring-boot-starter-parent做parent是最好的方式,
13 但是有時我們可能要引入我們自己的parent,此時解決方式有兩種:
14 1)我們自己的parent的pom.xml的parent設為spring-boot-starter-parent(沒有做過驗證,但是感覺可行)
15 2)使用springboot文檔中的方式:見spring-boot-1.2.5-reference.pdf的第13頁
16 -->
17 <parent>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-parent</artifactId>
20 <version>1.2.5.RELEASE</version>
21 </parent>
22
23 <!-- 引入實際依賴 -->
24 <dependencies>
25 <dependency>
26 <groupId>org.springframework.boot</groupId>
27 <artifactId>spring-boot-starter-web</artifactId>
28 </dependency>
29 </dependencies>
30
31 <build>
32 <plugins>
33 <!--
34 用於將應用打成可直接運行的jar(該jar就是用於生產環境中的jar)
35 值得注意的是,如果沒有引用spring-boot-starter-parent做parent,
36 且采用了上述的第二種方式,這裡也要做出相應的改動
37 -->
38 <plugin>
39 <groupId>org.springframework.boot</groupId>
40 <artifactId>spring-boot-maven-plugin</artifactId>
41 </plugin>
42 </plugins>
43 </build>
44 </project>
View Code
之後,使用"mvn clean compile"成功後,引入eclipse,具體的方式見:第一章 企業項目開發--maven+springmvc+spring+mybatis+velocity整合
2、引入eclipse中的工程結構如下:

之後,創建紅框部分。
FirstBoot:

![]()
1 package com.xxx.firstboot;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5 import org.springframework.web.bind.annotation.RequestMapping;
6 import org.springframework.web.bind.annotation.RestController;
7
8 /**
9 * @RestController:spring mvcd的注解,
10 * 相當於@Controller與@ResponseBody的合體,可以直接返回json
11 *
12 * @EnableAutoConfiguration:spring boot的注解
13 * 是無配置啟動的關鍵部分
14 */
15 @RestController
16 @EnableAutoConfiguration
17 public class FirstBoot {
18
19 @RequestMapping("/hello")
20 public String testFirstMethod(){
21 return "Hello jigang!!!";
22 }
23
24 /**
25 * spring boot的入口,在整個項目中,包括其子項目在內,
26 * 只能有一個main方法,否則spring boot啟動不起來
27 */
28 public static void main(String[] args) {
29 SpringApplication.run(FirstBoot.class, args);
30 }
31
32 }
View Code
解釋:
3、啟動spring boot
首次啟動,采用"mvn spring-boot:run"(會下載一些東西),之後啟動直接在主類(含有main方法的類)上"run as-->java application"即可
當看到console輸出以下的信息時,表示啟動spring-boot成功,

之後在浏覽器輸入路徑"http://localhost:8080/hello",輸出信息"Hello jigang!!!",則成功!!!
注意:
在我們輸入路徑訪問資源的時候,console輸出如下信息:

也就是說在此時才初始化了springMVC的dispatcherServlet。
4、打包成可執行的jar,該jar將是在生產環境下可用的jar
生產jar的過程依賴於引入的spring-boot-maven-plugin.jar。
執行"mvn package",查看target下的文件:

後邊那一個包是maven自己打的,前邊那個是spring-boot自己又基於後邊那個重新打包了(repackage)
注意:如果沒有使用spring-boot-starter-parent做parent(看pom.xml的注釋),那麼打包過程需要作出相應的調整。
5、運行打包好的jar
進入myboot文件夾下,執行命令"java -jar target/myboot-1.0-SNAPSHOT.jar",得出如下結果:

之後訪問浏覽器即可。
說明:
在沒有引入spring-boot-starter-web.jar之前,查看依賴樹:

引入spring-boot-starter-web.jar之後,查看依賴樹:

實際上,spring-boot-starter-web.jar這個jar引入5各方面的jar:
- spring-boot的啟動jar:spring-boot-starter.jar
- tomcat相關jar(spring-boot-1.2.5-->tomcat8)
- jackson
- hiberbate-validator
- spring相關jar(spring-boot-1.2.5-->spring4.1)
兩個問題:
1)若不采用spring-boot-starter-parent做parent,該怎麼配置?
解決方案:
加入以下代碼即可。

![]()
1 <dependencyManagement>
2 <dependencies>
3 <dependency>
4 <!-- Import dependency management from Spring Boot -->
5 <groupId>org.springframework.boot</groupId>
6 <artifactId>spring-boot-dependencies</artifactId>
7 <version>1.2.5.RELEASE</version>
8 <type>pom</type>
9 <scope>import</scope>
10 </dependency>
11 </dependencies>
12 </dependencyManagement>
View Code
完整的pom.xml

![]()
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.xxx</groupId>
8 <artifactId>myboot</artifactId>
9 <version>1.0-SNAPSHOT</version>
10
11 <!-- 引入spring-boot-starter-parent做parent是最好的方式, 但是有時我們可能要引入我們自己的parent,此時解決方式有兩種:
12 1)我們自己的parent的pom.xml的parent設為spring-boot-starter-parent(沒有做過驗證,但是感覺可行) 2)使用springboot文檔中的方式:見spring-boot-1.2.5-reference.pdf的第13頁 -->
13 <!-- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>
14 <version>1.2.5.RELEASE</version> </parent> -->
15
16 <dependencyManagement>
17 <dependencies>
18 <dependency>
19 <!-- Import dependency management from Spring Boot -->
20 <groupId>org.springframework.boot</groupId>
21 <artifactId>spring-boot-dependencies</artifactId>
22 <version>1.2.5.RELEASE</version>
23 <type>pom</type>
24 <scope>import</scope>
25 </dependency>
26 </dependencies>
27 </dependencyManagement>
28
29 <!-- 引入實際依賴 -->
30 <dependencies>
31 <dependency>
32 <groupId>org.springframework.boot</groupId>
33 <artifactId>spring-boot-starter-web</artifactId>
34 </dependency>
35 </dependencies>
36
37 <build>
38 <plugins>
39 <!-- 用於將應用打成可直接運行的jar(該jar就是用於生產環境中的jar) 值得注意的是,如果沒有引用spring-boot-starter-parent做parent,
40 且采用了上述的第二種方式,這裡也要做出相應的改動 -->
41 <plugin>
42 <groupId>org.springframework.boot</groupId>
43 <artifactId>spring-boot-maven-plugin</artifactId>
44 </plugin>
45 </plugins>
46 </build>
47 </project>
View Code
注意:引入的是在dependencyManagement,spring-boot-dependencies的type=pom,scope=import
2)若不采用spring-boot-starter-parent做parent,打包插件spring-boot-maven-plugin該怎麼配置?
這裡采用上邊的配置,我直接執行"mvn package"也成功了,這裡有知道的給解釋一下?與官方文檔不一樣啊