今天在研究代码的过程中发现@Value 注解也走的是@Autowired 自动注入的流程, 接下来研究@Autowired 和 @Resource 的逻辑。
0. 前置理解
属性注入属于Spring 生命周期的一部分,其开始时间是在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean 属性设置过程中。其利用的也是后置处理器,在populateBean方法中获取到所有的后置处理器,然后判断如果是 InstantiationAwareBeanPostProcessor 类型,则调用 postProcessProperties 进行属性注入。其实@Resource和@Autowired(@Value) 用的分别是CommonAnnotationBeanPostProcessor 和 AutowiredAnnotationBeanPostProcessor。
继承图如下:
1. CommonAnnotationBeanPostProcessor:处理@Resource
static { try { @SuppressWarnings("unchecked") Class<? extends Annotation> clazz = (Class<? extends Annotation>) ClassUtils.forName("javax.xml.ws.WebServiceRef", CommonAnnotationBeanPostProcessor.class.getClassLoader()); webServiceRefClass = clazz; } catch (ClassNotFoundException ex) { webServiceRefClass = null; } try { @SuppressWarnings("unchecked") Class<? extends Annotation> clazz = (Class<? extends Annotation>) ClassUtils.forName("javax.ejb.EJB", CommonAnnotationBeanPostProcessor.class.getClassLoader()); ejbRefClass = clazz; } catch (ClassNotFoundException ex) { ejbRefClass = null; } resourceAnnotationTypes.add(Resource.class); if (webServiceRefClass != null) { resourceAnnotationTypes.add(webServiceRefClass); } if (ejbRefClass != null) { resourceAnnotationTypes.add(ejbRefClass); } }
2. AutowiredAnnotationBeanPostProcessor 处理@Autowired和@Resource
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#AutowiredAnnotationBeanPostProcessor 默认创建指定其处理的注解:
public AutowiredAnnotationBeanPostProcessor() { this.autowiredAnnotationTypes.add(Autowired.class); this.autowiredAnnotationTypes.add(Value.class); try { this.autowiredAnnotationTypes.add((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader())); logger.trace("JSR-330 ‘javax.inject.Inject‘ annotation found and supported for autowiring"); } catch (ClassNotFoundException ex) { // JSR-330 API not available - simply skip. } }
1. 测试
Class1: 核心类的相关注入如下,接下来主要研究其注入过程。
package cn.qz.dubbo.injecttest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component public class Class1 { @Autowired private Class2 class2; @Resource private Class3 class3; @Value("${server.port}") private String port; private Class5 class5; public Class1(@Autowired Class4 class4) { System.out.println("======class1======"); } @Autowired public void setClass5(Class5 class5) { System.out.println("======setClass5======"); this.class5 = class5; } }
控制台:(如下代表对象创建过程)
======class4====== ======class1====== ======class3====== ======class2====== ======class5====== ======setClass5======
2. 构造注入过程
我们知道Spring 容器bean的生命周期大概可以分为: 注册beanDefinition -》 反射创建对象 -》 注入bean -》initialing -》 使用 -》 卸载
在构造过程中如果有自动注入的对象,查看其创建过程。
1. 将断点打到Class4 的构造上,查看调用链如下:
2. 过程梳理:
(1) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance 会选择合适的策略创建对象
/** * Create a new instance for the specified bean, using an appropriate instantiation strategy: * factory method, constructor autowiring, or simple instantiation. * @param beanName the name of the bean * @param mbd the bean definition for the bean * @param args explicit arguments to use for constructor or factory method invocation * @return a BeanWrapper for the new instance * @see #obtainFromSupplier * @see #instantiateUsingFactoryMethod * @see #autowireConstructor * @see #instantiateBean */ 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); 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()); } Supplier<?> instanceSupplier = mbd.getInstanceSupplier(); if (instanceSupplier != null) { return obtainFromSupplier(instanceSupplier, beanName); } if (mbd.getFactoryMethodName() != null) { return instantiateUsingFactoryMethod(beanName, mbd, args); } // Shortcut when re-creating the same bean... boolean resolved = false; boolean autowireNecessary = false; if (args == null) { synchronized (mbd.constructorArgumentLock) { if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } if (resolved) { if (autowireNecessary) { return autowireConstructor(beanName, mbd, null, null); } else { return instantiateBean(beanName, mbd); } } // Candidate constructors for autowiring? Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } // Preferred constructors for default construction? ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); } // No special handling: simply use no-arg constructor. return instantiateBean(beanName, mbd); }
这里会autowireConstructor(beanName, mbd, ctors, args); 这个代码块。 可以看到上面的逻辑是先判断是否需要自动注入,然后没有的话走默认的空构造。
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#determineConstructorsFromBeanPostProcessors 这个方法用后置处理器判断构造方法上是否满足备选条件:
protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName) throws BeansException { if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName); if (ctors != null) { return ctors; } } } } return null; }
最终在这里会委托给org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors 方法,方法内部实际也就是判断构造上面是否有@Autowired 注解 和 @Value 注解。 最终会委托给org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#findAutowiredAnnotation 判断是否有注解:
@Nullable private MergedAnnotation<?> findAutowiredAnnotation(AccessibleObject ao) { MergedAnnotations annotations = MergedAnnotations.from(ao); for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) { MergedAnnotation<?> annotation = annotations.get(type); if (annotation.isPresent()) { return annotation; } } return null; }
(2) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#autowireConstructor
protected BeanWrapper autowireConstructor( String beanName, RootBeanDefinition mbd, @Nullable Constructor<?>[] ctors, @Nullable Object[] explicitArgs) { return new ConstructorResolver(this).autowireConstructor(beanName, mbd, ctors, explicitArgs); }
(3) org.springframework.beans.factory.support.ConstructorResolver#autowireConstructor
(4) org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency
(5) org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency
(6) org.springframework.beans.factory.config.DependencyDescriptor#resolveCandidate
public Object resolveCandidate(String beanName, Class<?> requiredType, BeanFactory beanFactory) throws BeansException { return beanFactory.getBean(beanName); }
相当于先获取依赖的bean,这样完成构造注入。
3. 属性注入的过程
这里修改测试类,@Resource 注入两个bean,一个根据beanName 能获取到对象,一个获取不到:
package cn.qlq.dubbo.injecttest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component public class Class1 { @Autowired private Class2 class2; @Resource private Class3 class3; @Resource private Class3 class33933; @Value("${server.port}") private String port; @Value("${server.port}") private Integer port1; private Class5 class5; public Class1(@Autowired Class4 class4) { System.out.println("======class1======"); } @Autowired public void setClass5(Class5 class5) { System.out.println("======setClass5======"); this.class5 = class5; } }
1. @Resource 注入过程:
1. spring 容器创建对象过程中org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean 方法中会调用org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean 方法进行对象的属性注入,该方法内部有如下代码块:
PropertyDescriptor[] filteredPds = null; if (hasInstAwareBpps) { if (pvs == null) { pvs = mbd.getPropertyValues(); } for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { if (filteredPds == null) { filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { return; } } pvs = pvsToUse; } } }
这里获取到的对象后置处理器集合是:
2. 遍历处理器集合进行判断,如果是InstantiationAwareBeanPostProcessor 实例就调用postProcessProperties 方法。然后根据结果调用postProcessPropertyValues 方法。然后会调用到org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#postProcessProperties 方法:
@Override public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { InjectionMetadata metadata = findResourceMetadata(beanName, bean.getClass(), pvs); try { metadata.inject(bean, beanName, pvs); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Injection of resource dependencies failed", ex); } return pvs; }
(1) org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#findResourceMetadata 从方法名可以看出来其操作是获取@Resource 注解声明的元数据:
1 private InjectionMetadata findResourceMetadata(String beanName, final Class<?> clazz, @Nullable PropertyValues pvs) { 2 // Fall back to class name as cache key, for backwards compatibility with custom callers. 3 String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); 4 // Quick check on the concurrent map first, with minimal locking. 5 InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); 6 if (InjectionMetadata.needsRefresh(metadata, clazz)) { 7 synchronized (this.injectionMetadataCache) { 8 metadata = this.injectionMetadataCache.get(cacheKey); 9 if (InjectionMetadata.needsRefresh(metadata, clazz)) { 10 if (metadata != null) { 11 metadata.clear(pvs); 12 } 13 metadata = buildResourceMetadata(clazz); 14 this.injectionMetadataCache.put(cacheKey, metadata); 15 } 16 } 17 } 18 return metadata; 19 } 20 21 private InjectionMetadata buildResourceMetadata(final Class<?> clazz) { 22 if (!AnnotationUtils.isCandidateClass(clazz, resourceAnnotationTypes)) { 23 return InjectionMetadata.EMPTY; 24 } 25 26 List<InjectionMetadata.InjectedElement> elements = new ArrayList<>(); 27 Class<?> targetClass = clazz; 28 29 do { 30 final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>(); 31 32 ReflectionUtils.doWithLocalFields(targetClass, field -> { 33 if (webServiceRefClass != null && field.isAnnotationPresent(webServiceRefClass)) { 34 if (Modifier.isStatic(field.getModifiers())) { 35 throw new IllegalStateException("@WebServiceRef annotation is not supported on static fields"); 36 } 37 currElements.add(new WebServiceRefElement(field, field, null)); 38 } 39 else if (ejbRefClass != null && field.isAnnotationPresent(ejbRefClass)) { 40 if (Modifier.isStatic(field.getModifiers())) { 41 throw new IllegalStateException("@EJB annotation is not supported on static fields"); 42 } 43 currElements.add(new EjbRefElement(field, field, null)); 44 } 45 else if (field.isAnnotationPresent(Resource.class)) { 46 if (Modifier.isStatic(field.getModifiers())) { 47 throw new IllegalStateException("@Resource annotation is not supported on static fields"); 48 } 49 if (!this.ignoredResourceTypes.contains(field.getType().getName())) { 50 currElements.add(new ResourceElement(field, field, null)); 51 } 52 } 53 }); 54 55 ReflectionUtils.doWithLocalMethods(targetClass, method -> { 56 Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); 57 if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { 58 return; 59 } 60 if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { 61 if (webServiceRefClass != null && bridgedMethod.isAnnotationPresent(webServiceRefClass)) { 62 if (Modifier.isStatic(method.getModifiers())) { 63 throw new IllegalStateException("@WebServiceRef annotation is not supported on static methods"); 64 } 65 if (method.getParameterCount() != 1) { 66 throw new IllegalStateException("@WebServiceRef annotation requires a single-arg method: " + method); 67 } 68 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); 69 currElements.add(new WebServiceRefElement(method, bridgedMethod, pd)); 70 } 71 else if (ejbRefClass != null && bridgedMethod.isAnnotationPresent(ejbRefClass)) { 72 if (Modifier.isStatic(method.getModifiers())) { 73 throw new IllegalStateException("@EJB annotation is not supported on static methods"); 74 } 75 if (method.getParameterCount() != 1) { 76 throw new IllegalStateException("@EJB annotation requires a single-arg method: " + method); 77 } 78 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); 79 currElements.add(new EjbRefElement(method, bridgedMethod, pd)); 80 } 81 else if (bridgedMethod.isAnnotationPresent(Resource.class)) { 82 if (Modifier.isStatic(method.getModifiers())) { 83 throw new IllegalStateException("@Resource annotation is not supported on static methods"); 84 } 85 Class<?>[] paramTypes = method.getParameterTypes(); 86 if (paramTypes.length != 1) { 87 throw new IllegalStateException("@Resource annotation requires a single-arg method: " + method); 88 } 89 if (!this.ignoredResourceTypes.contains(paramTypes[0].getName())) { 90 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); 91 currElements.add(new ResourceElement(method, bridgedMethod, pd)); 92 } 93 } 94 } 95 }); 96 97 elements.addAll(0, currElements); 98 targetClass = targetClass.getSuperclass(); 99 } 100 while (targetClass != null && targetClass != Object.class); 101 102 return InjectionMetadata.forElements(elements, clazz); 103 }
可以看到是利用反射遍历字段和方法,获取带@Resource 注解的属性和方法,然后创建ResourceElement 对象之后封装成对象返回,最后获取到的metadata 对象如下:
(2) org.springframework.beans.factory.annotation.InjectionMetadata#inject 方法进行注入
public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { Collection<InjectedElement> checkedElements = this.checkedElements; Collection<InjectedElement> elementsToIterate = (checkedElements != null ? checkedElements : this.injectedElements); if (!elementsToIterate.isEmpty()) { for (InjectedElement element : elementsToIterate) { if (logger.isTraceEnabled()) { logger.trace("Processing injected element of bean ‘" + beanName + "‘: " + element); } element.inject(target, beanName, pvs); } } } /** * Either this or {@link #getResourceToInject} needs to be overridden. */ protected void inject(Object target, @Nullable String requestingBeanName, @Nullable PropertyValues pvs) throws Throwable { if (this.isField) { Field field = (Field) this.member; ReflectionUtils.makeAccessible(field); field.set(target, getResourceToInject(target, requestingBeanName)); } else { if (checkPropertySkipping(pvs)) { return; } try { Method method = (Method) this.member; ReflectionUtils.makeAccessible(method); method.invoke(target, getResourceToInject(target, requestingBeanName)); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } } }
可以看到是遍历获取到的element 集合,然后依次进行调用其 inject 方法。并且是采用模板方法模式,后面子类需要重写inject 或者 getResourceToInject 方法。
(3) 走field.set(target, getResourceToInject(target, requestingBeanName)); 这里实际就是反射设置属性的值,field 就是需要注入的对象,target 就是目标对象。值也就是需要注入的属性的值调用到:org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.ResourceElement#getResourceToInject
@Override protected Object getResourceToInject(Object target, @Nullable String requestingBeanName) { return (this.lazyLookup ? buildLazyResourceProxy(this, requestingBeanName) : getResource(this, requestingBeanName)); }
继续调用走org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#getResource:
protected Object getResource(LookupElement element, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { if (StringUtils.hasLength(element.mappedName)) { return this.jndiFactory.getBean(element.mappedName, element.lookupType); } if (this.alwaysUseJndiLookup) { return this.jndiFactory.getBean(element.name, element.lookupType); } if (this.resourceFactory == null) { throw new NoSuchBeanDefinitionException(element.lookupType, "No resource factory configured - specify the ‘resourceFactory‘ property"); } return autowireResource(this.resourceFactory, element, requestingBeanName); }
走最后的方法
(4) org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#autowireResource
/** * Obtain a resource object for the given name and type through autowiring * based on the given factory. * @param factory the factory to autowire against * @param element the descriptor for the annotated field/method * @param requestingBeanName the name of the requesting bean * @return the resource object (never {@code null}) * @throws NoSuchBeanDefinitionException if no corresponding target resource found */ protected Object autowireResource(BeanFactory factory, LookupElement element, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { Object resource; Set<String> autowiredBeanNames; String name = element.name; if (factory instanceof AutowireCapableBeanFactory) { AutowireCapableBeanFactory beanFactory = (AutowireCapableBeanFactory) factory; DependencyDescriptor descriptor = element.getDependencyDescriptor(); if (this.fallbackToDefaultTypeMatch && element.isDefaultName && !factory.containsBean(name)) { // 容器中没有对应name的bean autowiredBeanNames = new LinkedHashSet<>(); resource = beanFactory.resolveDependency(descriptor, requestingBeanName, autowiredBeanNames, null); if (resource == null) { throw new NoSuchBeanDefinitionException(element.getLookupType(), "No resolvable resource object"); } } else { // 容器中有对应name的bean resource = beanFactory.resolveBeanByName(name, descriptor); autowiredBeanNames = Collections.singleton(name); } } else { resource = factory.getBean(name, element.lookupType); autowiredBeanNames = Collections.singleton(name); } if (factory instanceof ConfigurableBeanFactory) { ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory; for (String autowiredBeanName : autowiredBeanNames) { if (requestingBeanName != null && beanFactory.containsBean(autowiredBeanName)) { beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName); } } } return resource; }
这里可以看到先根据name 判断容器是否有对应name的bean。然后走不同的解析方法获取依赖的对象,然后返回去用于反射设置属性。
- 对于存在name的bean调用链如下, 相当于从容器中直接调用getBean(name)获取bean,比如上面依赖注入class3:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#resolveBeanByName:
public Object resolveBeanByName(String name, DependencyDescriptor descriptor) { InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor); try { return getBean(name, descriptor.getDependencyType()); } finally { ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint); } }
org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String, java.lang.Class<T>):
@Override public <T> T getBean(String name, Class<T> requiredType) throws BeansException { return doGetBean(name, requiredType, null, false); }
对应的beanName 和 requiredType 如下:
- 对于不存在name的bean调用链如下,比如上面依赖注入class33933, 如果解析不到bean 就抛出异常NoSuchBeanDefinitionException(element.getLookupType(), "No resolvable resource object")
org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency:
@Override @Nullable public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException { descriptor.initParameterNameDiscovery(getParameterNameDiscoverer()); if (Optional.class == descriptor.getDependencyType()) { return createOptionalDependency(descriptor, requestingBeanName); } else if (ObjectFactory.class == descriptor.getDependencyType() || ObjectProvider.class == descriptor.getDependencyType()) { return new DependencyObjectProvider(descriptor, requestingBeanName); } else if (javaxInjectProviderClass == descriptor.getDependencyType()) { return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName); } else { Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary( descriptor, requestingBeanName); if (result == null) { result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter); } return result; } }
相关的参数 descriptor 如下:
然后走代码doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter); 调用到:org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency
@Nullable 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 = 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())); } } Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter); if (multipleBeans != null) { return multipleBeans; } Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor); if (matchingBeans.isEmpty()) { if (isRequired(descriptor)) { raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor); } return null; } String autowiredBeanName; Object instanceCandidate; 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. Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next(); autowiredBeanName = entry.getKey(); instanceCandidate = entry.getValue(); } if (autowiredBeanNames != null) { autowiredBeanNames.add(autowiredBeanName); } if (instanceCandidate instanceof Class) { instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this); } Object 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); } }
核心逻辑如下:
1》descriptor.getDependencyType(); 找到依赖的类型,如上会获取到 class cn.qlq.dubbo.injecttest.Class3
2》org.springframework.beans.factory.support.DefaultListableBeanFactory#findAutowireCandidates 根据type 获取到容器中存在的beanName
protected Map<String, Object> findAutowireCandidates( @Nullable String beanName, Class<?> requiredType, DependencyDescriptor descriptor) { String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this, requiredType, true, descriptor.isEager()); Map<String, Object> result = new LinkedHashMap<>(candidateNames.length); for (Map.Entry<Class<?>, Object> classObjectEntry : this.resolvableDependencies.entrySet()) { Class<?> autowiringType = classObjectEntry.getKey(); if (autowiringType.isAssignableFrom(requiredType)) { Object autowiringValue = classObjectEntry.getValue(); autowiringValue = AutowireUtils.resolveAutowiringValue(autowiringValue, requiredType); if (requiredType.isInstance(autowiringValue)) { result.put(ObjectUtils.identityToString(autowiringValue), autowiringValue); break; } } } for (String candidate : candidateNames) { if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, descriptor)) { addCandidateEntry(result, candidate, descriptor, requiredType); } } if (result.isEmpty()) { boolean multiple = indicatesMultipleBeans(requiredType); // Consider fallback matches if the first pass failed to find anything... DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch(); for (String candidate : candidateNames) { if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, fallbackDescriptor) && (!multiple || getAutowireCandidateResolver().hasQualifier(descriptor))) { addCandidateEntry(result, candidate, descriptor, requiredType); } } if (result.isEmpty() && !multiple) { // Consider self references as a final pass... // but in the case of a dependency collection, not the very same bean itself. for (String candidate : candidateNames) { if (isSelfReference(beanName, candidate) && (!(descriptor instanceof MultiElementDescriptor) || !beanName.equals(candidate)) && isAutowireCandidate(candidate, fallbackDescriptor)) { addCandidateEntry(result, candidate, descriptor, requiredType); } } } } return result; }
candidateNames 获取到的值是: ["class3"]
其实也是调用BeanFactory 的方法: org.springframework.beans.factory.BeanFactoryUtils#beanNamesForTypeIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory, java.lang.Class<?>, boolean, boolean)
public static String[] beanNamesForTypeIncludingAncestors( ListableBeanFactory lbf, Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit); result = mergeNamesWithParent(result, parentResult, hbf); } } return result; }
lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit); 根据类型获取name
3》判断获取到的matchingBeans。
如果为空,返回null, 则上层抛出NoSuchBeanDefinitionException(element.getLookupType(), "No resolvable resource object")
如果不为空,获取到autowiredBeanName, 然后直接从matchingBeans 获取到一个valuue 然后返回去,也就是获取到满足条件的bean
2. @Autowired 注入过程
1. 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean 属性设置过程中,同上面代码一样有获取InstantiationAwareBeanPostProcessor 后置处理器,然后调用postProcessProperties 方法的过程。其中@Autowired和@Value 使用的是org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessProperties
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs); try { metadata.inject(bean, beanName, pvs); } catch (BeanCreationException ex) { throw ex; } catch (Throwable ex) { throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex); } return pvs; } private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) { // Fall back to class name as cache key, for backwards compatibility with custom callers. String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); // Quick check on the concurrent map first, with minimal locking. InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); if (InjectionMetadata.needsRefresh(metadata, clazz)) { synchronized (this.injectionMetadataCache) { metadata = this.injectionMetadataCache.get(cacheKey); if (InjectionMetadata.needsRefresh(metadata, clazz)) { if (metadata != null) { metadata.clear(pvs); } metadata = buildAutowiringMetadata(clazz); this.injectionMetadataCache.put(cacheKey, metadata); } } } return metadata; } private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) { // Fall back to class name as cache key, for backwards compatibility with custom callers. String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); // Quick check on the concurrent map first, with minimal locking. InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); if (InjectionMetadata.needsRefresh(metadata, clazz)) { synchronized (this.injectionMetadataCache) { metadata = this.injectionMetadataCache.get(cacheKey); if (InjectionMetadata.needsRefresh(metadata, clazz)) { if (metadata != null) { metadata.clear(pvs); } metadata = buildAutowiringMetadata(clazz); this.injectionMetadataCache.put(cacheKey, metadata); } } } return metadata; } private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) { if (!AnnotationUtils.isCandidateClass(clazz, this.autowiredAnnotationTypes)) { return InjectionMetadata.EMPTY; } List<InjectionMetadata.InjectedElement> elements = new ArrayList<>(); Class<?> targetClass = clazz; do { final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>(); ReflectionUtils.doWithLocalFields(targetClass, field -> { MergedAnnotation<?> ann = findAutowiredAnnotation(field); if (ann != null) { if (Modifier.isStatic(field.getModifiers())) { if (logger.isInfoEnabled()) { logger.info("Autowired annotation is not supported on static fields: " + field); } return; } boolean required = determineRequiredStatus(ann); currElements.add(new AutowiredFieldElement(field, required)); } }); ReflectionUtils.doWithLocalMethods(targetClass, method -> { Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { return; } MergedAnnotation<?> ann = findAutowiredAnnotation(bridgedMethod); if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { if (Modifier.isStatic(method.getModifiers())) { if (logger.isInfoEnabled()) { logger.info("Autowired annotation is not supported on static methods: " + method); } return; } if (method.getParameterCount() == 0) { if (logger.isInfoEnabled()) { logger.info("Autowired annotation should only be used on methods with parameters: " + method); } } boolean required = determineRequiredStatus(ann); PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); currElements.add(new AutowiredMethodElement(method, required, pd)); } }); elements.addAll(0, currElements); targetClass = targetClass.getSuperclass(); } while (targetClass != null && targetClass != Object.class); return InjectionMetadata.forElements(elements, clazz); }
可以看到第一步是调用findAutowiringMetadata 获取到InjectionMetadata 对象,实际也就是遍历该类的所有字段和相关方法,过滤带有@Autowired和@Value 注解属性的构造成InjectedElement 对象。
获取到的对象如下:
2. 调用org.springframework.beans.factory.annotation.InjectionMetadata#inject
public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { Collection<InjectedElement> checkedElements = this.checkedElements; Collection<InjectedElement> elementsToIterate = (checkedElements != null ? checkedElements : this.injectedElements); if (!elementsToIterate.isEmpty()) { for (InjectedElement element : elementsToIterate) { if (logger.isTraceEnabled()) { logger.trace("Processing injected element of bean ‘" + beanName + "‘: " + element); } element.inject(target, beanName, pvs); } } }
遍历上面获取到的element 对象,然后调用其inject 方法,可以理解为一种策略模式的实现,调用不同实现类的inject 方法。
AutowiredFieldElement 和 AutowiredMethodElement 源码如下:
private class AutowiredFieldElement extends InjectionMetadata.InjectedElement { private final boolean required; private volatile boolean cached = false; @Nullable private volatile Object cachedFieldValue; public AutowiredFieldElement(Field field, boolean required) { super(field, null); this.required = required; } @Override protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { Field field = (Field) this.member; Object value; if (this.cached) { value = resolvedCachedArgument(beanName, this.cachedFieldValue); } else { DependencyDescriptor desc = new DependencyDescriptor(field, this.required); desc.setContainingClass(bean.getClass()); Set<String> autowiredBeanNames = new LinkedHashSet<>(1); Assert.state(beanFactory != null, "No BeanFactory available"); TypeConverter typeConverter = beanFactory.getTypeConverter(); try { value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter); } catch (BeansException ex) { throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex); } synchronized (this) { if (!this.cached) { if (value != null || this.required) { this.cachedFieldValue = desc; registerDependentBeans(beanName, autowiredBeanNames); if (autowiredBeanNames.size() == 1) { String autowiredBeanName = autowiredBeanNames.iterator().next(); if (beanFactory.containsBean(autowiredBeanName) && beanFactory.isTypeMatch(autowiredBeanName, field.getType())) { this.cachedFieldValue = new ShortcutDependencyDescriptor( desc, autowiredBeanName, field.getType()); } } } else { this.cachedFieldValue = null; } this.cached = true; } } } if (value != null) { ReflectionUtils.makeAccessible(field); field.set(bean, value); } } } /** * Class representing injection information about an annotated method. */ private class AutowiredMethodElement extends InjectionMetadata.InjectedElement { private final boolean required; private volatile boolean cached = false; @Nullable private volatile Object[] cachedMethodArguments; public AutowiredMethodElement(Method method, boolean required, @Nullable PropertyDescriptor pd) { super(method, pd); this.required = required; } @Override protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { if (checkPropertySkipping(pvs)) { return; } Method method = (Method) this.member; Object[] arguments; if (this.cached) { // Shortcut for avoiding synchronization... arguments = resolveCachedArguments(beanName); } else { int argumentCount = method.getParameterCount(); arguments = new Object[argumentCount]; DependencyDescriptor[] descriptors = new DependencyDescriptor[argumentCount]; Set<String> autowiredBeans = new LinkedHashSet<>(argumentCount); Assert.state(beanFactory != null, "No BeanFactory available"); TypeConverter typeConverter = beanFactory.getTypeConverter(); for (int i = 0; i < arguments.length; i++) { MethodParameter methodParam = new MethodParameter(method, i); DependencyDescriptor currDesc = new DependencyDescriptor(methodParam, this.required); currDesc.setContainingClass(bean.getClass()); descriptors[i] = currDesc; try { Object arg = beanFactory.resolveDependency(currDesc, beanName, autowiredBeans, typeConverter); if (arg == null && !this.required) { arguments = null; break; } arguments[i] = arg; } catch (BeansException ex) { throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(methodParam), ex); } } synchronized (this) { if (!this.cached) { if (arguments != null) { DependencyDescriptor[] cachedMethodArguments = Arrays.copyOf(descriptors, arguments.length); registerDependentBeans(beanName, autowiredBeans); if (autowiredBeans.size() == argumentCount) { Iterator<String> it = autowiredBeans.iterator(); Class<?>[] paramTypes = method.getParameterTypes(); for (int i = 0; i < paramTypes.length; i++) { String autowiredBeanName = it.next(); if (beanFactory.containsBean(autowiredBeanName) && beanFactory.isTypeMatch(autowiredBeanName, paramTypes[i])) { cachedMethodArguments[i] = new ShortcutDependencyDescriptor( descriptors[i], autowiredBeanName, paramTypes[i]); } } } this.cachedMethodArguments = cachedMethodArguments; } else { this.cachedMethodArguments = null; } this.cached = true; } } } if (arguments != null) { try { ReflectionUtils.makeAccessible(method); method.invoke(bean, arguments); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } } } @Nullable private Object[] resolveCachedArguments(@Nullable String beanName) { Object[] cachedMethodArguments = this.cachedMethodArguments; if (cachedMethodArguments == null) { return null; } Object[] arguments = new Object[cachedMethodArguments.length]; for (int i = 0; i < arguments.length; i++) { arguments[i] = resolvedCachedArgument(beanName, cachedMethodArguments[i]); } return arguments; } }
3. 对于AutowiredFieldElement 对象的调用如下: (也就是前面三种属性的注入,是走的AutowiredFieldElement.inject 方法,该方法内部的重要的是先获取到依赖的bean,然后反射调用field.set(bean, value); 反射设置属性为获取到的bean)
(1) org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject 方法内部会调用org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency 进行注入,也就是上面的根据类型注入的过程:
public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException { descriptor.initParameterNameDiscovery(getParameterNameDiscoverer()); if (Optional.class == descriptor.getDependencyType()) { return createOptionalDependency(descriptor, requestingBeanName); } else if (ObjectFactory.class == descriptor.getDependencyType() || ObjectProvider.class == descriptor.getDependencyType()) { return new DependencyObjectProvider(descriptor, requestingBeanName); } else if (javaxInjectProviderClass == descriptor.getDependencyType()) { return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName); } else { Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary( descriptor, requestingBeanName); if (result == null) { result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter); } return result; } }
相关实参如下:
(2) 上面方法走 doResolveDependency 调用到org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency, 传递的参数如下:
@Nullable 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 = 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())); } } Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter); if (multipleBeans != null) { return multipleBeans; } Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor); if (matchingBeans.isEmpty()) { if (isRequired(descriptor)) { raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor); } return null; } String autowiredBeanName; Object instanceCandidate; 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. Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next(); autowiredBeanName = entry.getKey(); instanceCandidate = entry.getValue(); } if (autowiredBeanNames != null) { autowiredBeanNames.add(autowiredBeanName); } if (instanceCandidate instanceof Class) { instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this); } Object 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); } }
1》 Class<?> type = descriptor.getDependencyType(); 获取到需要注入的bean的类型
2》Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor); 根据descriptor 获取注解上的value 属性
3》value 如果是String 类型,也就是@Value 注入的属性,走resolveEmbeddedValue((String) value) 获取值,最终会调用到 org.springframework.core.env.PropertySourcesPropertyResolver#getPropertyAsRawString, 其实也就是从environment 环境中获取配置信息, 然后转换类型之后返回; 比如上面用@Value 注入Integer 类型的数据,实际就是先获取到String 类型的数据,然后用Convert 进行转换之后返回。
4》如果不是String类型,也就是@Autowired 注入的bean,走org.springframework.beans.factory.support.DefaultListableBeanFactory#findAutowireCandidates 获取到相关的bean,获取到之后进行返回
5》返回之后上层获取到相关bean,然后用反射设置字段的值
4. 对于AutowiredMethodElement 方法的inject方法分析:方setter法自动注入
其实这个和上面一样都是先org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency 获取bean,只是后续操作不同
1》method.getParameterCount(); 获取到参数个数
2》循环参数,然后调用org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency 这个获取bean。获取到之后加入到arguments 参数列表
3》走下面方法,反射调用方法进行setter 方法调用
ReflectionUtils.makeAccessible(method);
method.invoke(bean, arguments);
补充:关于Autowired 的自动注入的多个bean的解决办法
1. Autowired 默认是根据类型进行查找,如果根据type 找到多个满足条件的bean; 谈话autowired 默认会根据属性的名称进行匹配(用属性名称作为beanName 判断是否有名称一致的)。如果有则返回beanName 与 属性名称一致的,如果没有则报出异常
2. 也可以结合@Qualifier 注解限定名称进行匹配
3. 也可以对注入的bean 用 @Primary 进行声明,这样根据类型进行获取的时候只会获取到一个。
测试如下以及源码分析:
(1) 相关类:
package cn.qlq.dubbo.injecttest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Class1 { @Autowired private Interface1 interface1; }
package cn.qlq.dubbo.injecttest; public interface Interface1 { } package cn.qlq.dubbo.injecttest; import org.springframework.stereotype.Component; @Component public class InterfaceImpl1 implements Interface1{ } package cn.qlq.dubbo.injecttest; import org.springframework.stereotype.Component; @Component public class InterfaceImpl2 implements Interface1{ }
(2) 如上类启动会报错:
Description: Field interface1 in cn.qlq.dubbo.injecttest.Class1 required a single bean, but 2 were found: - interfaceImpl1: defined in file [E:\xiangmu\springcloud\dubbo-service-consumer\target\classes\cn\qlq\dubbo\injecttest\InterfaceImpl1.class] - interfaceImpl2: defined in file [E:\xiangmu\springcloud\dubbo-service-consumer\target\classes\cn\qlq\dubbo\injecttest\InterfaceImpl2.class]
查看源码
1》org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject 自动注入过程
2》上面调用org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency 获取依赖的bean
3》上面调用org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency:
@Nullable 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 = 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())); } } Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter); if (multipleBeans != null) { return multipleBeans; } Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor); if (matchingBeans.isEmpty()) { if (isRequired(descriptor)) { raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor); } return null; } String autowiredBeanName; Object instanceCandidate; 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. Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next(); autowiredBeanName = entry.getKey(); instanceCandidate = entry.getValue(); } if (autowiredBeanNames != null) { autowiredBeanNames.add(autowiredBeanName); } if (instanceCandidate instanceof Class) { instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this); } Object 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); } }
这里获取到的matchingBeans 如下:
然后size > 1, 会调用org.springframework.beans.factory.support.DefaultListableBeanFactory#determineAutowireCandidate 根据DependencyDescriptor 获取比较匹配的beanName:
protected String determineAutowireCandidate(Map<String, Object> candidates, DependencyDescriptor descriptor) { Class<?> requiredType = descriptor.getDependencyType(); String primaryCandidate = determinePrimaryCandidate(candidates, requiredType); if (primaryCandidate != null) { return primaryCandidate; } String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType); if (priorityCandidate != null) { return priorityCandidate; } // Fallback for (Map.Entry<String, Object> entry : candidates.entrySet()) { String candidateName = entry.getKey(); Object beanInstance = entry.getValue(); if ((beanInstance != null && this.resolvableDependencies.containsValue(beanInstance)) || matchesBeanName(candidateName, descriptor.getDependencyName())) { return candidateName; } } return null; }
可以看到这个方法里面简单的处理逻辑:第一步获取primary 声明的beanname; 如果没有获取到就用org.springframework.beans.factory.support.DefaultListableBeanFactory#matchesBeanName 方法找到匹配的beanname(也就是找到beanName或者bean的别名等于descriptor.getDependencyName() 的beanName)
如果上面找到了满足条件的一个beanName 就获取到bean 后返回,否则会报出上面的异常。
(3) 解决办法:
1》修改属性名称,属性名称也就是descriptor.getDependencyName()
package cn.qlq.dubbo.injecttest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Class1 { @Autowired private Interface1 interfaceImpl2; }
解释:
这时候会获取到的最匹配的beanName如下:(也就是属性名称作为beanName去匹配能找到匹配的beanName)
2》加@Qualifier 进行限定
package cn.qlq.dubbo.injecttest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Class1 { @Autowired @Qualifier("interfaceImpl2") private Interface1 interface1; }
源码解释:
加了@Qualifier 之后Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor); 会根据指定的beanName 进行获取,其调用链如下:
最终返回的就是@Qualifier 声明的beanName 以及 bean:
3》实现类加@Primary 进行声明
package cn.qlq.dubbo.injecttest; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component @Primary public class InterfaceImpl2 implements Interface1{ }
源码解释:
加了@Primary 注解,matchingBeans 返回的仍然是2个,只是determineAutowireCandidate 会先找primary 属性的bean
4》修改具体实现类注入到spring的beanName 和 属性名称,这个解决方案同1》
可以看到这个解决方案与自动注入的类型无关,属于对象工厂BeanFactory的一套机制。所以上面@Resource 自动注入过程调用此方法也是这样的逻辑。
总结:
1. Spring 自动注入的方式有: 构造注入、属性注入、setter 注入, 其注入过程也都在上面分析了。
(1) 构造注入的过程是用策略模式解析到需要注入的bean,然后反射创建构造函数
(2)属性注入和方法注入是在org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor#postProcessProperties 中开始注入过程,也是后置处理器。 @Resource 与@Autowired 注入分别发生在CommonAnnotationBeanPostProcessor 和 AutowiredAnnotationBeanPostProcessor, @Value 也发生在AutowiredAnnotationBeanPostProcessor
2. @Autowired 自动注入的过程如下:
(1)解析属性和方法中带@Autowired或者@Value 属性的,构造成Element 对象集合;
(2)遍历集合调用AutowiredFieldElement、AutowiredMethodElement 的inject 方法
(3)方法内部的核心操作是获取bean,然后反射设置属性或者调用方法
获取属性调用的是org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency。核心过程如下:
1》先根据类型获取到满足条件的bean,返回的bean存在Map<String, Object> matchingBeans 容器中
2》判断上面的matchingBeans 是否有元素
集合为空,报NoSuchBeanDefinitionException 异常
集合大小为1,返回找到的bean
集合大小大于1, 则有一套匹配beanName的规则:先获取primary声明的对象;找不到primary 声明的对象就根据属性name 和 当前的beanName进行匹配,满足则返回,不满足则报异常NoUniqueBeanDefinitionException。
3. Spring 中@Resource 发生注入的过程如下:
1》 注解上没带name 属性,会默认以属性名称作为beanName 去 容器中获取对象,如果获取到则直接返回;获取不到,也就是容器中不存在和属性名一致的bean,则走上面org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency 的规则,同@Autowired 后续一样。
2》注解上带name 属性,直接根据name 属性获取,获取不到则报错。
4. 总结就是:
@Autowired 先根据类型获取,获取到1个则直接注入;获取到多个再根据primary或者属性的名称与beanName 进行匹配。
@Resource 默认先根据属性名称获取,也就是先byName;然后byType,同上面@Autowired 机制一样。