Spring 5.x Source trip twenty-two eight getBean Detailed summary of the relevant instantiateUsingFactoryMethod

instantiateUsingFactoryMethod important processes related summary

Speaking in front of a factory method to instantiate the process, but it is still more complex, many details go deep a little Mongolian, so to not-depth, first understand the general process, summarized here under so first roughly done.

instantiateUsingFactoryMethod general process

Here Insert Picture Description

createArgumentArray general process

Here Insert Picture Description

Points

  • There are multiple methods of reference time, it will be sorted by publicpriority, protectedfollowed, if the same modifier, it is in accordance with the number of rows of parameters.
  • Each method will try to get the assembly object parameters, then according to the type of parameter matching method to determine whether the same type of method, if any, there is no way to determine which local parameters, an exception will be reported. Thus in order to select the most suitable method to instantiate.

Look at the comparison method is the type of argument, that is, after the conversion right, right after the conversion it can not work, return the maximum weight difference, and then determine the original type, such as a primitive type Object, then go to the parameter type xxx, so that does not work, The biggest difference is returned -512,
the last full if the type is not correct, it returns the smallest difference.

//获得差异,越小越好
		public int getAssignabilityWeight(Class<?>[] paramTypes) {
			for (int i = 0; i < paramTypes.length; i++) {
				if (!ClassUtils.isAssignableValue(paramTypes[i], this.arguments[i])) {
					return Integer.MAX_VALUE;//如果有参数类型不同的话,直接返回最大
				}
			}
			for (int i = 0; i < paramTypes.length; i++) {
				if (!ClassUtils.isAssignableValue(paramTypes[i], this.rawArguments[i])) {
					return Integer.MAX_VALUE - 512;//有类型和原始参数类型不同的话就-512
				}
			}
			return Integer.MAX_VALUE - 1024;//否则相同就-1024
		}
  • A method of traversing the candidate time, if there is a display parameter passing, will display the parameter to the reference length, the length is not less than the process, which speeds up, or each one to acquire, and then determines the difference wasted .

Overload of the same name being given the same number of parameters

If overloaded, and it is a parameter, there will not know which factory method with friends:
Here Insert Picture Description
Exception information:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in com.ww.config.MyConfig: Ambiguous factory method matches found in bean 'userDao' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): [public com.ww.pojo.UserDao com.ww.config.MyConfig.userDao(com.ww.pojo.TestBean), protected com.ww.pojo.UserDao com.ww.config.MyConfig.userDao(com.ww.pojo.TestBean2)]

A plurality of parameter types of the same type being given injection subject

If this happens, he does not know to be injected which, in fact, ideahas prompted, parameter name can not be found.
Here Insert Picture Description
Exception information:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDao' defined in com.ww.config.MyConfig: Unsatisfied dependency expressed through method 'userDao' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.ww.pojo.TestBean2' available: expected single matching bean but found 2: t1,t2

For example, we may modify such a way, that is to use t1the method:
Here Insert Picture Description
inject objects have to repeat the words, DefaultListableBeanFactorythe determineAutowireCandidatemethod Lane will be judged, and the name of the method on the object corresponding to the injection comes in:
Here Insert Picture Description
Of course there are other methods such as using Qualifiernotes which method specified :
Here Insert Picture Description
in fact, in the interior DefaultListableBeanFactoryof findAutowireCandidatesthe determination isAutowireCandidateinternal inspection of:
Here Insert Picture Description
or this may be:
Here Insert Picture Description

Anyway, you have to have differences on the line. A factory method can involve so much knowledge, many of the details did not say, I am not lazy, is because it involves too deep, will be dizzy, and so the whole process finished details to say it.

Well, here today, we hope to help study and understand, do not spray the Great God see, understand only their own learning, limited capacity, please excuse.

Published 235 original articles · won praise 74 · views 30000 +

Guess you like

Origin blog.csdn.net/wangwei19871103/article/details/105097228