views:

49

answers:

2

Hello all... I need to use the values from the property file... I tried searching it.. What i got is... i need to define a bean of PropertyPlaceHolderConfirguartion under beans.factory.config package of spring framework. But i wish to use it in a pure java class. Depending on a particular value selected, i need to load a particular property file and use the property. How can i achieve this?

A: 

In your Spring config file, you can have something like this;

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="classpath:myapp.properties"/>
</bean>

Then say you have a class like this;

package com.myorg;

public class MyClass
{
  private String myProperty;

  public MyClass(String myProperty)
  {
    this.myProperty = myProperty;
  }

  //other stuff
}

You can use Spring to define a bean and give it properties by adding this to your Spring config file;

<bean id="myBean" class="com.myorg.MyClass">
  <constructor-arg type="java.lang.String" value="${my.prop.name}"/>
</bean>
Qwerky
<code><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:myapp.properties"/></bean></code> under this bean definition i want to add the property value for prperty file name dyanmic... It is dependant on a condition.
apoorvabade
What condition, do you want to use a different property time in runtime, or in build time (eg use different property files for different environments; dev, test, prod)?
Qwerky
A: 

Something like this;

spring-config:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations" value="dir/settings.properties" />  
</bean>

<bean id="beanName" class="classNameWhereValuesAreRequired">
   <property name="nameOfValue" value="${value.name}" />
</bean>

settings.properties:

value.name=ValueRequired
Jivings