Spring的注入模型( 二 )

之后在调用
org.springframework.beans.factory.annotation.InjectionMetadata#inject方法进行属性填充

Spring的注入模型

文章插图
 
其中
AutowiredAnnotationBeanPostProcessor类里面有3个内部类,其中2个类为AutowiredFieldElement和AutowiredMethodElement,这2个类封装了对打在字段上或者方法上的@Autowired注解进行处理的逻辑;
Spring的注入模型

文章插图
 
我们就根据AutowiredFieldElement字段上的@Autowired注解进行分析,这个也是我们最常用的方式;
入口方法为inject方法,就是上面InjectionMetadata遍历调用的inject方法;
Spring的注入模型

文章插图
 

Spring的注入模型

文章插图
 
org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency
Spring的注入模型

文章插图
 
org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency
public Object doResolveDependency(DependencyDescriptor descriptor, @Nullable String beanName,@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor);try {Object shortcut = descriptor.resolveShortcut(this);if (shortcut != null) {return shortcut;}// 获取依赖的类型Class<?> type = descriptor.getDependencyType();Object value = https://www.isolves.com/it/cxkf/kj/2022-06-08/getAutowireCandidateResolver().getSuggestedValue(descriptor);if (value != null) {if (value instanceof String) {String strVal = resolveEmbeddedValue((String) value);BeanDefinition bd = (beanName != null && containsBean(beanName) ?getMergedBeanDefinition(beanName) : null);value = evaluateBeanDefinitionString(strVal, bd);}TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());try {return converter.convertIfNecessary(value, type, descriptor.getTypeDescriptor());}catch (UnsupportedOperationException ex) {// A custom TypeConverter which does not support TypeDescriptor resolution...return (descriptor.getField() != null ?converter.convertIfNecessary(value, type, descriptor.getField()) :converter.convertIfNecessary(value, type, descriptor.getMethodParameter()));}}/*** 解析当前依赖是否支持多个bean注入,如:list、set、map类型等* 如果支持多个bean注入,则在内部就完成bean的查找,不为空直接返回*/Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);if (multipleBeans != null) {return multipleBeans;}/*** 根据类型查询bean实例,可能会有多个,key为需要注入对象的名字*/Map matchingBeans = findAutowireCandidates(beanName, type, descriptor);if (matchingBeans.isEmpty()) {// 判断在依赖上面是否加了必须的条件,默认为trueif (isRequired(descriptor)) {// 抛出异常raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);}// 如果非必须则返回null,不注入任何对象return null;}String autowiredBeanName;Object instanceCandidate;// 如果找出的实例大于1个if (matchingBeans.size() > 1) {// 通过依赖描述组件来推断需要注入对象的名字autowiredBeanName = determineAutowireCandidate(matchingBeans, descriptor);if (autowiredBeanName == null) {if (isRequired(descriptor) || !indicatesMultipleBeans(type)) {return descriptor.resolveNotUnique(descriptor.getResolvableType(), matchingBeans);}else {// In case of an optional Collection/Map, silently ignore a non-unique case:// possibly it was meant to be an empty collection of multiple regular beans// (before 4.3 in particular when we didn't even look for collection beans).return null;}}// 通过名字获取对象instanceCandidate = matchingBeans.get(autowiredBeanName);}else {// We have exactly one match.// 如果找出来的实例为1个,则直接获取出来,赋值给instanceCandidateMap.Entry entry = matchingBeans.entrySet().iterator().next();autowiredBeanName = entry.getKey();instanceCandidate = entry.getValue();}if (autowiredBeanNames != null) {autowiredBeanNames.add(autowiredBeanName);}// 判断是否为Class,如果是则说明没有实例化if (instanceCandidate instanceof Class) {// 实例化instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this);}// 将获取到的实例候选赋值给resultObject result = instanceCandidate;if (result instanceof NullBean) {if (isRequired(descriptor)) {raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);}result = null;}if (!ClassUtils.isAssignableValue(type, result)) {throw new BeanNotOfRequiredTypeException(autowiredBeanName, type, instanceCandidate.getClass());}return result;}finally {ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint);}}


推荐阅读