PropertiesFactoryBean PropertyPlaceholderConfigurer 区别
2016-06-16 13:23
483 查看
正如 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
而 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
相关文章推荐
- opencv寻找轮廓
- Linux crontab任务计划服务
- Apache Commons工具集简介
- linux目录随意切换
- apache commons包简介
- tomcat内存溢出之PermGen space
- Docker实例教程[超详细](一)
- PhotoShop CS6 抠图教程
- Linux迁移tomcat所在目录路径
- Linux Centos-6 Mysql修改密码 不需要输入原密码
- 软件架构的两大流派:组成派、决策派
- Linux下安装启动多个Tomcat的配置
- 关于Error:Could not open cp_init class cache for initialization script系统找不到文件
- hadoop 安装教程
- [置顶] MVP:有呼吸的Android架构
- linux修改时区
- 入门———— Linux后门技术及实践 20160616
- SpriteKit关于SKScene中的渲染Loop
- SpriteKit关于SKScene中的渲染Loop
- SpriteKit关于SKScene中的渲染Loop