3

On Spring @Autowired usage question most of the people answer they prefer not using configuration files, if possible. It seems like a good answer at the first glance.

However, once you have quite a big application which uses Spring IoC and autowires all the stuff using annotations @Autowired, @Service, etc. you must hit this problem: you no longer are able to keep track of your bean dependencies.
How do you deal with that?

Using SpringSource Tool Suite one can create graphs of dependencies on the basis of configuration files.
Is there any tool out there which does the same with @Autowired stuff? (I understand graphs would have to be created on runtime).

2
  • I thought they were adding support for graphing annotated dependencies into STS... maybe they haven't done that yet.
    – Mark Pope
    May 1, 2011 at 19:09
  • While not a direct answer I think it deserves some eyes. I have found that auto wired is best for smaller projects and xml configurations are best for larger projects. At one point I read a few blogs/articles that agreed and mainly due to the very issue you are seeing. Jan 25, 2016 at 16:37

1 Answer 1

2

Implement BeanFactoryAware and cast the factory that is passed into setBeanFactory() to a DefaultListableBeanFactory. Then use DefaultListableBeanFactory.getBeanDefinitionNames() and DefaultListableBeanFactory.getDependenciesForBean() to generate the dependency graph.

6
  • That could be a good idea but to implement this I'd have to change my bean (or: all of my beans). I don't feel like doing that...
    – mjaskowski
    May 3, 2011 at 11:00
  • Not all your beans, you would just have one bean that generates the dependency graph. May 5, 2011 at 0:44
  • 2
    Inspired by your answer, I create an ApplicationContextDumper. It works well in our project. Please read: good-good-study.appspot.com/blog/posts/157001
    – aleung
    Nov 8, 2011 at 10:51
  • @aleung - I wanted to use your tool but you've not included any license notice; gists are not open source by default. Could you release it under an OSS license of your choice? (well GPL or areo won't be useful for me) Apr 29, 2014 at 19:20
  • The above link was broken, please visit the gist: gist.github.com/aleung/1347171. It's licensed under WTFPL v2. @JakubBochenski
    – aleung
    Apr 30, 2014 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.