微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在运行时从属性文件读取值

如何解决在运行时从属性文件读取值

| 我想根据属性文件的请求获取特定的值。该怎么做? 我有以下春季配置。我想根据请求设置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对象     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。