I\'m learning the Spring Boot framework and I want to understand how the @Autowired
annotation works. I know that in Spring Boot we have a context, and inside that
When creating beans Spring will have to invoke constructors that the target bean class contains :
@Autowired
).@Autowired
or use configuration class to define your beans).Spring IOC container (application context) is responsible for holding beans and return them whenever it is asked to do so. To create a context you have to tell Spring where to look for bean definitions : you can provide xml file, java configuration or enable auto-scanning of components in given packages. When Spring context is being created it has to create beans. It will try to invoke constructors and provide any dependencies for beans that require them.
In your example when instance of MyClass
will be created for the context, it will invoke default constructor of MyClass
class and then set it's dependency via reflection.
However field injection is typically a bad idea as you might have problems with testing such components. Constructor or setter injection is a better choice.
If you changed your MyClass
to :
public class MyClass {
private MyService service;
@Autowired
public MyClass(MyService service) {
this.service = service;
}
}
here you provide your own constructor - note that there will be no default constructor generated in this case. So Spring will have to invoke constructor you provided and satisfy dependency for it. If there is no dependency that can be injected - an exception will be thrown.
Notice that you can use your classes even without Spring :
MyService myService = new MyService();
MyClass myclass = new MyClass(myService);
By marking your classes with Spring stereotypes and by using @Autowired
you just enable spring support for context creation and dependency injection (in case of automated package scanning)