Announcement Announcement Module
Collapse
No announcement yet.
PropertyPlaceholderConfigurer with Lists? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyPlaceholderConfigurer with Lists?

    The API indicates that the PropertyPlaceholderConfigurer works with lists. Is there an example of this?

    I'm already using the PropertyPlaceholderConfigurer to pull in database connection info. Works great, when need to do something like this in my bean definition:

    Code:
    <property name="user"><value>$&#123;jdbc.user&#125;</value></property>
    <property name="url"><value>$&#123;jdbc.url&#125;</value></property>
    But what I now need to do is take a set of name/value pairs from a property file and insert those name/value pairs into a HashMap in my storage object.

    So my properties file will look something like:

    Code:
    java.sql.type.1="text.vm"
    java.sql.type.2= "textarea.vm"
    java.sql.type.3="select-list.vm"
    And my object that contains that looks like this:

    Code:
    public class Mapper &#123;
        
    // instance variables
    private HashMap Map;
    
    // accessors go here
    ...
    &#125;
    The data from the property file needs to go into the HashMap as key/value pairs.

    I can structure the accessor methods to this storage object any way I want (with setters/getters, add/puts that expose the underlying HashMap, etc.) but I'm not sure what the syntax for the applicationContext.xml file would be to get a list read out of the property file and into a HashMap fronted by my storage object.

    Is that possible?

    Thanks!

    - Gary

  • #2
    Just checked the Spring source code and found that PropertyPlaceholderConfigurer (and superclasses) use a java.util.Properties object for grabbing data out of the properties file and storing it temporarily while the PlaceholderConfigurer does the job of parsing the bean-defs and doing the parsing swaps.

    If I'm reading it right, the parsing/swapping code assumes a 1-for-1 mapping between a key/value pair in the original properities file and a placeholder such as ${propname} in the bean-def file.

    That would mean that I can't use PropertyPlaceholderConfigurer to take a n-number of properties out of a properties file and stuff them into a single HashMap (or a custom object backed by a HashMap). I can only take a n-number of properties and store each discreet property value into a corresponding single-valued placeholder.

    In other words, I can'd do this:

    Code:
    <property name="theMap"><value>$&#123;*&#125;</value></property>
    Where * would be a wildcard indicating "all properties" and the parsing code would then "expand" that * into my Map (using put or whatever).

    Damn!

    So, uh, is there anything else in Spring that can help here?

    I can always go roll my own Properties reader/translator (or patch Spring to implement the wildcard * in the above if people feel it would be of value), but I'd prefer not to re-invent something that's already there.

    Thanks!

    - Gary

    Comment


    • #3
      PropertiesFactoryBean creates a Properties object from a properties file.
      The Properties class implements Map.
      Would this give you what you want?

      Comment


      • #4
        While not as clean as defining separate properties and using Collection methods, you can set a property equal to a comma separated list of values, then have that automatically convert to a String array on a setter.

        So in your properties file:
        Code:
        java.sql.types=text.vm,textarea.vm,select-list.vm
        Then:
        Code:
        <property name="types"><value>$&#123;java.sql.types&#125;</value></property>
        Where you have defined:
        Code:
        public void setTypes&#40;String&#91;&#93; types&#41;

        Tim

        Comment


        • #5
          Originally posted by cmgharris
          PropertiesFactoryBean creates a Properties object from a properties file.
          The Properties class implements Map.
          Would this give you what you want?
          Unless I'm missing something, I don't think that helps.

          The problem isn't really about the capabilities of the underlying storage object used for the property values (in the case a Properities object) but more about what kind of mappings one can express with the "placeholder" syntax (the ${propertyname} thing).

          The placeholder syntax assumes a 1-for-1 mapping between properties extracted from a properties file or explicitely defined in the bean definition xml and the corresponding properties on an object.

          What I'm looking for is a way to roll-up a group of properties in a properties file and map those to individual slots within a HashMap.

          But thanks for the suggestion. :-)

          - Gary

          Comment


          • #6
            Originally posted by timmorrow
            While not as clean as defining separate properties and using Collection methods, you can set a property equal to a comma separated list of values, then have that automatically convert to a String array on a setter.
            My list of of name/value pairs is a bit large to go that route but I appreciate the suggestion.

            I think the thing to do is to just have my object directly read and extract the name/value pairs from the properties file.

            It's probably opening a big can of worms to start extending the Spring PropertyPlaceholder stuff (with something like wildcard notation in the placeholders) to handle something other than 1-to-1 mappings between object properties and property-file properties.

            At least more worms than I'm willing (or maybe even qualifed) to deal with by implementing it myself.

            - Gary

            Comment

            Working...
            X