views:

2930

answers:

3

Hi there,

i want to fill a bean list property using Spring properties place holder.

Context file
------------
<bean name="XXX" class="XX.YY.Z">
      <property name="urlList">
            <value>${prop.list}</value>
      </property>
</bean>

Properties File
---------------
prop.list.one=foo
prop.list.two=bar

Any help would be much appreciated

+1  A: 

The only way i see here is, implement the interface 'MessageSourceAware' to get the messageResource, and then manually populate your list.

class MyMessageSourceAwareClass implemets MessageSourceAware{
    public static MessageSource messageSource = null;

    public void setMessageSource(MessageSource _messageSource) {
     messageSource = _messageSource;
    }

    public static String getMessage( String code){
     return messageSource.getMessage(code, null, null );
    }

}

--- Properties File ---

prop.list=foo;bar;one more

Populate your list like this

String strlist = MyMessageSourceAwareClass.getMessage ( "prop.list" );

if ( StringUtilities.isNotEmptyString ( strlist ) ){
   String[] arrStr = strList.split(";");
   myBean.setList ( Arrays.asList ( arrStr ) );
}
Rakesh Juyal
+2  A: 

Use a util:properties element to load your properties. You can use PropertyPlaceholderConfigurer to specify the path to your file:

<bean name="XXX" class="XX.YY.Z">
  <property name="urlList">
    <util:properties location="${path.to.properties.file}"/>
  </property>
</bean>

Update I've misunderstood the question; you only want to return properties where key starts with specific string. The easiest way to achieve that would be to do so within setter method of your bean. You'll have to pass the string to your bean as a separate property. Extending the above declaration:

<bean name="XXX" class="XX.YY.Z" init-method="init">
  <property name="urlList">
     <!-- not sure if location has to be customizable here; set it directly if needed -->
    <util:properties location="${path.to.properties.file}"/>
  </property>
  <property name="propertyFilter" value="${property.filter}" />
</bean>

In your XX.YY.Z bean:

private String propertyFilter;
private Properties propertiesHolder;
private List<String> urlList;

// add setter methods for propertyFilter / propertiesHolder

// initialization callback
public void init() {
  urlList = new ArrayList<String>();
  for (Enumeration en = this.propertiesHolder.keys(); en.hasMoreElements(); ) {
    String key = (String) en.nextElement();
    if (key.startsWith(this.propertyFilter + ".") { // or whatever condition you want to check
      this.urlList.add(this.propertiesHolder.getProperty(key));
    }
  } // for
}

If you need to do this in many different places you can wrap the above functionality into a FactoryBean.

ChssPly76
I don't think that's what he's asking, he wanted to inject just the values of a properties file, but only those with a key of a certain prefix, and he wants them as a `List`. A rather esoteric and specific set of requirements.
skaffman
You're right! I want to filter the properties loaded as a list using a prefix
Lici
@skaffman - thank you, you're completely right. I suppose having properties start with 'prop.list' should have been a clue, it just never occurred to me that OP wants them filtered.
ChssPly76
+1  A: 

Just add the following Bean definition

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
     <value>classpath:myprops.properties</value>
    </list>
    </property>
</bean>

To use it like so please note port is defined in myprops.properties

<bean id="mybean" class="com.mycompany.Class" init-method="start">
 <property name="portNumber" value="${port}"/>
</bean>
Paul Whelan
you can use **<context:property-placeholder location="classpath:myprops.properties"/>**
Rakesh Juyal
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer
Rakesh Juyal