Does anyone know of a Maven plugin that can be used to validate Spring configuration files? By validation, I mean:
- Verify all beans reference a class on the build path
- Verify all bean references refer to a valid bean definition
- Verify no orphaned beans exist
- Other configuration mistakes I’m sure I’m missing.
I searched around and didn’t come up with anything.
A Maven plugin would be ideal for my purposes, but any other tools (Eclipse plugin, etc.) would be appreciated.
What we do on our project is simply write a JUnit test which loads the Spring configuration. This does a few of the things you described like:
It does not check that there are no orphan beans. There is no reliable way of doing this anyway considering from anywhere in your code, you can lookup beans directly given their ID. Just because a bean is not referenced by any other beans does not mean it is not used. In fact all Spring configs will have at least one bean which is not referenced by other beans because there always has to be a root to the hierarchy.
If you have beans which rely on real services like databases or something and you don’t want to connect to these services in a JUnit test, you simply need to abstract the configuration to allow for test values. This can be easily accomplished with something like the PropertyPlaceholderConfigurer which allows you to have different properties specified in separate config files for each environment and then referenced by one beans definition file.
EDIT (to include sample code):
The way we do this is have at least 3 different spring files…
applicationContext.xml
beanDefinitions.xml
testContext.xml
There are many things going on here, let me explain…
Now that we have a test context which we aren’t afraid to load from a test, here is the code to do it…
SpringContextTest.java
package com.example;
This may not be the optimal way of doing it; the ApplicationContext class is the recommended way of loading spring contexts. The above might be able to be replaced by:
I believe that one line will accomplish everything you need to verify your spring context is wired correctly. From there, you can load beans and assert like before.
Hope this helps!