Results 1 to 4 of 4

Thread: is there any bug in PropertyPlaceholderConfigurer?

  1. #1
    Join Date
    Jan 2012
    Location
    Switzerland
    Posts
    2

    Question 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2006
    Posts
    129

    Default

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

  4. #4
    Join Date
    Jan 2012
    Location
    Switzerland
    Posts
    2

    Thumbs up

    thank you all for useful information.

Tags for this Thread

Posting Permissions

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