spring注解的方式读取properties文件中的内容

在spring的配置文件ApplicationContext.xml中加入.


<!-- 加载所有的properties文件,方便类中用spring注解方式获取 -->

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/properties/*.properties</value>
</list>
</property>

</bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/jdbc.properties" />
<property name="properties" ref="configProperties" />
</bean>


需要用的类里面如下声明:

private String name;

public String getName() {
return name;
}

@Value("#{configProperties[name]}")
public void setName(String name) {
this.name = name;
}

意思就是读取*.properties文件中key为name的值,如果有多个properties文件。默认会合并,所以不需要关注是哪个properties中的key,如果多个properties中有重名的key,默认后面的会覆盖前面的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值