自定義屬性與加載
我們在使用Spring Boot的時候,通常也需要定義一些自己使用的屬性,我們可以如下方式直接定義:
application-dev.yml
com.didispace.blog: name: 程序猿DD title: Spring Boot教程 desc: ${com.didispace.blog.name}正在努力寫《${com.didispace.blog.title}》 # 隨機字符串 value: ${random.value} # 隨機int number: ${random.int} # 隨機long bignumber: ${random.long} # 10以內的隨機數 test1: ${random.int(10)} # 10-20的隨機數 test2: ${random.int[10,20]}
package com.wls.integrateplugs.property; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author 程序猿DD * @version 1.0.0 * @date 16/5/5 下午12:16. * @blog http://blog.didispace.com */ @Component public class BlogProperties { @Value("${com.didispace.blog.name}") private String name; @Value("${com.didispace.blog.title}") private String title; @Value("${com.didispace.blog.desc}") private String desc; @Value("${com.didispace.blog.value}") private String value; @Value("${com.didispace.blog.number}") private Integer number; @Value("${com.didispace.blog.bignumber}") private Long bignumber; @Value("${com.didispace.blog.test1}") private Integer test1; @Value("${com.didispace.blog.test2}") private Integer test2; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } public Long getBignumber() { return bignumber; } public void setBignumber(Long bignumber) { this.bignumber = bignumber; } public Integer getTest1() { return test1; } public void setTest1(Integer test1) { this.test1 = test1; } public Integer getTest2() { return test2; } public void setTest2(Integer test2) { this.test2 = test2; } }
按照慣例,通過單元測試來驗證BlogProperties中的屬性是否已經根據配置文件加載了。然后通過@Value("${屬性名}")
注解來加載對應的配置屬性,具體如下:
單元測試
package com.wls.test.integrateplugs.property; import com.wls.integrateplugs.property.BlogProperties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * * @author 程序猿DD * @version 1.0.0 * @blog http://blog.didispace.com * */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class BlogPropertiesTest { private static final Log log = LogFactory.getLog(BlogPropertiesTest.class); @Autowired private BlogProperties blogProperties; @Test public void test1() throws Exception { Assert.assertEquals("程序猿DD", blogProperties.getName()); Assert.assertEquals("Spring Boot教程", blogProperties.getTitle()); Assert.assertEquals("程序猿DD正在努力寫《Spring Boot教程》", blogProperties.getDesc()); log.info("隨機數測試輸出:"); log.info("隨機字符串 : " + blogProperties.getValue()); log.info("隨機int : " + blogProperties.getNumber()); log.info("隨機long : " + blogProperties.getBignumber()); log.info("隨機10以下 : " + blogProperties.getTest1()); log.info("隨機10-20 : " + blogProperties.getTest2()); } }
參數間的引用
在application.properties
中的各個參數之間也可以直接引用來使用,就像下面的設置:
com.didispace.blog.name=程序猿DD com.didispace.blog.title=Spring Boot教程 com.didispace.blog.desc=${com.didispace.blog.name}正在努力寫《${com.didispace.blog.title}》
com.didispace.blog.desc
參數引用了上文中定義的name
和title
屬性,最后該屬性的值就是程序猿DD正在努力寫《Spring Boot教程》
。
使用隨機數
在一些情況下,有些參數我們需要希望它不是一個固定的值,比如密鑰、服務端口等。Spring Boot的屬性配置文件中可以通過${random}
來產生int值、long值或者string字符串,來支持屬性的隨機值。
# 隨機字符串 com.didispace.blog.value=${random.value} # 隨機int com.didispace.blog.number=${random.int} # 隨機long com.didispace.blog.bignumber=${random.long} # 10以內的隨機數 com.didispace.blog.test1=${random.int(10)} # 10-20的隨機數 com.didispace.blog.test2=${random.int[10,20]}
通過命令行設置屬性值
相信使用過一段時間Spring Boot的用戶,一定知道這條命令:java -jar xxx.jar --server.port=8888
,通過使用–server.port屬性來設置xxx.jar應用的端口為8888。
在命令行運行時,連續的兩個減號--
就是對application.properties
中的屬性值進行賦值的標識。所以,java -jar xxx.jar --server.port=8888
命令,等價於我們在application.properties
中添加屬性server.port=8888
,該設置在樣例工程中可見,讀者可通過刪除該值或使用命令行來設置該值來驗證。
通過命令行來修改屬性值固然提供了不錯的便利性,但是通過命令行就能更改應用運行的參數,那豈不是很不安全?是的,所以Spring Boot也貼心的提供了屏蔽命令行訪問屬性的設置,只需要這句設置就能屏蔽:SpringApplication.setAddCommandLineProperties(false)
。
多環境配置
我們在開發Spring Boot應用時,通常同一套程序會被應用和安裝到幾個不同的環境,比如:開發、測試、生產等。其中每個環境的數據庫地址、服務器端口等等配置都會不同,如果在為不同環境打包時都要頻繁修改配置文件的話,那必將是個非常繁瑣且容易發生錯誤的事。
對於多環境的配置,各種項目構建工具或是框架的基本思路是一致的,通過配置多份不同環境的配置文件,再通過打包命令指定需要打包的內容之后進行區分打包,Spring Boot也不例外,或者說更加簡單。
在Spring Boot中多環境配置文件名需要滿足application-{profile}.properties
的格式,其中{profile}
對應你的環境標識,比如:
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環境設置 -
測試不同配置的加載
- 執行
java -jar xxx.jar
,可以觀察到服務端口被設置為1111
,也就是默認的開發環境(dev) - 執行
java -jar xxx.jar --spring.profiles.active=test
,可以觀察到服務端口被設置為2222
,也就是測試環境的配置(test) - 執行
java -jar xxx.jar --spring.profiles.active=prod
,可以觀察到服務端口被設置為3333
,也就是生產環境的配置(prod)
- 執行
按照上面的實驗,可以如下總結多環境的配置思路:
application.properties
中配置通用內容,並設置spring.profiles.active=dev
,以開發環境為默認配置application-{profile}.properties
中配置各個環境不同的內容- 通過命令行方式去激活不同環境的配置