How do constructor calls with @Autowired work?

后端 未结 3 1546
北恋
北恋 2021-02-05 15:52

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

相关标签:
3条回答
  • 2021-02-05 15:57

    Since MyService has a @Service annotation, Spring will instantiate it automatically (i.e. register it as a bean).

    While creating it, it scans the constructors of the class and sees that a no-args constructor is defined, which is the only constructor so it will use it.

    Since it creates the service by calling your no-args constructor it found, your code at the place of "//do something" will be executed.

    On the other side, if you wouldn't have added this constructor, the MyService class would implicitly have an empty non-args constructor defined, so Spring would take the empty constructor. But in your case, you have defined an explicit constructor (which overrides the implicit empty no-args constructors) so Spring sees it as the only constructor available and there are no ambiguities.

    Long story short -> if you have one constructor defined, Spring will always use it automatically to create the bean.

    P.S: You can also have a constructor with parameters if you use the @Autowired annotation. On this case, Spring will call this constructor to create the bean and pass the required parameters if there are such beans declared that can be autowired into the constructor.

    0 讨论(0)
  • 2021-02-05 16:03

    Long story short

    Spring will use the default constructor (the no-args constructor) to construct the components. In your example

    @Service
    public class MyService {
    
    public MyService() {
       // do something
    }
    }
    

    the constructor will be called and the // do something will be called.

    However, keep in mind that if you added a non-default constructor to the class(a constructor with arguments) then the compiler will not generate a default constructor for you and in this case, spring will fail to instantiate your MyService

    For example

      @Service
        public class MyService {
    
        public MyService(String var) {
           // do something
        }
        }
    

    will throw an exception Parameter 0 of constructor in com.example.MyService required a bean of type 'java.lang.String' that could not be found.

    0 讨论(0)
  • 2021-02-05 16:15

    TL;DR

    When creating beans Spring will have to invoke constructors that the target bean class contains :

    1. If there is no constructor defined - then Spring will invoke the implicit default constructor generated by compiler.
    2. If there is a no-args constructor defined explicitly then Spring invoke this one since there is no other constructor defined.
    3. 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).
    4. 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)

    0 讨论(0)
提交回复
热议问题