相信不少人選擇Spring Boot主要是考慮到它既能兼顧Spring的強大功能,還能實現快速開發的便捷。咱們在Spring Boot使用過程當中,最直觀的感覺就是沒有了原來本身整合Spring應用時繁多的XML配置內容,替代它的是在pom.xml
中引入模塊化的Starter POMs
,其中各個模塊都有本身的默認配置,因此若是不是特殊應用場景,就只須要在application.properties
中完成一些屬性配置就能開啓各模塊的應用。java
pom包裏面添加相關包引用git
<!-- Web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 單元測試依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
在以前的各篇文章中都有說起關於application.properties
的使用,主要用來配置數據庫鏈接、日誌相關配置等。除了這些配置內容以外,本文將具體介紹一些在application.properties
配置中的其餘特性和使用方法。github
而後經過@Value("${屬性名}")
註解來加載對應的配置屬性,具體以下:web
@RestController public class UserController { @Value("${user.desc}") private String desc; @RequestMapping(value = "/user",method = RequestMethod.GET) public String getDesc(){ return desc; } }
@RestController public class HelloController { @RequestMapping(value = "/", method = RequestMethod.GET) public String index(){ return "Welcome Spring Boot Properties!!!"; } @Autowired HomeProperties homeProperties; @RequestMapping(value = "/home",method = RequestMethod.GET) public String getDesc(){ return homeProperties.getDesc(); } }
在application.properties
中的各個參數之間也能夠直接引用來使用,就像下面的設置:spring
## 家鄉屬性 Dev home.province=ZheJiang home.city=WenLing home.desc=dev: I'm living in ${home.province} ${home.city}. ###我的屬性 user.name=ilimhumar user.age=25 user.job=Java Programmer user.weight = 70kg user.height=180cm user.desc= My name's ${user.name} ${user.age} ${user.job} ${user.weight} ${user.height}
咱們在開發Spring Boot應用時,一般同一套程序會被應用和安裝到幾個不一樣的環境,好比:開發、測試、生產等。其中每一個環境的數據庫地址、服務器端口等等配置都會不一樣,若是在爲不一樣環境打包時都要頻繁修改配置文件的話,那必將是個很是繁瑣且容易發生錯誤的事。數據庫
對於多環境的配置,各類項目構建工具或是框架的基本思路是一致的,經過配置多份不一樣環境的配置文件,再經過打包命令指定須要打包的內容以後進行區分打包,Spring Boot也不例外,或者說更加簡單。服務器
在Spring Boot中多環境配置文件名須要知足application-{profile}.properties
的格式,其中{profile}
對應你的環境標識,好比:app
application-dev.properties
:開發環境application-test.properties
:測試環境application-prod.properties
:生產環境至於哪一個具體的配置文件會被加載,須要在application.properties
文件中經過spring.profiles.active
屬性來設置,其值對應{profile}
值。框架
如:spring.profiles.active=test
就會加載application-test.properties
配置文件內容模塊化
下面,以不一樣環境配置不一樣的服務端口爲例,進行樣例實驗。
針對各環境新建不一樣的配置文件application-dev.properties
、application-test.properties
、application-prod.properties
在這三個文件均都設置不一樣的server.port
屬性,如:dev環境設置爲1111,test環境設置爲2222,prod環境設置爲3333
application.properties中設置spring.profiles.active=dev
,就是說默認以dev環境設置
測試不一樣配置的加載
application.properties 配置文件
# 多環境配置文件激活屬性 spring.profiles.active=dev
application-dev.properties 配置文件
# 服務端口 server.port=1111 ## 家鄉屬性 Dev home.province=ZheJiang home.city=WenLing home.desc=dev: I'm living in ${home.province} ${home.city}. ###我的屬性 user.name=ilimhumar user.age=25 user.job=Java Programmer user.weight = 70kg user.height=180cm user.desc= My name's ${user.name} ${user.age} ${user.job} ${user.weight} ${user.height}
application-prod.properties 配置文件
# 服務端口 server.port=2222 ## 家鄉屬性 Prod home.province=guangdong home.city=shenzhen home.desc=prod: I'm living in ${home.province} ${home.city}. ###我的屬性 user.name=bilzat user.age=24 user.job=PHP Programmer user.weight = 80kg user.height=180cm user.desc= My name's ${user.name} ${user.age} ${user.job} ${user.weight} ${user.height}
application-test.properties 配置文件
# 服務端口 server.port=3333 ## 家鄉屬性 Prod home.province=jiangsu home.city=hangzhou home.desc=test: I'm living in ${home.province} ${home.city}. ###我的屬性 user.name=elzat user.age=26 user.job=C++ Programmer user.weight = 75kg user.height=180cm user.desc= My name's ${user.name} ${user.age} ${user.job} ${user.weight} ${user.height}
運行效果