How to detect unused properties in Spring

I am working on a Spring 2.0 project without annotations. We use several PropertyPlaceholderConfigurer beans with different pre and suffixes to load properties from different property files. It works great.

Due to the large number of properties and properties files, I wanted the application to display properties that were not used . This means that properties that are configured in the properties file, but are never mentioned in the context of the Spring application.

I wrote a bean that implements BeanFactoryPostProcessor, and did some tricks to find links in the application context for different PropertyPlaceHolderConfigurers. This gives me a list of properties that are being used.

However, I cannot go over to the properties that were loaded by the PlaceHolderConfigurers platform. Because of this, I cannot show properties that are NOT used.

Is there a (simple) way to get PropertyPlaceholderConfigurer properties? Any other suggestions to solve this?

Change The solution got access to the mergeProperties method, for example:

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 initially loaded properties and getting beandefinitions during BeanFactoryPostProcessing, the rest was simple. Subtract the two collections and voila: now we can list the unused properties.

+6
java spring
source share
3 answers

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

Probably too dangerous for production code, but I assume that you will only run this in the unit test setup to generate this report.

+3
source share

How to create your own subclass PropertyPlaceholderConfigurer, which will save a link to its Properties object and provide an accessory. Then your BeanFactoryPostProcessor will be able to access each source Properties object and, in combination with the list of properties that are used, you can define properties that have not been used.

+1
source share

Could you just iterate over the list of used properties and remove them from the duplicated set of all properties? This would leave unused.

0
source share

All Articles