對 Spring 里面的 Properties 不理解的開發者可能會覺得有點亂,主要是因為配置方式很多種,使用方式也很多種。
本文不是原理分析、源碼分析文章,只是希望可以幫助讀者更好地理解和使用 Spring Properties。
Properties 的使用
本文的讀者都是使用過 Spring 的,先來看看 Properties 是怎么使用的,Spring 中常用的有以下幾種使用方式:
1. 在 xml 配置文件中使用
即自動替換 ${} 里面的值。
1
2
3
|
< bean id = "xxx" class = "com.javadoop.Xxx" > < property name = "url" value = "${javadoop.jdbc.url}" /> </ bean > |
2. 通過 @Value 注入使用
1
2
|
@Value ( "${javadoop.jdbc.url}" ) private String url; |
3. 通過 Environment 獲取
此法有需要注意的地方。并不是所有的配置方式都支持通過 Environment 接口來獲取屬性值,親測只有使用注解 @PropertySource 的時候可以用,否則會得到 null,至于怎么配置,下面馬上就會說。
1
2
3
4
5
6
|
@Autowired private Environment env; public String getUrl() { return env.getProperty( "javadoop.jdbc.url" ); } |
如果是 Spring Boot 的 application.properties 注冊的,那也是可以的。
Properties 配置
前面我們說了怎么使用我們配置的 Properties,那么該怎么配置呢?Spring 提供了很多種配置方式。
1. 通過 xml 配置
下面這個是最常用的配置方式了,很多項目都是這么寫的:
1
|
< context:property-placeholder location = "classpath:sys.properties" /> |
2. 通過 @PropertySource 配置
前面的通過 xml 配置非常常用,但是如果你也有一種要消滅所有 xml 配置文件的沖動的話,你應該使用以下方式:
1
2
3
4
5
|
@PropertySource ( "classpath:sys.properties" ) @Configuration public class JavaDoopConfig { } |
注意一點,@PropertySource 在這里必須搭配 @Configuration 來使用,具體不展開說了。
3. PropertyPlaceholderConfigurer
如果讀者見過這個,也不必覺得奇怪,在 Spring 3.1 之前,經常就是這么使用的:
1
2
3
4
5
6
7
8
9
|
< bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > < property name = "locations" > < list > < value >classpath:sys.properties</ value > </ list > </ property > < property name = "ignoreUnresolvablePlaceholders" value = "true" /> <!-- 這里可以配置一些屬性 --> </ bean > |
當然,我們也可以用相應的 java configuration 的版本:
1
2
3
4
5
6
7
8
|
@Bean public PropertyPlaceholderConfigurer propertiess() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[]{ new ClassPathResource( "sys.properties" )}; ppc.setLocations(resources); ppc.setIgnoreUnresolvablePlaceholders( true ); return ppc; } |
4. PropertySourcesPlaceholderConfigurer
到了 Spring 3.1 的時候,引入了 PropertySourcesPlaceholderConfigurer,這是一個新的類,注意看和之前的 PropertyPlaceholderConfigurer 在名字上多了一個 Sources,所屬的包也不一樣,它在 Spring-Context 包中。
在配置上倒是沒有什么區別:
1
2
3
4
5
6
7
8
9
|
< bean class = "org.springframework.context.support.PropertySourcesPlaceholderConfigurer" > < property name = "locations" > < list > < value >classpath:sys.properties</ value > </ list > </ property > < property name = "ignoreUnresolvablePlaceholders" value = "true" /> <!-- 這里可以配置一些屬性 --> </ bean > |
也來一個 java configuration 版本吧:
1
2
3
4
5
6
7
8
|
@Bean public PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[]{ new ClassPathResource( "sys.properties" )}; pspc.setLocations(resources); pspc.setIgnoreUnresolvablePlaceholders( true ); return pspc; } |
Spring Boot 相關
Spring Boot 真的是好東西,開箱即用的感覺實在是太好了。這里簡單介紹下相關的內容。
快速生成一個 Spring Boot 項目: https://start.spring.io/
application.properties
我們每個項目都默認有一個 application.properties 文件,這個配置文件不需要像前面說的那樣進行 注冊 ,Spring Boot 會幫我們自動注冊。
當然,也許你想換個名字也是可以的,在啟動的時候指定你的文件名字就可以了:
1
|
java -Dspring.config.location=classpath:sys.properties -jar app.jar |
application-{env}.properties
為了給不同的環境指定不同的配置,我們會用到這個。
比如測試環境和生產環境的數據庫連接信息就不一樣。
所以,在 application.properties 的基礎上,我們還需要新建 application-dev.properties 和 application-prd.properties,用于配置環境相關的信息,然后啟動的時候指定環境。
1
|
java -Dspring.profiles.active=prd -jar app.jar |
結果就是,application.properties 和 application-prd.properties 兩個文件中的配置都會注冊進去,如果有重復的 key,application-prd.properties 文件中的優先級較高。
@ConfigurationProperties
這個注解是 Spring Boot 中才有的。
即使大家不使用這個注解,大家也可能會在開源項目中看到這個,這里簡單介紹下。
來一個例子直觀一些。按照之前說的,在配置文件中填入下面的信息,你可以選擇寫入 application.properties 也可以用第一節介紹的方法。
1
2
3
|
javadoop.database.url=jdbc:mysql: javadoop.database.username=admin javadoop.database.password=admin123456 |
java 文件:
1
2
3
4
5
6
7
8
|
@Configuration @ConfigurationProperties (prefix = "javadoop.database" ) public class DataBase { String url; String username; String password; // getters and setters } |
這樣,就在 Spring 的容器中就自動注冊了一個類型為 DataBase 的 bean 了,而且屬性都已經 set 好了。
在啟動過程中動態修改屬性值
這個我覺得都不需要太多介紹,用 Spring Boot 的應該基本上都知道。
屬性配置有個覆蓋順序,也就是當出現相同的 key 的時候,以哪里的值為準。
啟動參數 > application-{env}.properties > application.properties
啟動參數動態設置屬性:
1
|
java -Djavadoop.database.password=admin4321 -jar app.jar |
另外,還可以利用系統環境變量設置屬性,還可以指定隨機數等等,確實很靈活,不過沒什么用,就不介紹了。
總結
讀者如果想要更加深入地了解 Spring 的 Properties,需要去理解 Spring 的 Environment 接口相關的源碼。建議感興趣的讀者去翻翻源代碼看看。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.importnew.com/27997.html?