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

问题描述

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...