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()?


Reply With Quote
