Spring-depth analysis of the source code in the constructor injection

@Autowired annotation determineConstructorsFromBeanPostProcessors dependency injection method to select whether automatic injector with a suitable builder, if not, using the constructor with no arguments will instantiate ** @Autowired without annotation: ** No argument constructor directly added defaultConstructor collection. When the number of the constructor and there is only one parameter, this parameter has the only added candidateConstructors constructor set. There are two in number when the constructor, and there is no argument constructor, the defaultConstructor (first constructor no arguments) into candidateConstructors collection. Constructor number greater than two, and the presence of non-argument constructor case, candidateConstructors returns an empty set, i.e. not of the constructor. ** In the case where there @Autowired annotation: Analyzing ** required attribute: true: determining whether requiredConstructor first set is empty, if not empty prior to have a required represents constructor = true, the two true to throw abnormal, and then determine whether candidates set is empty, if there is already a hit annotated constructor is empty before said that at this time required is true, an exception is thrown. If neither an empty set will be placed requiredConstructor then placed candidates set. false: candidates directly into the collection. Determining whether requiredConstructor set is empty (the presence or absence of builder required = true), if not, the default constructor is also set into candidates. Finally, the above-mentioned candidates assigned to a collection candidateConstructors final return. # 3. ** 1 summarizes why write three constructors (containing no argument constructor), and there is no @Autowired annotation, Spring always use the no-argument constructor to instantiate Bean? ** A: No annotation reference approach: If the constructor only two, and there is no argument constructor, the constructor with no arguments directly initialized. If more than two constructors, returns an empty set, i.e. not find a suitable builder, then the reference to the third end of the initialization method createBeanInstance first code of the Bean, without the use of reference constructor will instantiate. This also answers why there is no annotation, Spring will always use the no-argument constructor instantiates Bean, and at this time if there is no no-argument constructor throws an exception, Bean instantiation fails. ** 2, why comment out two constructors, leaving a constructor parameter, and no @Autowired annotation, Spring will use constructor injection to initialize the way Bean Bean? ** A: no reference annotation approach: only a constructor parameter, and when, will put out this constructor returns as applicable constructor, this constructor instantiates, parameters will naturally get from the IOC Bean It is injected. ** 3, why write three constructors, and on one of the constructor marked @Autowired annotations, you can normally inject constructor? ** A: annotated reference approach: the final candidates determined whether the set of applicable constructor is empty, if annotation, of course, this set is not empty, and the required = true, nor will set the default constructor defaultConstructor Join candidates in the collection, the final return of the data collection of candidates, which is the only one that hit annotated constructor, so in the end this fight annotated constructor instantiated. ** 4, two @Autowired comment'll get an error in necessarily required in all @Autowired are adding false to initialize properly? ** A: annotated reference approach: When played two @Autowired notes, that is, two required are true, will throw an exception, if a is true, one is false, it will also throw an exception, regardless of the order, judgment because there are two layers, is a collection of requiredConstructor judgment is empty, is a collection of candidates for the empty judgment, required if the attribute constructors are two is false, the determination is not performed, directly into a set of candidates, and the following determination defaultConstructor candidates will be added to the collection, a collection of candidates is three constructors, returned as a result. ** 5, return to the constructor if three, Spring will be how to determine which one constructor do? ** later will traverse three constructors Spring, Spring sequentially determines whether the parameter Bean (whether the IOC container management), if the parameter is not Bean, the skip determining a constructor, that is to say, the above-described two e.g. constructor parameter is not a parameter which Bean, will judge a constructor parameter, if this parameter is Bean, using a constructor instantiation parameter, if this parameter is not Bean, without the use of reference instance constructor. That is, if used for annotation @Autowired constructor injection, required properties are set to false, it will not prevent abnormal Bean injection using normal constructor with no arguments instantiated. If the two parameters are Bean, the constructor directly using two parameters instantiated and obtains a corresponding constructor Bean injection. In this final note, it can be seen from the above, if you want to use constructor injection feature, it is best to be injected into the constructor are marked @Autowired comment (If there is more need to inject constructors, all the required attributes @Autowired are set to false), if multiple constructors, need to inject only a constructor, this constructor can be marked @Autowired comment, do not set the required properties. If you do not hit the notes can also be used constructor injection feature, but the number can only constructor to 1, and the poor readability of the code, the code read people do not know where you use the constructor injection it, so here I If the proposals using constructor injection annotations marked @Autowired a little better.

Guess you like

Origin www.cnblogs.com/chen-chen-chen/p/11619737.html