tags:

views:

220

answers:

3

I'm working on a Spring 2.0 project, without annotations. We're using several PropertyPlaceholderConfigurer beans with different pre- and suffixes to load properties from different property files. This works beautifully.

Because of the large number of property files and properties, I wanted the application to list the properties which are not used. That means, properties which are configured in a property file, but never referenced in the Spring application context.

I wrote a bean which implements BeanFactoryPostProcessor, and did some trickery to find references in the application context to the different PropertyPlaceHolderConfigurers. This gives me a list of properties which are used.

However, I can not get to the properties which were loaded by the PlaceHolderConfigurers. Because of that, I can not show the properties which are NOT used.

Is there a (simple) way to get to the properties of a PropertyPlaceholderConfigurer? Any other suggestions on how to solve this problem?

Edit: The solution was accessing the mergeProperties metod, like so:

PropertyPlaceholderConfigurer ppc = 
    (PropertyPlaceholderConfigurer) applicationContext.getBean("yourBeanId");
Method m = PropertiesLoaderSupport.class.getDeclaredMethod("mergeProperties", 
            new Class[] {});
m.setAccessible(true);
Properties loadedProperties = (Properties) m.invoke(propertyPlaceHolder, null);

After getting the originally loaded properties, and fetching the beandefinitions during the BeanFactoryPostProcessing, the rest was simple. Subtract the two collections, and voila: We can now list the unused properties.

+1  A: 

How about creating your own subclass of PropertyPlaceholderConfigurer that would keep a reference to the its Properties object and provide an accessor. Your BeanFactoryPostProcessor would then be able to access each original Properties objects and combined with the list of properties which are used you could figure out the properties that were not used.

Mark
That would require all PropertyPlaceHolderConfigurers to be editted, which is possible, but I don't want to do. By using the solution above, I can also detect missing properties in new PropertyPlaceHolderConfigurers which are added by "the new guy" :-)
Rolf
A: 

Couldn't you just iterate over the list of used properties and remove them from a duplicate set of all properties? That would leave the unused ones behind.

duffymo
There is no getter for the used properties, and no getter for all properties.
Rolf
+3  A: 

You might try calling the protected method mergeProperties using reflection to get a hold of the complete list of properties, and then, as other posters have already said, remove all properties that are actually used to end up with the set of unused properties.

Probably a bit too hackish for production code, but I am assuming you would be running this only in a unit test setting to generate this report.

Joris