Search code examples
springeclipsedependency-injectionautowiredspring-tool-suite

Debugging with Java/Spring's @Autowired on Eclipse


I have picked up a project that uses @Autowired and dependency injection a lot. People have been saying all the nice things about them. It seems kind of like a magic.

I am new to Spring injection and @Autowired. My first glance of @Autowired is good, but when I work with them on Eclipse, I have difficulties tracing them around when debugging.

For example, is there an easy way in Eclipse that I can see where and how an @Autowired bean is initiated? How do I know if it is initiated correctly the way I want? Is there a way to trace the sequence of it being called/accessed?

Anybody has any tips or comments that may help me to debug @Autowired bean easier on Eclipse?


Solution

  • When a Spring application starts, it first creates instances of beans, either by scanning for annotations, or processing the XML Spring context definition. Some of those beans have to be created in a specific order, like if they use constructor injection. Once beans are created, wiring those beans together can happen.

    If you want to see what happens when a bean is constructed, set a breakpoint in the constructors of the class. Look down the stacktrace to see what caused it to be constructed.