spring 和 spring boot 中的屬性配置方式
本文我們介紹如何在spring中配置和應用屬性——通過xml的 或java Configuration 的@PropertySource。
在Spring 3.1之前,將新的屬性文件添加到Spring中及使用屬性值并不是那么靈活和健壯。從Spring 3.1開始,新的Environment 和 PropertySource 抽象已經(jīng)簡化整個過程。
在xml中注冊屬性文件
通過在xml增加
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:property-placeholder location="classpath:foo.properties" /> </beans>
foo.properties文件可以放在/src/main/resources文件夾中,即運行時類路徑。
多個
如果在Spring上下文中出現(xiàn)了多個 元素,那么應該遵循以下幾個最佳實踐:
- 需指定order屬性來確定spring處理順序
- 需要引用其他原始屬性元素應該增加ignore-unresolvable= “true”,使解析機制先不拋出異常的情況下繼續(xù)加載其他配置。
通過java注解方式注冊屬性文件
Spring 3.1 引入新的 @PropertySource 注解,可以方便地給spring environment中添加property source。該注解與基于Java Configuration 方式配置的@Configuration注解一起使用:
@Configuration @PropertySource("classpath:foo.properties") public class PropertiesWithJavaConfig { //... }
另一個非常有用的注冊方式為使用占位符方式實現(xiàn)運行時動態(tài)選擇屬性文件,示例如下:
@PropertySource({ "classpath:persistence-${envTarget:mysql}.properties" }) …
使用及注入屬性
直接通過 @Value 注解注入屬性:
@Value( "${jdbc.url}" ) private String jdbcUrl;
也可以指定缺省值:
@Value( "${jdbc.url:aDefaultUrl}" ) private String jdbcUrl;
在 Spring XML configuration使用屬性:
<bean id="dataSource"> <property name="url" value="${jdbc.url}" /> </bean>
舊的 PropertyPlaceholderConfigurer 和新的 PropertySourcesPlaceholderConfigurer(Spring 3.1 中引入)都可以解析xml bean定義和@value注解中的 ${…} 占位符 。
最后,使用新的Environment API可以獲取屬性值:
@Autowired private Environment env; ... dataSource.setUrl(env.getProperty("jdbc.url"));
特別需要注意的是,使用不會暴露屬性給 Spring Environment,這意味這下面代碼會返回null:
env.getProperty("key.something")
屬性搜索優(yōu)先級
spring4中,默認local properties文件最后加載,environment Properties和system Properties之后。local properties是通過PropertiesLoaderSupport 類的API方法 (setProperties, setLocation, etc)手工或編程方式配置的。
這種機制可以通過設置PropertySourcesPlaceholderConfigurer類的localOverride 屬性進行修改,值為true允許local properties覆蓋spring系統(tǒng)加載的。
spring3.0之前,PropertyPlaceholderConfigurer 類嘗試在手工定義源和System Properties兩個地方查找,查找順序也可以通過設置systemPropertiesMode屬性進行配置:
- never – 總不檢查 system properties
- fallback (default) – 如果指定的properties files查找不到,則檢查 system properties
- override – 先檢查system properties,然后再嘗試指定的properties files。這允許system properties覆蓋任何其他屬性源。
最后需注意,如果在兩個或多個通過@PropertySource定義了屬性,那么最后一個定義優(yōu)先級最高并覆蓋以前的定義。這使得準確的屬性值難以預測,所以如果覆蓋不滿足需求,那么可以重寫PropertySource API。
spring boot 屬性加載
在我們進入更高級的屬性配置之前,讓我們先看看Spring Boot中屬性加載的新特性。
總的來說與標準Spring相比,這種新支持的配置更少,當然這是Spring Boot的主要目標之一。
application.properties – 缺省屬性文件
spring boot 應用是典型基于配置文件規(guī)范約定。我們可以簡單地放“application.properties” 文件在“src/main/resources”目錄中,spring boot會自定監(jiān)測,我們能在其中放入任何屬性。
通過使用缺省文件,我們無須顯示注冊PropertySource并指定屬性文件路徑。我們也可以在運行時使用環(huán)境變量屬性指定不同的屬性配置文件:
java -jar app.jar --spring.config.location=classpath:/another-location.properties
特定環(huán)境屬性文件
如果我們需要針對不同環(huán)境,spring boot內(nèi)置機制可以滿足。我們可以在“src/main/resources”目錄中定義“application-environment.properties” 文件,然后設置spring profile與environment名稱一致。
例如,如果我們定義“staging” 環(huán)境變量,則我們必須定義staging profile,然后定義application-staging.properties屬性文件。
特定環(huán)境屬性文件加載優(yōu)先級高于缺省屬性文件。注意,默認文件仍然會被加載,只是當有屬性沖突時,特定環(huán)境屬性文件優(yōu)先。
特定測試屬性文件
在應用測試階段,我們可能需要不同的屬性值。Spring Boot通過在測試運行期間查找“src/test/resources”目錄中的屬性文件來處理這個問題。同樣,默認屬性仍然會被加載,但是如果發(fā)生沖突,將會覆蓋這些屬性。
@TestPropertySource注解
如果需要更細粒度控制測試屬性,我們可以使用@TestPropertySource注解。其可以設置給測試上下文設置測試屬性,比缺省屬性源優(yōu)先級高:
@ContextConfiguration @TestPropertySource("/my-test.properties") public class IntegrationTests { // tests }
如果我們不想使用文件,也直接指定名稱和值:
@ContextConfiguration @TestPropertySource("foo=bar", "bar=foo") public class IntegrationTests { // tests }
也可以通過@SpringBootTest注解,指定相應properties參數(shù)值達到同樣效果:
@SpringBootTest(properties = {"foo=bar", "bar=foo"}) public class IntegrationTests { // tests }
層次屬性
如果屬性按分組形式配置,可以使用 @ConfigurationProperties注解,其會按照對象圖方式映射這些分層組織屬性。下面示例看看數(shù)據(jù)庫連接配置屬性:
database.url=jdbc:postgresql:/localhost:5432/instance database.username=foo database.password=bar
然后使用注解映射至數(shù)據(jù)庫對象:
@ConfigurationProperties(prefix = "database") public class Database { String url; String username; String password; // standard getters and setters }
spring boot 在配置方法中再次應用了基于約定原則,自動映射屬性值對象字段,我們僅需提供屬性前綴即可。
YAML 文件
YAML文件也支持。
同樣名稱規(guī)則可以應用至測試規(guī)范、environmet規(guī)范以及缺省屬性文件。僅文件擴展名不同以及需提供SnakeYAML依賴。
YAML對層次屬性存儲特別方便,下面的屬性文件:
database.url=jdbc:postgresql:/localhost:5432/instance database.username=foo database.password=bar secret: foo
對應的YAML文件為:
database: url: jdbc:postgresql:/localhost:5432/instance username: foo password: bar secret: foo
需要注意的是YAML文件不支持使用@PropertySource注解,所以如果使用該注解則必須使用屬性文件。
命令行傳入屬性
相對于使用文件,屬性也可以直接通過命令行進行傳遞:
java -jar app.jar --property="value"
你也能通過系統(tǒng)屬性實現(xiàn),需要在-jar命令之前提供:
java -Dproperty.name="value" -jar app.jar
環(huán)境變量屬性
spring boot也能監(jiān)測環(huán)境變量,效果與屬性一樣:
export name=value java -jar app.jar
隨機屬性值
如果屬性值不確定,RandomValuePropertySource 可以實現(xiàn)給屬性賦隨機值:
random.number=${random.int} random.long=${random.long} random.uuid=${random.uuid}
其他類型的屬性源
spring boot 支持很多屬性源,實現(xiàn)較好順序及合理覆蓋。其官方文檔可以參閱。
spring配置實現(xiàn)
- spring3.1之前
spring3.1引入注解,可以方便地定義屬性源,在此之前,xml配置是必須的。 xml元素自動在spring上下文中注冊新的PropertyPlaceholderConfigurer bean。為了向后兼容,在spring3.1及之后版本中,如果XSD schemas不升級到新的3.1 XSD版本,仍然會創(chuàng)建相應bean。
- spring3.1之后
從spring3.1起,XML 元素不再注冊舊的PropertyPlaceholderConfigurer 類,代替引入PropertySourcesPlaceholderConfigurer類,新的類可以實現(xiàn)更靈活地和Environment 和 PropertySource機制進行交互。
對3.1之后版本,應該應用新的標準。
多層級上下文中屬性加載
當web應用有父和子上下文時,屬性如何加載是很常見的問題。父上下文有一些通用的核心功能和bean,并包括一個或多個子上下文,可能包含servlet特定的bean。
這種場景下,如何最佳方式定義屬性文件并引入到各自的上下文中?以及如何在spring中以最佳方式獲取這些屬性,下面分類進行說明:
屬性文件通過定義xml中
如果文件定義在父上下文:
- @Value 在子上下文 : 否
- @Value 在父上下文 : 是
如果文件定義在子上下文:
- @Value 在子上下文 : 是
- @Value 在父上下文 : 否
總之如上所述,沒有暴露屬性給environment,所以environment.getProperty在上下文中不工作。
屬性文件通過@PropertySource定義在java中
如果文件定義在父上下文:
- @Value 在子上下文 : 是
- @Value 在父上下文 : 是
- environment.getProperty 在子上下文: 是
- environment.getProperty 在父上下文: 是
如果文件定義在子上下文:
- @Value 在子上下文 : 是
- @Value 在父上下文 : 否
- environment.getProperty 在子上下文: 是
- environment.getProperty 在父上下文: 否
總結
本文通過幾個示例說明了spring中加載屬性機制。希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解SpringBoot中@PostMapping注解的用法
在SpringBoot中,我們經(jīng)常需要編寫RESTful Web服務,以便于客戶端與服務器之間的通信,@PostMapping注解可以讓我們更方便地編寫POST請求處理方法,在本文中,我們將介紹@PostMapping注解的作用、原理,以及如何在SpringBoot應用程序中使用它2023-06-06elasticsearch索引index數(shù)據(jù)功能源碼示例
這篇文章主要為大家介紹了elasticsearch索引index功能源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04idea使用mybatis插件mapper中的方法爆紅的解決方案
這篇文章主要介紹了idea使用mybatis插件mapper中的方法爆紅的解決方案,文中給出了詳細的原因分析和解決方案,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-07-07