在运行时从属性文件中读取值 [英] Reading valued from properties file at Runtime
问题描述
我想根据来自属性文件的请求获取特定值.如何做到这一点?
I want to get specific value based on request from the property file.how to do that?
我有以下 spring 配置.我想根据请求设置 Exprops 的值并从属性文件中获取相应的值
I have following spring configuration.i want to set the value for Exprops as per the request and get corresponding values from the properties file
<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 中以 myPropertiesBean
名称注入的对象/Bean (<property name="x" ref="myPropertiesBean"/>
).
This provides a Properties Object/Bean which can be injected under the name myPropertiesBean
in any Bean (<property name="x" ref="myPropertiesBean"/>
).
此外 Spring 提供了 util 命名空间(自 Spring 2.5 起):在那里你可以编写更短的 PropertyFactoryBean 定义:
In addition Spring provides the util namespace (since Spring 2.5): There you can write the PropertyFactoryBean definition a bit shorter:
<util:properties id="myPropertiesBean"
location="classpath:ErrorMessage.properties"/>
@see Spring 参考章节 C.2.2.3.
这篇关于在运行时从属性文件中读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!