Vish
Vish

Reputation: 879

Reading valued from properties file at Runtime

I want to get specific value based on request from the property file.how to do that?

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>

Upvotes: 2

Views: 18513

Answers (3)

Sarang
Sarang

Reputation: 512

<util:properties id="" location="location of prop file" />

this return java.util.Properties object

Upvotes: 0

Venky
Venky

Reputation: 85

All use the following for doing this programmatically

XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
cfg.postProcessBeanFactory(factory);

Upvotes: 1

Ralph
Ralph

Reputation: 120761

Use the PropertiesFactoryBean to inject the Properties in a Bean.

<bean id="myPropertiesBean"
  class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location" value="classpath:ErrorMessage.properties"/>
</bean>

This provides a Properties Object/Bean which can be injected under the name myPropertiesBean in any Bean (<property name="x" ref="myPropertiesBean"/>).

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 Reference Chapter C.2.2.3.

Upvotes: 11

Related Questions