Spring里PropertyPlaceholderConfigurer类的使用
2012-11-23 11:09
405 查看
这个类是用来解析Java Properties属性文件值,并提供在spring配置期间替换使用属性值。以下是它的三种常见使用方法。
(1).基本的使用方法:
spring-config.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
其中classpath是引用src目录下的文件写法。
(2).当存在多个Properties文件时,配置就需使用locations了:
spring-config.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
(3).接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties文件,其配置如下
Xml代码
<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
Xml代码
<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。
(1).基本的使用方法:
spring-config.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
其中classpath是引用src目录下的文件写法。
(2).当存在多个Properties文件时,配置就需使用locations了:
spring-config.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
(3).接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties文件,其配置如下
Xml代码
<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
Xml代码
<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。
相关文章推荐
- 使用DefaultAdvisorAutoProxyCreator实现spring的自动代理
- spring注解方式,使用jax-ws配置webservice,适合小白。看不会你打死我!
- Dubbo Zookeeper与Spring或者struts2整合和使用
- Spring Boot 学习(一)入门及使用
- quartz整合spring 相关配置及使用
- Spring Boot教程(三十四)使用Redis数据库(2)
- Spring学习笔记(16)----使用Spring配置文件实现事务管理
- 在Spring中使用ActiveMQ发送邮件
- springMVC-Spring propertyConfigurer的使用
- spring boot 使用spring AOP实现拦截器
- 使用注解配置ehcache,环境:Spring4、hibernate4
- Spring配置文件<context:property-placeholder>标签使用漫谈
- Spring-boot 初使用(二)
- spring 使用c3po连接池
- 使用Java(Springboot)操作Redis
- SpringBoot中使用Swagger
- 使用工具创建 SpringBoot Web 应用
- 使用Java(Springboot)操作Redis
- StrutsSpringTestCase中使用convention报Couldn't get resource paths for class path resource的解决方案
- maven中使用spring的test包结合junit4进行测试。