Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Using PropertyOverrideConfigurer to override value of Map property embedded in a bean

  1. #1
    Join Date
    Jan 2007
    Location
    California
    Posts
    31

    Default Using PropertyOverrideConfigurer to override value of Map property embedded in a bean

    Hi,

    I am trying to use PropertyOverrideConfigurer to override a couple of values in a java.util.Properties object that is embedded inside of a bean defenition in the application context file.

    So say for example that I want to override the "file.path.input" property of the filePathProperties property of the bean someBeaName:

    Code:
      <bean id="someBeaName" class="com.package.SomeBeaName" >
        <property name="filePathProperties">
          <map>
            <entry key="file.path.input" value="file/path/input.xml"/>
            <entry key="file.path.output" value="file/path/output.xml"/>
            <entry key="file.path.script" value="file/path/script.ksh"/>
          </map>
        </property>
      </bean>

    Then how would i specify that in the properties file that I am going to load into the PropertyOverrideConfigurer?

    Code:
    someBeaName.filePathProperties.????=alternative/file/path/input.xml
    what would go in place of the ???? above?
    Any help is very much appreciated.

  2. #2
    Join Date
    Feb 2005
    Posts
    217

    Default

    Code:
     
     <bean id="someBeaName" class="com.package.SomeBeaName" >
        <property name="filePathProperties">
          <map>
            <entry key="file.path.input" value="${file.path.input}"/>
            <entry key="file.path.output" value="file/path/output.xml"/>
            <entry key="file.path.script" value="file/path/script.ksh"/>
          </map>
        </property>
      </bean>
    And put this in a properties file that you load up:

    Code:
    file.path.input=alternative/file/path/input.xml

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

    Default

    I take it what you mean is you want to replace the whole Properties object rather than just one of the elements.

  4. #4
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    I think the question was how to override (not replace) a single property. What about

    Code:
    someBeaName.filePathProperties[file.path.input]=alternative/file/path/input.xml
    I'm guessing it works based on what I know about BeanWrapperImpl but haven't tried it.

  5. #5
    Join Date
    Jan 2007
    Location
    California
    Posts
    31

    Default

    cwilkes,
    If I try you suggestion then that would mean that I would have to use a PropertyPlaceholderConfigurer in my app context file. And that will probabley not work in my situation because Spring gives me an error when I use PropertyPlaceholderConfigurer twice in the ClassPathXmlApplicationContext that I am loading.

    The problem that I am having is that i have two context files, one that is common to all of my JUnits (contains database connetion and transaction bean defenitions), and I have another context file that is specific to each particular JUnit.

    In my JUnit's setUp() method I load both context files using the ClassPathXmlApplicationContext(String[] configLocation) constructor, where I pass in both context files as an array. But the problem is that I am already using a PropertyPlaceholderConfigurer in my first context file (which is common to all of my JUnits), and then when I try to use it again in my second context file (for my specific JUnit), then Spring gives me an error saying something like:

    [/context-file-name.xml]: Could not resolve placeholder 'folder.path.input'
    I came across this problem some while ago, and it really annoys me the fact that Spring does not allow us to load more than one PropertyPlaceholderConfigurer bean defenition in our ApplicationContext.

    david_syer, your suggestion might work, though i have not tried it yet.

    Thank you all for your help and suggestions.

  6. #6
    Join Date
    Jan 2007
    Location
    California
    Posts
    31

    Default

    david_syer,

    I tried your suggestion, but I end up getting an error that says something like the following:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'someBeaName' defined in class path resource [/context-file-name.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'filePathProperties[file.path.input]' of bean class [com.package.SomeBeaName]: Cannot access indexed value in property referenced in indexed property path 'filePathProperties[file.path.input]'; nested exception is org.springframework.beans.NotReadablePropertyExcep tion: Invalid property 'filePathProperties[file.path.input]' of bean class [com.package.SomeBeaName]: Bean property 'filePathProperties[file.path.input]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    ...
    [REMAINER OF STACK TRACE ERROR HAS BEEN OMITTED FOR BREVITY]

    Thanks for the suggestion though. I really appreciate your help.

  7. #7
    Join Date
    Jan 2007
    Location
    California
    Posts
    31

    Default

    david_syer,

    Actually your suggestion does work. The problem was that I only had a setter method

    Code:
    public void setFilePathProperties(Properties filePathProperties)
    {
    	this.filePathProperties = filePathProperties;
    }
    defined in my bean class. After I add a corresponding getter method

    Code:
    public Properties getFilePathProperties()
    {
    	return filePathProperties;
    }
    the error went away, and it worked. Thanks.

    Though on a separate note, I wish Spring would fix their framework to allow us to defined more than one PropertyOverrideConfigurer for the same ApplicationContext object.

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

    Default

    Quote Originally Posted by al_zawia View Post
    Though on a separate note, I wish Spring would fix their framework to allow us to defined more than one PropertyOverrideConfigurer for the same ApplicationContext object.
    You can. You just need to ensure they are configured correctly e.g. setting the order and ignoreUnresolvablePlaceholders as required.

  9. #9
    Join Date
    Jan 2007
    Location
    California
    Posts
    31

    Default

    Quote Originally Posted by karldmoore View Post
    You can. You just need to ensure they are configured correctly e.g. setting the order and ignoreUnresolvablePlaceholders as required.
    You are correct. I was able to get it to work by setting the ignoreUnresolvablePlaceholders property to true on the PropertyPlaceholderConfigurer. Thanks for pointing that out to me

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

    Default

    Quote Originally Posted by al_zawia View Post
    You are correct. I was able to get it to work by setting the ignoreUnresolvablePlaceholders property to true on the PropertyPlaceholderConfigurer. Thanks for pointing that out to me
    Not a problem, it's quite a useful trick!

Posting Permissions

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