读书人

由spring治理properties文件的资源

发布时间: 2012-09-02 21:00:34 作者: rapoo

由spring管理properties文件的资源
一种方式是将一批资源文件交由spring管理,在类中以@Value的方式直接提取信息。
classpath目录下创建xxx.properties文件
applicationContext.xml中配置
<bean value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath*:xxx.properties</value>
...
</list>
</property>
</bean>

java类
...
import org.springframework.beans.factory.annotation.Value;
...
@Value("${properties文件中的key}")
private String abc;

现在可以直接使用abc属性了。


一种方式是定义一个properties文件bean,再注入到类中,使用时通过properties.getProperty()来提取信息。
classpath目录下创建xxx.properties文件
applicationContext.xml中配置
...
xmlns:util="http://www.springframework.org/schema/util"
...
<util:properties id="property_a" location="classpath:openview-jms.properties"/>
...

java类
...
import org.springframework.beans.factory.annotation.Autowired;
...
@Autowired
private Properties property_a;
...
现在可以用openview_jms.getProperty("properties文件中的key")提取信息了。

读书人网 >软件架构设计

热点推荐