### 5 springboot基礎
習慣優于配置。
優點:快速構建項目,對主流框架無配置集成,可以獨立運行而不需servlet容器,提供了運行時監控,提高了開發部署效率,與云計算天然集成;
添加spring boot的父級依賴,這樣這個項目就是spring boot項目了。
pom:parent>groupId>org.springframework.boot; parent>artifactId>spring-boot-starter-parent;
~~~
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
~~~
web支持的starter pom:spring-boot-starter-web
~~~
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
~~~
spring boot的編譯插件pom:spring-boot-maven-plugin
~~~
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
~~~
還需要添加ch.qos.back和slf4j的pom:
~~~
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
~~~
使用 mvn spring-boot:run進行程序運行,或者運行入口類;
### 6 springboot 核心
使用banner.txt修改啟動banner;
spring boot reference 提供了spring boot都支持的pom內容,spring-boot-starter-xxx[-xxx];
常規屬性配置:在application.yml中配置屬性,使用@Value("${book.name}")就可以注入;可以進行類型轉換,但是如果轉換出錯,系統將崩潰;
類型安全的配置:使用Bean進行屬性的獲取,在Bean上面添加@ConfigurationProperties(prefix="a.b"),在Bean中設置屬性名稱,系統會自動注入屬性值。如果屬性文件不是application.yml,可以通過locations設置屬性文件位置。
Profie是Spring對不同的環境提供不同的支持,application-dev.yml和application-prod.yml,在application.yml中進行spring.profiles.active=dev設置;
@EnableAutoConfiguration讓Spring Boot根據類路徑中的jar包依賴為當前項目進行自動配置;