這篇文章將為大家詳細講解有關spring和springboot中屬性配置的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
通過在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上下文中出現了多個 元素,那么應該遵循以下幾個最佳實踐:
需指定order屬性來確定spring處理順序
需要引用其他原始屬性元素應該增加ignore-unresolvable= “true”,使解析機制先不拋出異常的情況下繼續加載其他配置。
Spring 3.1 引入新的 @PropertySource 注解,可以方便地給spring environment中添加property source。該注解與基于Java Configuration 方式配置的@Configuration注解一起使用:
@Configuration @PropertySource("classpath:foo.properties") public class PropertiesWithJavaConfig { //... }
另一個非常有用的注冊方式為使用占位符方式實現運行時動態選擇屬性文件,示例如下:
@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")
spring4中,默認local properties文件最后加載,environment Properties和system Properties之后。local properties是通過PropertiesLoaderSupport 類的API方法 (setProperties, setLocation, etc)手工或編程方式配置的。
這種機制可以通過設置PropertySourcesPlaceholderConfigurer類的localOverride 屬性進行修改,值為true允許local properties覆蓋spring系統加載的。
spring3.0之前,PropertyPlaceholderConfigurer 類嘗試在手工定義源和System Properties兩個地方查找,查找順序也可以通過設置systemPropertiesMode屬性進行配置:
never – 總不檢查 system properties
fallback (default) – 如果指定的properties files查找不到,則檢查 system properties
override – 先檢查system properties,然后再嘗試指定的properties files。這允許system properties覆蓋任何其他屬性源。
最后需注意,如果在兩個或多個通過@PropertySource定義了屬性,那么最后一個定義優先級最高并覆蓋以前的定義。這使得準確的屬性值難以預測,所以如果覆蓋不滿足需求,那么可以重寫PropertySource API。
在我們進入更高級的屬性配置之前,讓我們先看看Spring Boot中屬性加載的新特性。
總的來說與標準Spring相比,這種新支持的配置更少,當然這是Spring Boot的主要目標之一。
spring boot 應用是典型基于配置文件規范約定。我們可以簡單地放“application.properties” 文件在“src/main/resources”目錄中,spring boot會自定監測,我們能在其中放入任何屬性。
通過使用缺省文件,我們無須顯示注冊PropertySource并指定屬性文件路徑。我們也可以在運行時使用環境變量屬性指定不同的屬性配置文件:
java -jar app.jar --spring.config.location=classpath:/another-location.properties
如果我們需要針對不同環境,spring boot內置機制可以滿足。我們可以在“src/main/resources”目錄中定義“application-environment.properties” 文件,然后設置spring profile與environment名稱一致。
例如,如果我們定義“staging” 環境變量,則我們必須定義staging profile,然后定義application-staging.properties屬性文件。
特定環境屬性文件加載優先級高于缺省屬性文件。注意,默認文件仍然會被加載,只是當有屬性沖突時,特定環境屬性文件優先。
在應用測試階段,我們可能需要不同的屬性值。Spring Boot通過在測試運行期間查找“src/test/resources”目錄中的屬性文件來處理這個問題。同樣,默認屬性仍然會被加載,但是如果發生沖突,將會覆蓋這些屬性。
如果需要更細粒度控制測試屬性,我們可以使用@TestPropertySource注解。其可以設置給測試上下文設置測試屬性,比缺省屬性源優先級高:
@ContextConfiguration @TestPropertySource("/my-test.properties") public class IntegrationTests { // tests }
如果我們不想使用文件,也直接指定名稱和值:
@ContextConfiguration @TestPropertySource("foo=bar", "bar=foo") public class IntegrationTests { // tests }
也可以通過@SpringBootTest注解,指定相應properties參數值達到同樣效果:
@SpringBootTest(properties = {"foo=bar", "bar=foo"}) public class IntegrationTests { // tests }
如果屬性按分組形式配置,可以使用 @ConfigurationProperties注解,其會按照對象圖方式映射這些分層組織屬性。下面示例看看數據庫連接配置屬性:
database.url=jdbc:postgresql:/localhost:5432/instance database.username=foo database.password=bar
然后使用注解映射至數據庫對象:
@ConfigurationProperties(prefix = "database") public class Database { String url; String username; String password; // standard getters and setters }
spring boot 在配置方法中再次應用了基于約定原則,自動映射屬性值對象字段,我們僅需提供屬性前綴即可。
YAML文件也支持。
同樣名稱規則可以應用至測試規范、environmet規范以及缺省屬性文件。僅文件擴展名不同以及需提供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"
你也能通過系統屬性實現,需要在-jar命令之前提供:
java -Dproperty.name="value" -jar app.jar
spring boot也能監測環境變量,效果與屬性一樣:
export name=value java -jar app.jar
如果屬性值不確定,RandomValuePropertySource 可以實現給屬性賦隨機值:
random.number=${random.int} random.long=${random.long} random.uuid=${random.uuid}
spring boot 支持很多屬性源,實現較好順序及合理覆蓋。其官方文檔可以參閱。
spring3.1之前
spring3.1引入注解,可以方便地定義屬性源,在此之前,xml配置是必須的。 xml元素自動在spring上下文中注冊新的PropertyPlaceholderConfigurer bean。為了向后兼容,在spring3.1及之后版本中,如果XSD schemas不升級到新的3.1 XSD版本,仍然會創建相應bean。
spring3.1之后
從spring3.1起,XML 元素不再注冊舊的PropertyPlaceholderConfigurer 類,代替引入PropertySourcesPlaceholderConfigurer類,新的類可以實現更靈活地和Environment 和 PropertySource機制進行交互。
對3.1之后版本,應該應用新的標準。
當web應用有父和子上下文時,屬性如何加載是很常見的問題。父上下文有一些通用的核心功能和bean,并包括一個或多個子上下文,可能包含servlet特定的bean。
這種場景下,如何最佳方式定義屬性文件并引入到各自的上下文中?以及如何在spring中以最佳方式獲取這些屬性,下面分類進行說明:
如果文件定義在父上下文:
@Value 在子上下文 : 否
@Value 在父上下文 : 是
如果文件定義在子上下文:
@Value 在子上下文 : 是
@Value 在父上下文 : 否
總之如上所述,沒有暴露屬性給environment,所以environment.getProperty在上下文中不工作。
如果文件定義在父上下文:
@Value 在子上下文 : 是
@Value 在父上下文 : 是
environment.getProperty 在子上下文: 是
environment.getProperty 在父上下文: 是
如果文件定義在子上下文:
@Value 在子上下文 : 是
@Value 在父上下文 : 否
environment.getProperty 在子上下文: 是
environment.getProperty 在父上下文: 否
關于“spring和springboot中屬性配置的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。