TL;DR
When creating beans Spring will have to invoke constructors that the target bean class contains :
- If there is no constructor defined - then Spring will invoke the implicit default constructor generated by compiler.
- If there is a no-args constructor defined explicitly then Spring invoke this one since there is no other constructor defined.
- If there is a constructor defined, which requires some dependencies then Spring will have to invoke this one and provide dependencies for it. (Since Spring 4.3 you do not even have to mark this constructor with
@Autowired
).
- If there are multiple args constructor defined then you will have to resolve ambiguity, since Spring will not know which one to choose. (Then you can mark one of them with
@Autowired
or use configuration class to define your beans).
Side notes
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)