Results 1 to 4 of 4

Thread: Bean collection propertie from properties file

  1. #1
    Join Date
    Jan 2008
    Posts
    2

    Default Bean collection propertie from properties file

    Hello,

    I'm currently converting over our spring config file to use the context property placeholder to populate the bean properties from a properties file. eg :

    <contextroperty-placeholder location="classpath:spring.properties" />

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${db.class}"/>
    ....


    A few of the beans have properties that are collections, (mostly List<String>). These were previously configured via the xml collection markup <value>blah1</value><value>blah2</value><value>blah3</value>.

    I've tried putting the xml into the properties file using the normal method, but I believe this means that it skips the phase during startup where the xml is converted to a list.

    Does anyone know a way to set collection properties on beans via a properties file?

    Many thanks in advance!

    p.s sorry for the typo in the subject!

  2. #2
    Join Date
    Jan 2008
    Posts
    2

    Default

    So I guess no-one knows a way to do this? I've been looking everywhere to no avail.. maybe I need to add a JIRA request or something?

  3. #3
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Afaik there is no out-of-the-box utility for collection handling.
    I think you have to create your own BeanPostProcessor that allows this.
    You need to define a format however, that can provide a value collection out of properties.

    Possible examples:
    Code:
    collection.1=val1
    collection.2=val2
    collection.3=val3
    or

    Code:
    collection=val1;val2;val3

  4. #4
    Join Date
    May 2006
    Posts
    25

    Default

    What I always do is have the property be a comma separated string. Then my bean setter method takes a string, but the getter returns a List<String>

    A-like so:

    public List<String> getSeedEmails() {
    return seedEmails;
    }


    public void setSeedEmails(String seedEmails) {
    String[] emails = seedEmails.split(",");
    this.seedEmails = Arrays.asList(emails);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •