Announcement Announcement Module
Collapse
No announcement yet.
Property Substitution Across Multiple Spring Config Files in Webapp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Property Substitution Across Multiple Spring Config Files in Webapp

    I have a config.xml which contains a PropertyPlaceholderConfigurer bean declaration and specifies a placeholderPrefix of "$test{". It is located in WEB-INF/.

    I have a config2.xml which does not contain a PropertyPlaceholderConfigurer bean declaration. It is located in WEB-INF/classes/.

    If in config2.xml I specify a value to a property for one of my beans to be $test{myProp} where "myProp" is the key from the property file read in by the PropertyPlaceholderConfigurer in the first config.xml, should config2.xml be able resolve that property and the value be substituted correctly?

    The config files are specified in the web.xml with config.xml coming before config2.xml in the contextConfigLocation param in the web.xml.

    The property values with the prefix "$test{" in config2.xml are not being subsituted in the beans, instead the values are still $test{myProp} in the beans.

    If the values are supposed to be substituted, what am I doing wrong? If they aren't, how do you get Spring to subsitute the values?

    Thanks!

  • #2
    PropertyPlaceholderConfigurer works for the entire ApplicationContext. So if both xml files are loaded by the same ContextLoaderListener / ContextLoaderServlet, the ${name} in both files will be processed regardless of the files order in the config.
    Could you post relevant parts of your configuration?

    Comment


    • #3
      I ended up creating a simple webapp which contained 3 config files. Two of them I wanted to be able to share properties from the same properties file and the third was separate altogether.

      config1.xml -
      <beans>
      <bean id="properties" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
      <property name="location">
      <value>classpath:test/config/test1.properties</value>
      </property>
      </bean>
      <bean id="testBean1" class="test.TestBean1">
      <property name="password" value="${password}" />
      </bean>
      </beans>

      config2.xml
      <beans>
      <bean id="properties" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
      <property name="location">
      <value>classpath:test/config/passwordtest/test2.properties</value>
      </property>
      <property name="placeholderPrefix"><value>$test{</value></property>
      </bean>
      <bean id="testBean2"
      class="test.TestBean2">
      <property name="password2">
      <value>$test{password2}</value>
      </property>
      </bean>
      </beans>

      config3.xml
      <beans>
      <!-- PTW Source Selection Service -->
      <bean id="testBean3"
      class="test.TestBean3">
      <property name="password3">
      <value>$test{password3}</value>
      </property>
      </bean>
      </beans>

      In these files, I print out the value of the password variables in each of the three test beans. testBean1 from the first config file gets the value of the token itself, ${password}, as the actual value that is set on the bean(which seems incorrect). testBean2 and testBean3 get the actual values from the properties files and not the token.

      Is this the correct behaviour? I would think that testBean1 would get it's property replaced as well instead of it not getting replaced at all. Basically, I want to be able to set different placeholderPrefixes for different properties files because some properties files may have the same property name and I need to be sure to get the right property value from the right property file. Is there a way to do this?

      Comment


      • #4
        I figured out my error, the name of the property placeholder bean was the same in both config1.xml and config2.xml, when I change the bean name it all works as expected.

        Comment

        Working...
        X