Just to keep my sanity, I’m telling Spring to barf on circular references:
_context = new ClassPathXmlApplicationContext(getApplicationContextFiles(), false);
_context.setAllowCircularReferences(false);
_context.refresh();
_context.start();
The problem is that the resulting stack trace actually does look like barf 😐
How can I get a simple output of a circular references, similar to:
Unresolved circular reference: [aBean->nextBean->otherBean->aBean]
?
If all your cycles are between registered components this may help (as far as I can recall you can’t reference inner beans easily as they are not registered).
You can register a custom listener on events generated by the bean parsers, to do so extend the application context and override initBeanDefinitionReader(), in that method set your custom listener on the beanDefinitionReader using setEventListener (you’ll need to cast to XmlBeanDefinitionReader).
The listener will be notified when ComponentDefinitions are registered and store them in a Map.
Once processing of the configuration is complete, you can then process the ComponentDefinitions to identify cycles using something similar to JDepend‘s jdepend.framework.JavaPackage..
For each ComponentDefinition, iterate the getBeanDefinitions() and getBeanReferences() (and maybe getInnerBeanDefinitions()) and add create a “JavaPackage” for each definition and a dependency for each reference.
After processing each reference and declaration, you can then interrogate the JavaPackage-like objects and spit out the results of any that return true for containsCycle()