开发者

spring property files

I need to use the values from the property file... I tried searching it.. What i got is开发者_如何学C... 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?


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>


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜