2

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>
Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
Vish
  • 867
  • 6
  • 19
  • 45
  • PropertyPlaceholderConfigurer is only to configure your application, somehow static, it is not intended to be invoked by you application. – Ralph Apr 14 '11 at 15:56
  • I guess you'll have to implement this yourself. There's no specific support from Spring for that as far as I know. – Benjamin Muschko Apr 14 '11 at 21:31
  • thanks @Ralph,@Benjamin..can any one provide me implementation where file read from PropertyPlaceholderConfigurer can be use to set the value at runtime – Vish Apr 15 '11 at 05:37

3 Answers3

11

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.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thanks@Ralph,This look better – Vish Apr 15 '11 at 09:52
  • Can we write multiple properties in the same PropertiesFactoryBean which will be used by different Spring beans – Vish Apr 15 '11 at 10:56
  • @Vish: PropertiesFactoryBean provides java.util.Properties - which are multiple properties! (or did you mean, multiple propertie files). And yes, you can use them in different beans (each will get its one instance of java.util.Properties, but with the same key/values) – Ralph Apr 15 '11 at 12:47
  • Autowiring this doesn't resolve values if your values are other variables. – TheJeff Apr 30 '18 at 21:40
1

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);
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Venky
  • 85
  • 1
0
<util:properties id="" location="location of prop file" />

this return java.util.Properties object

Sarang
  • 422
  • 5
  • 11