使用spring注解方法读取properties文件中值
2015-11-16 17:23
375 查看
引用properties还是要在xml里配置,有两种配置方法,对引用单个properties文件,和多个properties文件
一、读取单个 properties 文件
在 spring 的配置文件中,加入
引入until命名空间:
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
读取properties文件
<util:properties id="propertiesReader" location="classpath:test.properties" />
二、读取多个 properties 文件
<bean id="propertiesReader"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:param.properties</value>
<value>classpath:base.properties</value>
</list>
</property>
</bean>
两种方法其实都是一样的
在类中读取properties文件属性
在类中需要注入的属性实现 setter 和 getter 方法。
在 setter 方法前,添加 @Value 注解
@Value("#{propertiesReader[propertiesName]}")
propertiesName 为 properties 文件中的键。这样,在容器启动过程中, Spring 将自动注入值。
一、读取单个 properties 文件
在 spring 的配置文件中,加入
引入until命名空间:
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
读取properties文件
<util:properties id="propertiesReader" location="classpath:test.properties" />
二、读取多个 properties 文件
<bean id="propertiesReader"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:param.properties</value>
<value>classpath:base.properties</value>
</list>
</property>
</bean>
两种方法其实都是一样的
在类中读取properties文件属性
在类中需要注入的属性实现 setter 和 getter 方法。
在 setter 方法前,添加 @Value 注解
@Value("#{propertiesReader[propertiesName]}")
propertiesName 为 properties 文件中的键。这样,在容器启动过程中, Spring 将自动注入值。
相关文章推荐
- 对两个整数变量的值进行互换(不需要第三方变量)
- json java javabean xml 之相互转换 适合初学者
- Spring中实现IOC的方式:依赖注入(Dependency Injection)
- SSH整合教程(struts2+spring3+hibernate3)(含登陆示例)
- java获取当前时间、当前零点零分零秒、昨天这一时间
- java日期类封装
- Java引用数据类型之数组
- HibernateTemplate和HibernateDaoSupport(spring注入问题)
- Spring面试题和答案
- Java实现二叉树的创建和遍历操作(有更新)
- Java实现二叉树的创建和遍历操作
- java 使用 mail 发送邮件
- spring mvc 框架搭建及详解
- MyEclipse2014 常用设置优化
- eclipse上单步调试Hive
- springmvc前台往后传值的几种方式
- java中的排序方法
- Spring AOP在Bean生命周期中的调用时机
- 加密解密java
- spring 定时任务的写法