PropertiesFactoryBean PropertyPlaceholderConfigurer 区别
2016-07-03 01:13
561 查看
正如 stackoverflow 上说的,PropertiesFactoryBean 是PropertiesLoaderSupport
直接的实现类, 专门用来管理properties文件的工厂bean,默认是单例的,
而 PropertyPlaceholderConfigurer 是 解决 properties 文件占位符问题的,也实现了 PropertiesLoaderSupport 类。
在java 代码里,一般是使用@Value注解来引用 properties 文件的属性。
使用 PropertyPlaceholderConfigurer 时, @Value表达式的用法是 @Value(value="${properties key}") ,
使用 PropertiesFactoryBean 时,我们还可以用@Value 读取 properties对象的值, @Value 用法 是 @Value(value="#{configProperties['properties key']}")
最后 rate 和rate2 值是一样的。
参考资料:
1、 http://stackoverflow.com/questions/20353999/propertiesfactorybean-vs-propertyplaceholderconfigurer-spring
2、 使用spring注解方法读取properties文件中值
3、Spring的@PropertySource和@Value注解例子
4、 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-typesafe-configuration-properties
5 、 http://outofmemory.cn/code-snippet/3700/spring-bean-property-inject
直接的实现类, 专门用来管理properties文件的工厂bean,默认是单例的,
而 PropertyPlaceholderConfigurer 是 解决 properties 文件占位符问题的,也实现了 PropertiesLoaderSupport 类。
在java 代码里,一般是使用@Value注解来引用 properties 文件的属性。
使用 PropertyPlaceholderConfigurer 时, @Value表达式的用法是 @Value(value="${properties key}") ,
使用 PropertiesFactoryBean 时,我们还可以用@Value 读取 properties对象的值, @Value 用法 是 @Value(value="#{configProperties['properties key']}")
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:/config/jdbc.properties</value> <value>classpath:/config/base.properties</value> </list> </property> </bean> <!-- 属性文件读入 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties" ref="configProperties" /> </bean>
@Value(value="${profit.rate.csProfitRate}") double rate = 0.9; @Value(value="#{configProperties['profit.rate.csProfitRate']}") double rate2 = 0.9;
最后 rate 和rate2 值是一样的。
参考资料:
1、 http://stackoverflow.com/questions/20353999/propertiesfactorybean-vs-propertyplaceholderconfigurer-spring
2、 使用spring注解方法读取properties文件中值
3、Spring的@PropertySource和@Value注解例子
4、 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-typesafe-configuration-properties
5 、 http://outofmemory.cn/code-snippet/3700/spring-bean-property-inject
相关文章推荐
- Spring项目启动报错
- Web项目中对配置文件中的密码进行加密(数据库连接密码)
- PropertyPlaceholderConfigurer的用法
- PropertyPlaceholderConfigurer的使用
- PropertyPlaceholderConfigurer的用法
- 使用PropertyPlaceholderConfigurer读取.properties文件(1)
- 使用PropertyPlaceholderConfigurer读取.properties文件(2)-处理加密属性
- 使用PropertyPlaceholderConfigurer读取.properties文件(3)-公共类获取属性
- Spring MVC +Mybatis + Maven 配置之dataSource配置
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题处理方法
- Spring利用propertyConfigurer类 读取.property数据库配置文件
- Spring的PropertyPlaceholderConfigurer应用
- spring源码分析,聊聊PropertyPlaceholderConfigurer
- properties的获取支持,ResourceBundle 和 PropertyPlaceholderConfigurer 方式
- java web项目中的properties文件中的属性值在启动后进行修改(数据库密码加密等)
- Spring中配置和读取多个Properties文件
- propertiesfactorybean vs propertyplaceholderconfigurer spring?
- Spring类PropertyPlaceholderConfigurer的作用
- linux su切换用户提示 Authentication failure的解决方法
- 系统数据表架构