问题描述
|
我想根据属性文件的请求获取特定的值。该怎么做?
我有以下春季配置。我想根据请求设置Exprops的值并从属性文件中获取相应的值
<bean id=\"Prop\" class=\"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer\">
<property name=\"location\">
<value>classpath:ErrorMessage.properties</value>
</property>
</bean>
<bean id=\"PropertiesBean\" class=\"com.util.PropertiesUtil\">
<property name=\"Exprops\" value=\"${EXampleExceptiion}\"></property>
</bean>
解决方法
使用PropertiesFactoryBean将属性注入Bean中。
<bean id=\"myPropertiesBean\"
class=\"org.springframework.beans.factory.config.PropertiesFactoryBean\">
<property name=\"location\" value=\"classpath:ErrorMessage.properties\"/>
</bean>
这提供了一个属性对象/ Bean,可以在任何Bean(ѭ3injected)中以名称myPropertiesBean
注入。
另外,Spring提供了util名称空间(从Spring 2.5开始):
在那里,您可以编写一些简短的PropertyFactoryBean定义:
<util:properties id=\"myPropertiesBean\"
location=\"classpath:ErrorMessage.properties\"/>
@请参阅Spring Reference第C.2.2.3。章。
,所有使用以下内容以编程方式执行此操作
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource(\"beans.xml\"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource(\"jdbc.properties\"));
cfg.postProcessBeanFactory(factory);
,<util:properties id=\"\" location=\"location of prop file\" />
这将返回java.util.Properties对象