SpringBoot
是為了簡化Spring
應用的創建、運行、調試、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,我們只需遵循規范,引入相關的依賴就可以輕易的搭建出一個 WEB 工程
上一篇介紹了
SpringBoot
由來及構建方式,通過第一章的教程我們對SpringBoot
不在感到陌生,可以發現SpringBoot
雖然干掉了 XML 但未做到 零配置,它體現出了一種 約定優於配置,也稱作按約定編程,是一種軟件設計范式,旨在減少軟件開發人員需做決定的數量,獲得簡單的好處,而又不失靈活性。 一般情況下默認的配置足夠滿足日常開發所需,但在特殊的情況下,我們往往需要用到自定義屬性配置、自定義文件配置、多環境配置、外部命令引導等一系列功能。不用擔心,這些SpringBoot
都替我們考慮好了,我們只需要遵循它的規則配置即可准備前提
為了讓
SpringBoot
更好的生成數據,我們需要添加如下依賴(該依賴可以不添加,但是在 IDEA 和 STS 中不會有屬性提示,沒有提示的配置就跟你用記事本寫代碼一樣苦逼,出個問題弄哭你去),該依賴只會在編譯時調用,所以不用擔心會對生產造成影響…<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
自定義屬性配置
在
application.properties
寫入如下配置內容my1.age=22 my1.name=battcn
其次定義
MyProperties1.java
文件,用來映射我們在application.properties
中的內容,這樣一來我們就可以通過操作對象的方式來獲得配置文件的內容了package com.battcn.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author Levin * @since 2018/4/23 0023 */ @Component @ConfigurationProperties(prefix = "my1") public class MyProperties1 { private int age; private String name; // 省略 get set @Override public String toString() { return "MyProperties1{" + "age=" + age + ", name='" + name + '\'' + '}'; } }接下來就是定義我們的
PropertiesController
用來注入MyProperties1
測試我們編寫的代碼,值得注意的是Spring4.x
以后,推薦使用構造函數的形式注入屬性…import com.battcn.properties.MyProperties1; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Levin * @since 2018/4/23 0023 */ @RequestMapping("/properties") @RestController public class PropertiesController { private static final Logger log = LoggerFactory.getLogger(PropertiesController.class); private final MyProperties1 myProperties1; @Autowired public PropertiesController(MyProperties1 myProperties1) { this.myProperties1 = myProperties1; } @GetMapping("/1") public MyProperties1 myProperties1() { log.info("================================================================================================="); log.info(myProperties1.toString()); log.info("================================================================================================="); return myProperties1; } }打開瀏覽器,輸入如下地址:http://localhost:8080/properties/1,觀察控制台,監聽到如下內容則表示程序正確
2018-04-23 15:51:43.145 INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController : ================================================================================================= 2018-04-23 15:51:43.145 INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController : MyProperties1{age=22, name='battcn'} 2018-04-23 15:51:43.145 INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController : =================================================================================================
自定義文件配置
定義一個名為
my2.properties
的資源文件,自定義配置文件的命名不強制application
開頭my2.age=22 my2.name=Levin my2.email=1837307557@qq.com其次定義
MyProperties2.java
文件,用來映射我們在my2.properties
中的內容。package com.battcn.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * @author Levin * @since 2018/4/23 0023 */ @Component @PropertySource("classpath:my2.properties") @ConfigurationProperties(prefix = "my2") public class MyProperties2 { private int age; private String name; private String email; // 省略 get set @Override public String toString() { return "MyProperties2{" + "age=" + age + ", name='" + name + '\'' + ", email='" + email + '\'' + '}'; } }接下來在
PropertiesController
用來注入MyProperties2
測試我們編寫的代碼@GetMapping("/2") public MyProperties2 myProperties2() { log.info("================================================================================================="); log.info(myProperties2.toString()); log.info("================================================================================================="); return myProperties2; }打開瀏覽器,輸入如下地址:http://localhost:8080/properties/2,觀察控制台,監聽到如下內容則表示程序正確
2018-04-23 15:59:45.395 INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController : ================================================================================================= 2018-04-23 15:59:45.395 INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController : MyProperties2{age=22, name='Levin', email='1837307557@qq.com'} 2018-04-23 15:59:45.395 INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController : =================================================================================================
多環境化配置
在真實的應用中,常常會有多個環境(如:開發,測試,生產等),不同的環境數據庫連接都不一樣,這個時候就需要用到
spring.profile.active
的強大功能了,它的格式為application-{profile}.properties
,這里的application
為前綴不能改,{profile}
是我們自己定義的。創建
application-dev.properties
、application-test.properties
、application-prod.properties
,內容分別如下application-dev.properties
server.servlet.context-path=/devapplication-test.properties
server.servlet.context-path=/testapplication-prod.properties
server.servlet.context-path=/prod在
application.properties
配置文件中寫入spring.profiles.active=dev
,這個時候我們在次訪問 http://localhost:8080/properties/1 就沒用處了,因為我們設置了它的context-path=/dev
,所以新的路徑就是 http://localhost:8080/dev/properties/1 ,由此可以看出來我們激活不同的配置讀取的屬性值是不一樣的外部命令引導
前面三種方式都是基於配置文件層面的,那么有沒有辦法外部引導呢,假設這樣的場景,我們對已經開發完成的代碼打包發布,期間在測試環境測試通過了,那么即可發布上生產,這個時候是修改
application.properties
的配置方便還是直接在命令參數配置方便呢,毫無疑問是后者更有說服力。默認情況下,SpringApplication
會將命令行選項參數(即:–property,如–server.port=9000)添加到Environment,命令行屬性始終優先於其他屬性源。
如何測試?
- 進入到項目目錄,此處以我本地目錄為主:F:/battcn-workspace/spring-boot2-learning/chapter2
- 然后打開 cmd 程序,不會在當前目錄打開 cmd 的請自行百度,輸入:
mvn package
- 打包完畢后進入到:F:/battcn-workspace/spring-boot2-learning/chapter2/target 目錄中去,我們可以發現一個名為chapter2-0.0.1-SNAPSHOT.jar 的包
- 接着在打開 cmd 程序,輸入:
java -jar chapter2-0.0.1-SNAPSHOT.jar --spring.profiles.active=test --my1.age=32
。仔細觀察spring.profiles.active=test
、my1.age=32
這倆配置的鍵值是不是似曾相識(不認識的請從開頭認真閱讀)- 最后輸入測試地址:http://localhost:8080/test/properties/1 我們可以發現返回的JSON變成了
{"age":32,"name":"battcn"}
表示正確總結
- 掌握
@ConfigurationProperties
、@PropertySource
等注解的用法及作用- 掌握編寫自定義配置
- 掌握外部命令引導配置的方式
目前很多大佬都寫過關於
SpringBoot
的教程了,如有雷同,請多多包涵,本教程基於最新的spring-boot-starter-parent:2.0.1.RELEASE
編寫,包括新版本的特性都會一起介紹…