Results 1 to 9 of 9

Thread: Does PropertyPlaceholderConfigurer work with 1.5 generic collections?

  1. #1

    Default Does PropertyPlaceholderConfigurer work with 1.5 generic collections?

    I'm not able to get a comma delimited property value populated into a data member that is List<String> but if I change it to String[], it works.

    Is the PropertyPlaceholderConfigurer not able to populate a list property that is a java 1.5 generic collection? yet?

  2. #2
    Join Date
    Dec 2006
    Location
    Normal, Illinois
    Posts
    277

    Default

    Can you post the Spring configuration and properties file that is causing you a problem?
    Caleb Washburn

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'm surprised this isn't working, a stacktrace would also be useful!

  4. #4

    Default

    Thanks for the quick responses!

    To test this out, I've created a small class that has a String array and a List of Strings. I'm using the same property to set the values for both. I'm not even using a properties file, just setting it in the "properties" map of the PropertyPlaceholderConfigurer (same outcome if I do use a properties file though). As you can see from the output below, the String array is populated correctly while the String List contains one entry which is the comma delimited list.

    By the way, I'm using Spring 2.0.1


    Here's the configuration xml:
    Code:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="properties">
    		<map>
    			<entry key="strings" value="abc,def,ghi"/>
    
    		</map>
    	</property>
    </bean>
    
    <bean id="test" class="com.pics.ATest" init-method="init">
    	<property name="stringArray">
    		<value>${strings}</value>
    	</property>
    	<property name="stringList">
    		<value>${strings}</value>
    	</property>
    </bean>


    And the class:
    Code:
    public class ATest {
    
    	private String[] stringArray;
    	private List<String> stringList;
    
    	public void init() {
    		System.out.println("stringArray values: ");
    		for (String str : stringArray) {
    			System.out.println("  " + str);
    		}
    
    		System.out.println("stringList values: ");
    		for (String str : stringList) {
    			System.out.println("  " + str);
    		}
    	}
    
    	public void setStringArray(String[] stringArray) {
    		this.stringArray = stringArray;
    	}
    	
    	public void setStringList(List<String> stringList) {
    		this.stringList = stringList;
    	}
    	
    	public static void main(String[] args) {
    		ConfigurableApplicationContext context = new FileSystemXmlApplicationContext("classpath:application-context.xml");
    		ATest test = (ATest)context.getBean("test");
    		
    	}
    }


    And the output:

    Code:
    stringArray values: 
      abc
      def
      ghi
    stringList values: 
      abc,def,ghi

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I would guess there isn't a property editor to support this. There is already a StringArrayPropertyEditor which is why the String[] works. The value is simply treated as a complete String however for the List<String>. You should be able to write a property editor to support the required behaviour, have a look at the source for StringArrayPropertyEditor.

    http://www.springframework.org/docs/...ans-conversion
    http://www.springframework.org/docs/...rtyEditor.html

    Strongly typed collections do work however when specified explicity in the bean definition (3.3.3.4.2. Strongly-typed collection (Java5+ only)).
    http://www.springframework.org/docs/...ction-elements

  6. #6

    Default

    That makes sense. I think you're right, there just isn't a builtin property editor for it. A custom property editor would probably do the trick.
    Thanks!

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Glad it helped! If you want to post the code for the property editor you write, it might be useful for other people.

  8. #8
    Join Date
    Oct 2005
    Posts
    3

    Default Custom PropertyEditor works, but not sure how to register

    Here is a quick mod to the StringArrayPropertyEditor to work with a List of Strings. This does work; however, I am not sure how to register the editor. I tried using org.springframework.beans.factory.config.CustomEdi torConfigurer; however, this requires registration by classname and I don't want to register this custom editor for all Lists in my application context. I ended up just switching my List<String> to String[] in my class. Any advice on how to properly register the editor would be appreciated.

    Code:
    package com.bglobal.cip.atx.util;
    
    import java.beans.PropertyEditorSupport;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.springframework.util.StringUtils;
    
    public class StringListPropertyEditor extends PropertyEditorSupport {
    
        /**
         * Default separator for splitting a String: a comma (",")
         */
        public static final String DEFAULT_SEPARATOR = ",";
    
        private final String separator;
    
    
        /**
         * Creates a new instance of the {@link StringLinkPropertyEditor} with the
         * {@link #DEFAULT_SEPARATOR}.
         */
        public StringListPropertyEditor() {
            this.separator = DEFAULT_SEPARATOR;
        }
    
        /**
         * Creates a new instance of the {@link StringListPropertyEditor} with
         * the given separator.
         * @param separator the separator to use for splitting a {@link String}
         */
        public StringListPropertyEditor(String separator) {
            this.separator = separator;
        }
    
    
        public void setAsText(String text) throws IllegalArgumentException {
            List<String> list = Arrays.asList(StringUtils.delimitedListToStringArray(text, this.separator));
            setValue(list);
        }
    
        public String getAsText() {
            List<String> list = (ArrayList<String>) this.getValue();
            return StringUtils.arrayToDelimitedString(list.toArray(new String[] {}), this.separator);
        }
    }

  9. #9
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Spring already has a StringArrayPropertyEditor. The reference manual does explain how to register your own.
    http://www.springframework.org/docs/...ans-conversion

Posting Permissions

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