欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring源碼解析之推斷構造方法

 更新時間:2021年06月10日 11:46:03   作者:性感的大表哥  
今天給大家?guī)淼氖顷P于Java的相關知識,文章圍繞著Spring推斷構造方法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下

Spring推斷構造方法

貼個測試代碼直接開干,這只是個樣例,其他情況自行分析

@Component
public class OrderService {
 
	public OrderService() {
		System.out.println("無參構造方法");
	}
 
	@Autowired(required = false)
	public OrderService(UserService userService) {
		System.out.println("一個參數(shù)的構造方法");
	}
 
	@Autowired(required = false)
	public OrderService(String userName, String passWord) {
		System.out.println("兩個參數(shù)的構造方法");
	}
}

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
 
		// 加載類
		// Make sure bean class is actually resolved at this point.
		Class<?> beanClass = resolveBeanClass(mbd, beanName);
 
		// 確保class不為空,并且訪問權限為public
		if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
		}
 
		// 配置的一種特殊的callback回調(diào)方法,通過這個callback創(chuàng)建bean
		// 檢查BeanDefinition是否包含了一個Supplier
		Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
		if (instanceSupplier != null) {
			// 如果有就直接調(diào)用Supplier的get方法得到一個對象直接返回
			return obtainFromSupplier(instanceSupplier, beanName);
		}
 
		// 通過工廠方法創(chuàng)建
		if (mbd.getFactoryMethodName() != null) {
			// 如果BeanDefinition中存在FactoryMethodName,那么調(diào)用工廠方法得到一個bean對象并返回
			return instantiateUsingFactoryMethod(beanName, mbd, args);
		}
 
		// 一個類可能有多個構造器,所以Spring得根據(jù)參數(shù)個數(shù)、類型確定需要調(diào)用的構造器
		// 在使用構造器創(chuàng)建實例后,Spring會將解析過后確定下來的構造器或工廠方法保存在緩存中,避免再次創(chuàng)建相同bean時再次解析
		// Shortcut when re-creating the same bean...
		boolean resolved = false;
		boolean autowireNecessary = false;
		if (args == null) {
			synchronized (mbd.constructorArgumentLock) {
				if (mbd.resolvedConstructorOrFactoryMethod != null) {
					// 已經(jīng)解析過class的構造器
					resolved = true;
					autowireNecessary = mbd.constructorArgumentsResolved;
				}
			}
		}
		if (resolved) {
			// 已經(jīng)解析過class的構造器,使用已經(jīng)解析好的構造器
			if (autowireNecessary) {
				// 如果BeanDefinition中已經(jīng)構造過
				// 構造函數(shù)自動注入
				// 自動裝配構造函數(shù),俗稱推斷構造方法
				return autowireConstructor(beanName, mbd, null, null);
			}
			else {
				// 使用默認構造器
				return instantiateBean(beanName, mbd);
			}
		}
 
		// TODO 推斷構造方法
		// 需要根據(jù)參數(shù)解析、確定構造函數(shù)
		// 尋找當前實例化的bean中構造器是否有@Autowire注解
		// Candidate constructors for autowiring?
		Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
 
		// 解析的構造器不為空 || 注入類型為構造函數(shù)自動注入 || beanDefinition指定了構造方法參數(shù)值 || getBean時指定了構造方法參數(shù)
		if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
				mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
 
			// 到這里可能找到了多個構造方法,還要決定到底用哪個進行反射初始化
			return autowireConstructor(beanName, mbd, ctors, args);
		}
 
		// 默認構造的首選構造函數(shù)?
		// Preferred constructors for default construction?
		ctors = mbd.getPreferredConstructors();
		if (ctors != null) {
			return autowireConstructor(beanName, mbd, ctors, null);
		}
 
		// 無需特殊處理:只需使用默認的無參構造函數(shù)
		// No special handling: simply use no-arg constructor.
		return instantiateBean(beanName, mbd);
	}

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#determineConstructorsFromBeanPostProcessors

protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName)
			throws BeansException {
 
		// 裝配beanPostProcessor的時候會判斷其類型并設置 hasInstantiationAwareBeanPostProcessors 屬性, 符合條件才去找構造函數(shù)
		if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
 
			// getBeanPostProcessors拿到beanFactory中的所有BeanPostProcessor接口,找到一個合格的構造函數(shù)
			for (BeanPostProcessor bp : getBeanPostProcessors()) {
				if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
					SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
 
					// 獲取有autowire注解的構造函數(shù) 找到合格的構造函數(shù)
					// AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors
					Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName);
					if (ctors != null) {
						return ctors;
					}
				}
			}
		}
		return null;
	}

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors

public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final String beanName)
			throws BeanCreationException {
 
		// @Lookup標識的屬性每次調(diào)用都會被重新初始化,
		// 有些場景下原型類型的Bean就需要這樣做,否則每個Bean只會在spring容器初始化的時候創(chuàng)建一次,
		// 但是如果在一個單例的Bean中注入了一個原型的Bean,這樣的話原本原型的Bean就相當于變成了一個單例的Bean失去了原有的意義,
		// 這時就需要@Lookup來解決,或者是每次都從新從spring容器中通過getBean來獲取Bean
		// Let's check for lookup methods here...
		if (!this.lookupMethodsChecked.contains(beanName)) {
			if (AnnotationUtils.isCandidateClass(beanClass, Lookup.class)) {
				try {
					Class<?> targetClass = beanClass;
					do {
						ReflectionUtils.doWithLocalMethods(targetClass, method -> {
							Lookup lookup = method.getAnnotation(Lookup.class);
							if (lookup != null) {
								Assert.state(this.beanFactory != null, "No BeanFactory available");
								LookupOverride override = new LookupOverride(method, lookup.value());
								try {
									RootBeanDefinition mbd = (RootBeanDefinition)this.beanFactory.getMergedBeanDefinition(beanName);
									mbd.getMethodOverrides().addOverride(override);
								}
								catch (NoSuchBeanDefinitionException ex) {
									throw new BeanCreationException(beanName,
											"Cannot apply @Lookup to beans without corresponding bean definition");
								}
							}
						});
						targetClass = targetClass.getSuperclass();
					}
					while (targetClass != null && targetClass != Object.class);
 
				}
				catch (IllegalStateException ex) {
					throw new BeanCreationException(beanName, "Lookup method resolution failed", ex);
				}
			}
			this.lookupMethodsChecked.add(beanName);
		}
 
		// 一般只有原型的bean才會創(chuàng)建多次
		// Quick check on the concurrent map first, with minimal locking.
		Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
		if (candidateConstructors == null) {
			// Fully synchronized resolution now...
			synchronized (this.candidateConstructorsCache) {
				candidateConstructors = this.candidateConstructorsCache.get(beanClass);
				if (candidateConstructors == null) {
					Constructor<?>[] rawCandidates;
					try {
						// 獲取所有構造方法
						rawCandidates = beanClass.getDeclaredConstructors();
					}
					catch (Throwable ex) {
						throw new BeanCreationException(beanName,
								"Resolution of declared constructors on bean Class [" + beanClass.getName() +
								"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
					}
					// 定義要選舉的構造方法集合
					List<Constructor<?>> candidates = new ArrayList<>(rawCandidates.length);
 
					// 加了@AutoWire()并且是require=true的構造方法
					Constructor<?> requiredConstructor = null;
 
					// 默認構造發(fā)給方法
					Constructor<?> defaultConstructor = null;
 
					// 返回與 Kotlin 主構造函數(shù)相對應的 Java 構造函數(shù), 否則,特別是對于非 Kotlin 類,這只會返回 {@code null}。
					Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass);
 
					// 記錄合成的構造方法數(shù)量,理解為可用的構造方法個數(shù)吧
					int nonSyntheticConstructors = 0;
 
					// 遍歷所有的構造方法
					for (Constructor<?> candidate : rawCandidates) {
						if (!candidate.isSynthetic()) {
							nonSyntheticConstructors++;
						}
						else if (primaryConstructor != null) {
							continue;
						}
 
						// 加了@Autowired的構造方法
						MergedAnnotation<?> ann = findAutowiredAnnotation(candidate);
						if (ann == null) {
							Class<?> userClass = ClassUtils.getUserClass(beanClass);
							if (userClass != beanClass) {
								try {
									Constructor<?> superCtor = userClass.getDeclaredConstructor(candidate.getParameterTypes());
 
									// 在父類中找@Autowired的構造方法
									ann = findAutowiredAnnotation(superCtor);
								}
								catch (NoSuchMethodException ex) {
									// Simply proceed, no equivalent superclass constructor found...
								}
							}
						}
						if (ann != null) {
							// 如果找到加了@Autowired注解的構造方法,再判斷required屬性
 
							// 加了@AutoWire()并且是require=true的構造方法
							if (requiredConstructor != null) {
								throw new BeanCreationException(beanName,
										"Invalid autowire-marked constructor: " + candidate +
										". Found constructor with 'required' Autowired annotation already: " +
										requiredConstructor);
							}
							boolean required = determineRequiredStatus(ann);
							if (required) {
								if (!candidates.isEmpty()) {
									throw new BeanCreationException(beanName,
											"Invalid autowire-marked constructors: " + candidates +
											". Found constructor with 'required' Autowired annotation: " +
											candidate);
								}
								requiredConstructor = candidate;
							}
							candidates.add(candidate);
						}
						else if (candidate.getParameterCount() == 0) {
							// 否則如果構造函數(shù)參數(shù)個數(shù)為0,把它賦值給變量defaultConstructor
							defaultConstructor = candidate;
						}
					}
 
					// 處理上面遍歷后的結果
					if (!candidates.isEmpty()) {
						// Add default constructor to list of optional constructors, as fallback.
						if (requiredConstructor == null) {
							// 如果加了@Autowired、并且沒有指定required為true、并且存在默認的構造方法
							if (defaultConstructor != null) {
								// 把默認構造方法加到待篩選的集合中
								candidates.add(defaultConstructor);
							}
							else if (candidates.size() == 1 && logger.isInfoEnabled()) {
								logger.info("Inconsistent constructor declaration on bean with name '" + beanName +
										"': single autowire-marked constructor flagged as optional - " +
										"this constructor is effectively required since there is no " +
										"default constructor to fall back to: " + candidates.get(0));
							}
						}
						candidateConstructors = candidates.toArray(new Constructor<?>[0]);
					}
					// 如果只有一個構造方法,并且構造數(shù)的參數(shù)大于0
					else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
						candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
					}
					// primaryConstructor 做java開發(fā)一般都是null
					else if (nonSyntheticConstructors == 2 && primaryConstructor != null &&
							defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {
						candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor};
					}
					// primaryConstructor 做java開發(fā)一般都是null
					else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {
						candidateConstructors = new Constructor<?>[] {primaryConstructor};
					}
					else {
						candidateConstructors = new Constructor<?>[0];
					}
					// 把推斷的構造方法數(shù)組放到緩存map中
					this.candidateConstructorsCache.put(beanClass, candidateConstructors);
				}
			}
		}
		return (candidateConstructors.length > 0 ? candidateConstructors : null);

推斷構造方法第一步,先找出可用的構造方法,步驟如下:

1、先找出所有的構造方法。

2、遍歷所有構造方法,找出加了@Autowire的構造方法,如果沒找到就在父類中找,父類中還找不到,但是存在一個構造方法的參數(shù)的個數(shù)為0,就作為默認的構造方法;如果找到了加了@Autowire的構造方法,并且require都為true則直接報錯。

3、再次過濾上面篩選過的構造方法,如果有默認構造方法就加入候選者的集合;如果上面篩選過后沒有合適的構造方法,但是又只有參數(shù)個數(shù)大于0的構造方法,就把他加入到候選者的列表中。

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#autowireConstructor

protected BeanWrapper autowireConstructor(
			String beanName, RootBeanDefinition mbd, @Nullable Constructor<?>[] ctors, @Nullable Object[] explicitArgs) {
		// 帶有參數(shù)情況的實例化
		return new ConstructorResolver(this).autowireConstructor(beanName, mbd, ctors, explicitArgs);
	}

org.springframework.beans.factory.support.ConstructorResolver#autowireConstructor

public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
			@Nullable Constructor<?>[] chosenCtors, @Nullable Object[] explicitArgs) {
 
		//實例化BeanWrapper,是包裝bean的容器
		BeanWrapperImpl bw = new BeanWrapperImpl();
		this.beanFactory.initBeanWrapper(bw);
 
		Constructor<?> constructorToUse = null;
		ArgumentsHolder argsHolderToUse = null;
		Object[] argsToUse = null;
 
		// 1、首先判斷是否通過getBean方法指定了構造方法參數(shù)值
		// 如果getBean中傳入的參數(shù)不為空,那么就使用傳入的參數(shù)
		if (explicitArgs != null) {
			argsToUse = explicitArgs;
		}
		// 否則就需要解析配置文件中的參數(shù)
		else {
			Object[] argsToResolve = null;
			// 先嘗試從緩存中獲取
			synchronized (mbd.constructorArgumentLock) {
				// 緩存中的構造器
				constructorToUse = (Constructor<?>) mbd.resolvedConstructorOrFactoryMethod;
 
				// 2、針對當前BeanDefinition是否緩存了構造方法和構造方法參數(shù)值
				if (constructorToUse != null && mbd.constructorArgumentsResolved) {
					// 在緩存中找到了構造器,就繼續(xù)從緩存中尋找緩存的構造器參數(shù)
					// Found a cached constructor...
					argsToUse = mbd.resolvedConstructorArguments;
					if (argsToUse == null) {
						// 沒有緩存的參數(shù),就需要獲取配置文件中配置的參數(shù)
						argsToResolve = mbd.preparedConstructorArguments;
					}
				}
			}
			// 如果緩存中沒有緩存的參數(shù)的話,即argsToResolve不為空,就需要解析配置的參數(shù)
			if (argsToResolve != null) {
				// 解析參數(shù)類型,比如將配置的String類型轉(zhuǎn)換成int、boolean等類型
				argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve, true);
			}
		}
 
		// 3、如果兩者任意一個為空,則繼續(xù)進行下面的步驟
		// 如果沒有緩存,就需要從構造函數(shù)開始解析
		if (constructorToUse == null || argsToUse == null) {
 
			// 如果傳入的構造器數(shù)組不為空,就使用傳入的構造器參數(shù),否則通過反射獲取class中定義的構造器
			// Take specified constructors, if any.
			Constructor<?>[] candidates = chosenCtors;
 
			// 3.1 如果沒有傳入構造方法,那么則獲取當前BeanDefinition對應的BeanClass中所有的構造方法作為候選者
			if (candidates == null) {
				Class<?> beanClass = mbd.getBeanClass();
				try {
					// 使用public的構造器或者所有構造器
					candidates = (mbd.isNonPublicAccessAllowed() ?
							beanClass.getDeclaredConstructors() : beanClass.getConstructors());
				}
				catch (Throwable ex) {
					throw new BeanCreationException(mbd.getResourceDescription(), beanName,
							"Resolution of declared constructors on bean Class [" + beanClass.getName() +
							"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
				}
			}
 
			// 3.2 判斷候選者構造方法是不是只有一個,并且沒有指定構造方法參數(shù)
			if (candidates.length == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()) {
				Constructor<?> uniqueCandidate = candidates[0];
				if (uniqueCandidate.getParameterCount() == 0) {
					synchronized (mbd.constructorArgumentLock) {
						mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
						mbd.constructorArgumentsResolved = true;
						mbd.resolvedConstructorArguments = EMPTY_ARGS;
					}
					// 初始化并設置構造器參數(shù)
					bw.setBeanInstance(instantiate(beanName, mbd, uniqueCandidate, EMPTY_ARGS));
					return bw;
				}
			}
 
			// 是否需要解析構造器,在配置文件中指定注入方式為構造器注入
			// Need to resolve the constructor.
			boolean autowiring = (chosenCtors != null ||
					mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
 
			// 存放解析后的構造方法參數(shù)值
			ConstructorArgumentValues resolvedValues = null;
 
			int minNrOfArgs;
			if (explicitArgs != null) {
				// getBean方法傳入的參數(shù)
				minNrOfArgs = explicitArgs.length;
			}
			else {
				// 配置文件中的配置的參數(shù)
				ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
				// 用于承載解析后的構造函數(shù)參數(shù)的值
				resolvedValues = new ConstructorArgumentValues();
				// 解析配置文件中的參數(shù),并且返回參數(shù)個數(shù)
				minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
			}
 
			// 3.3 對候選者構造函數(shù)排序,public構造函數(shù)優(yōu)先、參數(shù)數(shù)量降序排序
			AutowireUtils.sortConstructors(candidates);
 
			// 計算構造方法參數(shù)個數(shù)最少個數(shù)
			// 意思是如果指定了構造方法參數(shù)個數(shù),所以當前BeanDefinition對應的BeanClass中所有構造方法參數(shù)個數(shù)至少滿足手動指定的參數(shù)值個數(shù)
			int minTypeDiffWeight = Integer.MAX_VALUE;
 
			Set<Constructor<?>> ambiguousConstructors = null;
			LinkedList<UnsatisfiedDependencyException> causes = null;
 
			// 3.4 遍歷所有的構造方法
			for (Constructor<?> candidate : candidates) {
				int parameterCount = candidate.getParameterCount();
 
				if (constructorToUse != null && argsToUse != null && argsToUse.length > parameterCount) {
					// Already found greedy constructor that can be satisfied ->
					// do not look any further, there are only less greedy constructors left.
					break;
				}
 
				// 如果候選者參數(shù)個數(shù) < minNrOfArgs,則不匹配,繼續(xù)下一個
				if (parameterCount < minNrOfArgs) {
					continue;
				}
 
				// 封裝解析到的參數(shù)信息
				ArgumentsHolder argsHolder;
 
				Class<?>[] paramTypes = candidate.getParameterTypes();
 
				// 解析配置文件得到的構造方法參數(shù)值
				if (resolvedValues != null) {
					try {
						// 3.5 判斷通過getBean方法指定構造方法參數(shù)
						String[] paramNames = ConstructorPropertiesChecker.evaluate(candidate, parameterCount);
						if (paramNames == null) {
							ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
							if (pnd != null) {
								paramNames = pnd.getParameterNames(candidate);
							}
						}
						// 參數(shù)個數(shù)匹配的情況下把所有參數(shù)封裝為一個ArgumentsHolder對象,不匹配就直接報錯了
						argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes, paramNames,
								getUserDeclaredConstructor(candidate), autowiring, candidates.length == 1);
					}
					catch (UnsatisfiedDependencyException ex) {
						if (logger.isTraceEnabled()) {
							logger.trace("Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
						}
						// Swallow and try next constructor.
						if (causes == null) {
							causes = new LinkedList<>();
						}
						causes.add(ex);
						continue;
					}
				}
				else {
					// 處理參數(shù)由getBean方法傳入的情況
					// Explicit arguments given -> arguments length must match exactly.
					if (parameterCount != explicitArgs.length) {
						continue;
					}
					argsHolder = new ArgumentsHolder(explicitArgs);
				}
 
				// 3.7 計算得到的構造方法參數(shù)值和參數(shù)的匹配程度
				// 因為不同構造函數(shù)的參數(shù)個數(shù)相同,而且參數(shù)類型為父子關系,所以需要找出類型最符合的一個構造函數(shù)
				// Spring用一種權重的形式來表示類型差異程度,差異權重越小越優(yōu)先
				// 如果是以寬松的方式解析,默認為true,所以執(zhí)行getTypeDifferenceWeight
				int typeDiffWeight = (mbd.isLenientConstructorResolution() ?
						argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes));
 
				// 當前構造函數(shù)最為匹配的話,清空先前ambiguousConstructors列表
				// Choose this constructor if it represents the closest match.
				if (typeDiffWeight < minTypeDiffWeight) {
					constructorToUse = candidate;
					argsHolderToUse = argsHolder;
					argsToUse = argsHolder.arguments;
					minTypeDiffWeight = typeDiffWeight;
					ambiguousConstructors = null;
				}
				// 存在相同權重的構造器,將構造器添加到一個ambiguousConstructors列表變量中
				// 注意,這時候constructorToUse 指向的仍是第一個匹配的構造函數(shù)
				else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
					if (ambiguousConstructors == null) {
						ambiguousConstructors = new LinkedHashSet<>();
						ambiguousConstructors.add(constructorToUse);
					}
					ambiguousConstructors.add(candidate);
				}
			}
 
			/*******************************************************************************************************/
 
			if (constructorToUse == null) {
				// 如果沒有匹配的構造函數(shù),拋出異常。略
				if (causes != null) {
					UnsatisfiedDependencyException ex = causes.removeLast();
					for (Exception cause : causes) {
						this.beanFactory.onSuppressedException(cause);
					}
					throw ex;
				}
				throw new BeanCreationException(mbd.getResourceDescription(), beanName,
						"Could not resolve matching constructor " +
						"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
			}
			else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
				// 如果存在多個構造函數(shù)匹配程度相同,并且BeanDefinition中設置isLenientConstructorResolution為false(默認值為true),
				// 表示構造器創(chuàng)建為嚴格模式的話,會拋出異常。異常代碼略
				throw new BeanCreationException(mbd.getResourceDescription(), beanName,
						"Ambiguous constructor matches found in bean '" + beanName + "' " +
						"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
						ambiguousConstructors);
			}
 
			if (explicitArgs == null && argsHolderToUse != null) {
				argsHolderToUse.storeCache(mbd, constructorToUse);
			}
		}
 
		Assert.state(argsToUse != null, "Unresolved constructor arguments");
 
		// 初始化
		bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse));
		return bw;
	}

1、只有一個無參的構造方法,那么直接使用無參的構造方法進行實例化 candidates.length == 1

2、有多個構造方法或者bean需要通過構造方法自動進行注入 ResolvedAutowireMode() == 3

3、根據(jù)所指定的構造方法參數(shù)值,確定所需要的最少的構造方法參數(shù)值的個數(shù) minNrOfArgs

4、對所有的構造方法進行排序,參數(shù)個數(shù)多的在前面 AutowireUtils.sortConstructors

5、遍歷所有的構造方法

6、如果當前構造方法參數(shù)個數(shù)小于minNrOfArgs則不匹配,繼續(xù)判斷下一個構造方法

7、如果是調(diào)用getBean方法指定的參數(shù)就直接利用這些值,如果不是就根據(jù)構造方法參數(shù)類型找值(先byType再byName),匹配的話則封裝成一個ArgumentsHolder對象

8、這里可能會匹配到多個構造方法,然后就需要那值和構造方法匹配程度計算一個權重,值越小優(yōu)先級越高(因為如果是父子類的話子類匹配成功更高)

9、計算權重分為寬松型(默認)和嚴格型,嚴格型的情況下如果有多個匹配就報錯

到此這篇關于Spring源碼解析之推斷構造方法的文章就介紹到這了,更多相關Spring推斷構造方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java 文件上傳的實例詳解

    Java 文件上傳的實例詳解

    這篇文章主要介紹了Java 文件上傳的實例詳解的相關資料,希望通過本文大家能掌握這部分內(nèi)容,使用幾種文件上傳的方法,需要的朋友可以參考下
    2017-09-09
  • SpringBoot使用@Cacheable注解實現(xiàn)緩存功能流程詳解

    SpringBoot使用@Cacheable注解實現(xiàn)緩存功能流程詳解

    最近一直再學Spring Boot,在學習的過程中也有過很多疑問。為了解答自己的疑惑,也在網(wǎng)上查了一些資料,以下是對@Cacheable注解的一些理解
    2023-01-01
  • java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行的方法詳解

    java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行的方法詳解

    這篇文章主要介紹了java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-09-09
  • 詳解JDK中ExecutorService與Callable和Future對線程的支持

    詳解JDK中ExecutorService與Callable和Future對線程的支持

    這篇文章主要介紹了詳解JDK中ExecutorService與Callable和Future對線程的支持的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • java 數(shù)組轉(zhuǎn)list的兩種方式

    java 數(shù)組轉(zhuǎn)list的兩種方式

    這篇文章主要介紹了java 數(shù)組轉(zhuǎn)list的兩種方式,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-10-10
  • Java斗地主發(fā)牌課程設計

    Java斗地主發(fā)牌課程設計

    這篇文章主要為大家詳細介紹了Java斗地主發(fā)牌課程設計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • SpringBoot中快速實現(xiàn)郵箱發(fā)送代碼解析

    SpringBoot中快速實現(xiàn)郵箱發(fā)送代碼解析

    這篇文章主要介紹了SpringBoot中快速實現(xiàn)郵箱發(fā)送代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • Java 在PDF中添加騎縫章示例解析

    Java 在PDF中添加騎縫章示例解析

    這篇文章主要介紹了Java 在PDF中添加騎縫章示例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Spring整合Springmvc的相關介紹

    Spring整合Springmvc的相關介紹

    今天小編就為大家分享一篇關于Spring整合Springmvc的相關介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java實現(xiàn)二分法變種的示例代碼

    Java實現(xiàn)二分法變種的示例代碼

    這篇文章主要為大家介紹了Java實現(xiàn)二分法變種的示例代碼復,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11

最新評論