spring中context:property-placeholder标签详解
spring中context:property-placeholder標簽的使用說明
1,有些參數在某些階段中是常量。
分布式應用中client端的server地址,端口等
期望:有一種方案可以方便我們在一個階段內不需要頻繁寫一個參數的值,而在不同階段間又可以方便的切換參數的配置信息
解決:spring3中提供了一種簡便的方式就是``元素
只需要在spring配置文件中添加一句:
即可,這里的location值為參數配置文件的位置,配置文件通常放到src目錄下,參數配置文件的格式即鍵值對的形式
#jdbc配置 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=root這樣一來就可以為spring配置的bean的屬性設置值了,比如spring有一個數據源的類
<bean id="dataSource" class="org.springframework,jdbc,datasource.DriverManagerDataSource"><property name="driverClassName" value="${driverClassName}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/> </bean>甚至可以將 ${} 這種形式的變量用在spring提供的注解當中,為注解的屬性提供值(下面會講到)
Spring容器采用反射掃描的發現機制,在探測到Spring容器中有一個 org.springframework.beans.config.PropertyPlaceholderConfigurer的Bean就會停止對剩余PropertyPlaceholderConfigurer的掃描,
換句話說,即Spring容器僅允許最多定義一個PropertyPlaceholderConfigurer 或 <content:property-placeholder>其余的會被Spring忽略。
由于Spring容器只能有一個PropertyPlaceholderConfigurer,如果有多個屬性文件,這時就看誰先誰后了,先的保留 ,后的忽略。
還有一種情況,是Spring 自動注入 properties文件中的配置:要自動注入properties文件中的配置,需要在Spring配置文件中添加 org.springframework.beans.factory.config.PropertiesFactoryBean和org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer的實例配置
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value> classpath*:application.properties</value></list></property> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="properties" ref="configProperties" /> </bean>在這個配置文件中我們配置了注解掃描和configProperties實例和propertyConfigurer實例,這樣我們就可以在java類中自動注入配置了
@Component public class Test{@Value("#{configProperties['userName']}")private String userName;public String getUserName(){return userName;} }自動注入需要使用 @Value 這個注解,這個注解的格式 #{configProperties[‘userName’]}其中configProperties是我們在配置文件中配置的bean的 id, userName 是在配置文件中配置的項。
同時出現兩個該配置時出現錯誤
<context:property-placeholder location="classpath:a.properties"/> <context:property-placeholder location="classpath:b.properties"/>同個模塊中如果出現多個context:property-placeholder ,location properties文件后,運行時出現Could not resolve placeholder ‘key’ in string value${key}。
解決辦法:
原因是在加載第一個context:property-placeholder時會掃描所有的bean,而有的bean里面出現第二個 context:property-placeholder引入的properties的占位符${key},spring只加載第一個配置,后面的將會被忽略,所以解析不了${key}。
-
解決辦法一:
可以將通過模塊的多個property-placeholder合并為一個,將初始化放在一起。 -
解決方法二:
添加ignore-unresolvable=“true”,這樣可以在加載第一個property-placeholder時出現解析不了的占位符進行忽略掉。
而且這樣會導致后面那個b.properties配置文件失效
原因:Spring 只會加載第一個context:property-placeholder配置后面的文件將不會再次進行加載,所以導致后面的文件讀取不到
context:property-placeholder標簽屬性詳解
<context:property-placeholder location="屬性文件,多個之間逗號分隔" file-encoding="文件編碼" ignore-resource-not-found="是否忽略找不到的屬性文件" ignore-unresolvable="是否忽略解析不到的屬性,如果不忽略,找不到將拋出異常" properties-ref="本地Properties配置" local-override="是否本地覆蓋模式,即如果true,那么properties-ref的屬性將覆蓋location加載的屬性,否則相反" system-properties-mode="系統屬性模式,默認ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永遠不用ENVIRONMENT的,OVERRIDE類似于ENVIRONMENT" order="順序" />location:表示屬性文件位置,多個之間通過如 逗號(,)/分號(;)等分隔;
file-encoding:文件編碼;
ignore-resource-not-found:如果屬性文件找不到,是否忽略,默認false,即不忽略,找不到將拋出異常;
ignore-unresolvable:是否忽略解析不到的屬性,如果不忽略,找不到將拋出異常;
properties-ref:本地java.util.Properties配置;
local-override:是否本地覆蓋模式,即如果true,那么properties-ref的屬性將覆蓋location加載的屬性;
system-properties-mode:系統屬性模式,ENVIRONMENT(默認),OVERRIDE,NEVER
- ENVIRONMENT:將使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情況使用Spring 3.1之前的PropertyPlaceholderConfigurer如果是本地覆蓋模式:那么查找順序是:properties-ref、location、environment,否則正好反過來;
- OVERRIDE: PropertyPlaceholderConfigurer使用,因為在spring 3.1之前版本是沒有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment如果是本地覆蓋模式:那么查找順序是:properties-ref、location、System.getProperty(),System.getenv(),否則正好反過來;
- NEVER:只查找properties-ref、location;
order:當配置多個context:property-placeholder/查找順序;
總結
以上是生活随笔為你收集整理的spring中context:property-placeholder标签详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3精要(21)-函数(2)
- 下一篇: 浅谈对称加密与非对称加密