1.概述
本教程將展示如何通過XML或Java配置在Spring中設置和使用屬性 。
在Spring 3.1之前 ,將新的屬性文件添加到Spring并使用屬性值并不像它那樣靈活和健壯。 從Spring 3.1開始 ,新的Environment和PropertySource抽象大大簡化了此過程。
2.通過XML名稱空間注冊屬性
使用XML,可以通過以下命名空間元素使Spring訪問新的屬性文件:
<?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-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"><context:property-placeholder location="classpath:foo.properties" /></beans>
foo.properties文件應放在/ src / main / resources下,以便在運行時可在類路徑上使用。
多個
如果在Spring上下文中存在多個<property-placeholder>元素 ,則應遵循一些最佳實踐:
- 需要指定order屬性來固定Spring處理這些訂單的順序
- 所有屬性占位符減去最后一個(最高順序 )應具有ignore-unresolvable =“ true”,以允許解析機制在上下文中傳遞給其他對象而不會引發異常
3.通過Java注釋注冊屬性
Spring 3.1還引入了新的@PropertySource批注 ,作為將屬性源添加到環境的便捷機制。 該注釋將與基于Java的配置和@Configuration注釋一起使用:
@Configuration
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}
}
與使用XML名稱空間元素相反,Java @PropertySource批注不會自動向Spring注冊PropertySourcesPlaceholderConfigurer 。 相反,必須在配置中顯式定義Bean,以使屬性解析機制正常工作。 此意外行為背后的原因是設計使然,并對此問題進行了記錄 。
4.使用屬性
在Spring 3.1中添加的較舊的PropertyPlaceholderConfigurer和新的PropertySourcesPlaceholderConfigurer都可以在bean定義屬性值和@Value批注中解析$ {…}占位符 。
例如,要使用@Value注釋注入屬性:
@Value( "${jdbc.url}" )
private String jdbcUrl;
還可以指定屬性的默認值 :
@Value( "${jdbc.url:aDefaultUrl}" )
private String jdbcUrl;
在Spring XML配置中使用屬性:
<bean id="dataSource"><property name="url" value="${jdbc.url}" />
</bean>
最后,通過新的環境API獲取屬性:
@Autowired
private Environment env;
...
dataSource.setUrl(env.getProperty("jdbc.url"));
一個非常重要的警告是,使用<property-placeholder> 不會將屬性公開給Spring Environment –這意味著檢索這樣的值將不起作用–它將返回null :
env.getProperty("key.something")
4.1屬性搜索優先級
默認情況下,在Spring 3.1中,本地屬性在所有環境屬性源(包括屬性文件)之后排在最后。 可以通過PropertySourcesPlaceholderConfigurer的localOverride屬性來覆蓋此行為,可以將其設置為true以允許本地屬性覆蓋文件屬性。
在Spring 3.0及更低版本中,舊的PropertyPlaceholderConfigurer也嘗試在手動定義的源以及系統屬性中查找屬性。 還可以通過配置程序的systemPropertiesMode屬性自定義查找優先級:
- 從不 –從不檢查系統屬性
- 備用 (默認)–檢查系統屬性,如果在指定的屬性文件中無法解析
- 覆蓋 –在嘗試指定的屬性文件之前,請先檢查系統屬性。 這允許系統屬性覆蓋任何其他屬性源。
最后,請注意,如果在通過@PropertySource定義的兩個或多個文件中定義了屬性,則最后一個定義將獲勝并覆蓋之前的定義 。 這使得確切的屬性值難以預測,因此,如果覆蓋很重要,則可以使用PropertySource API。
5.幕后–Spring配置
5.1。 在Spring 3.1之前
Spring 3.1引入了使用注釋定義屬性源的便捷選項–但在此之前,必須使用XML Configuration。
<context:property-placeholder> XML元素自動在Spring上下文中注冊一個新的PropertyPlaceholderConfigurer bean 。 為了向后兼容,如果XSD架構尚未升級為指向新的3.1 XSD版本,則在Spring 3.1中也是如此。
5.2。 在Spring 3.1之后
從Spring 3.1開始,XML <context:property-placeholder>將不再注冊舊的PropertyPlaceholderConfigurer,而是新注冊的PropertySourcesPlaceholderConfigurer 。 創建此替換類是??為了更靈活并更好地與新引入的Environment and PropertySource機制進行交互。
對于使用Spring 3.1或更高版本的應用程序,應將其視為標準。
6.在Spring 3.0中使用Raw Bean進行配置–
除了將屬性放入Spring(注釋和XML名稱空間)的便捷方法之外,還可以手動定義和注冊屬性配置bean。 使用PropertyPlaceholderConfigurer使我們可以完全控制配置,但缺點是過于冗長,并且在大多數情況下是不必要的。
6.1。 Java配置
@Bean
public static PropertyPlaceholderConfigurer properties(){PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();Resource[] resources = new ClassPathResource[ ]{ new ClassPathResource( "foo.properties" ) };ppc.setLocations( resources );ppc.setIgnoreUnresolvablePlaceholders( true );return ppc;
}
6.2。 XML配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:foo.properties</value></list></property><property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
7.在Spring 3.1中使用Raw Bean進行配置–
同樣,在Spring 3.1中,也可以手動配置新的PropertySourcesPlaceholderConfigurer :
7.1。 Java配置
@Bean
public static PropertySourcesPlaceholderConfigurer properties(){PropertySourcesPlaceholderConfigurer pspc =new PropertySourcesPlaceholderConfigurer();Resource[] resources = new ClassPathResource[ ]{ new ClassPathResource( "foo.properties" ) };pspc.setLocations( resources );pspc.setIgnoreUnresolvablePlaceholders( true );return pspc;
}
7.2。 XML配置
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"><property name="location"><list><value>classpath:foo.properties</value></list></property><property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
8.結論
本文展示了幾個在Spring中使用屬性和屬性文件的示例 ,并討論了舊的Spring 3.0選項以及Spring 3.1中引入的對屬性的新支持。
可以在github項目中找到所有注冊屬性文件和使用屬性值的示例的實現–這是一個基于Eclipse的項目,因此應該很容易直接導入和運行。
翻譯自: https://www.javacodegeeks.com/2012/02/properties-with-spring.html