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).

Community
  • 1
  • 1
mjaskowski
  • 1,479
  • 1
  • 12
  • 16
  • I thought they were adding support for graphing annotated dependencies into STS... maybe they haven't done that yet. – Mark Pope May 01 '11 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. – Andrew White Jan 25 '16 at 16:37

1 Answers1

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.

sourcedelica
  • 23,940
  • 7
  • 66
  • 74
  • 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 03 '11 at 11:00
  • Not all your beans, you would just have one bean that generates the dependency graph. – sourcedelica May 05 '11 at 00:44
  • 2
    Inspired by your answer, I create an ApplicationContextDumper. It works well in our project. Please read: http://good-good-study.appspot.com/blog/posts/157001 – aleung Nov 08 '11 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) – Jakub Bochenski Apr 29 '14 at 19:20
  • The above link was broken, please visit the gist: https://gist.github.com/aleung/1347171. It's licensed under WTFPL v2. @JakubBochenski – aleung Apr 30 '14 at 05:42
  • @aleung thanks, I actually came here through your github.io blog (did you add the license notice or was I blind at the time?) BTW you know you could edit your comment to update the link? – Jakub Bochenski Apr 30 '14 at 10:35