Announcement Announcement Module
Collapse
No announcement yet.
is there any bug in PropertyPlaceholderConfigurer? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • is there any bug in PropertyPlaceholderConfigurer?

    There is an issue related to PropertyPlaceholderConfigurerp.processProperties() method, if I work with more than one context.xml like this:

    BeanFactory BEAN_FACTORY = new ClassPathXmlApplicationContext(new String[] { "context_1.xml", "context_2.xml" });

    and each context.xml has a declaration for PropertyPlaceholderConfigurer:

    in context_1.xml:

    <bean id="stageDependantConfigFetcher1" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:examples1.properties</value>
    </list>
    </property>
    </bean>

    and in context_2.xml:

    <bean id="stageDependantConfigFetcher2" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:examples2.properties</value>
    </list>
    </property>
    </bean>

    In this case I get a problem during invocation of method "PropertyPlaceholderConfigurerp.processProperties( )" that the placeholder which were defined in examples2.properties could not be found!

    After changing of declaration (see below):

    in in context_1.xml:

    <bean id="stageDependantConfigFetcher1" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:examples1.properties</value>
    <value>classpath:examples2.properties</value>
    </list>
    </property>
    </bean>

    it runs correctly.

    I think, it's not a good way to repeat the declaration in each context.xml

    Therefore I created a sub class (extendsPropertyPlaceholderConfigurer):

    public class GlobalPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private static List<Resource> resources = new ArrayList<Resource>();

    @Override
    public void setLocations(Resource[] locations) {
    for (int i = 0; i < locations.length; i++) {
    resources.add(locations[i]);
    }
    }

    @Override
    protected void loadProperties(Properties props) throws IOException {
    Resource[] locations = new Resource[resources.size()];
    int j = 0;
    for (Resource res : resources) {
    locations[j] = res;
    j++;
    }
    super.setLocations(locations);
    super.loadProperties(props);
    }
    }

    and replaced PropertyPlaceholderConfigurer in each context.xml by GlobalPropertyPlaceholderConfigurer. In this case I don't need to repeat the declaration of propertie files in context.xml.

    My questions are:
    - is there a bug in PropertyPlaceholderConfigurer?
    - is it a good way to override the methods PropertiesLoaderSupport.setLocations() and PropertiesLoaderSupport.loadProperties()?

  • #2
    Please use [ code][/code ] tags when posting code that way it remains readable.

    Regarding your questions no there isn't a bug in the PropertyPlaceHolderConfigurer that is default behavior, if it finds more placeholders then it can replace it throws an error (by default but this is configurable, you might want to read the documentation of the PropertyPlaceHolderConfigurer)... Regarding the overriding, seems a little useless to me if you know how the PPHC works.

    Comment


    • #3
      Code:
      <context:property-placeholder ignore-unresolvable="true"/>
      allows multiple PPHC

      Comment


      • #4
        thank you all for useful information.

        Comment

        Working...
        X