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