Spring源码属性注入&@Autowired与@Resource注入原理 & 注入模式

  今天在研究代码的过程中发现@Value 注解也走的是@Autowired 自动注入的流程, 接下来研究@Autowired 和 @Resource 的逻辑。

1.  自动注入

  这里的自动注入说的是setter修饰的属性的自动注入,和@Autowired、@Resource 修饰的属性无关。两个的逻辑走的不是一套。

0. 前置

  在Spring 反射创建对象和属性注入过程中有个重要的后置处理器InstantiationAwareBeanPostProcessor, 可以理解为对象创建后置处理器。

package org.springframework.beans.factory.config;

import java.beans.PropertyDescriptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.lang.Nullable;

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
    // 反射创建对象前调用,调用时机是在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[]); 如果返回不为null, 则使用返回的对象作为容器生产的对象,也不会走该bean 的属性注入等过程
    @Nullable
    default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        return null;
    }

    // 反射创建完对象之后、属性注入 之前调用
    default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return true;
    }

    // 获取到需要注入的属性(@Autowired 和 @Resource 属性不在pvs 内部)之后、属性注入之前进行调用,  @Autowired 和 @Resource 是在该方法内进行注入
    @Nullable
    default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        return null;
    }

    // 同上面一样,加了属性描述符
    /** @deprecated */
    @Deprecated
    @Nullable
    default PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        return pvs;
    }
}

  这个后置处理器是在对象创建前后进行调用。 其逻辑可以理解为

1》对象创建前调用 postProcessBeforeInstantiation (相当于提供了自己创建对象的接口),返回不为null,则不走后续反射以及属性注入流程;

2》创建后调用populateBean 方法,方法内部主要逻辑是:

2.1》调用 InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation 处理对象创建后逻辑;

2.2》然后获取从beanDefinition的map 获取需要注入的属性,并且维护到PropertyValue 中

2.3》 调用 InstantiationAwareBeanPostProcessor.postProcessProperties 处理解析出的属性 (@Autowired 和 @Resource 是在这一步操作,其没有对PropertyValue  做处理,只是扫描相关注解反射进行注入 )

2.4》 调用 applyPropertyValues 方法 将上面的PropertyValue 进行反射 方法注入

1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[]) 这里是创建对象的开始

    protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
            throws BeanCreationException {

        if (logger.isTraceEnabled()) {
            logger.trace("Creating instance of bean '" + beanName + "'");
        }
        RootBeanDefinition mbdToUse = mbd;

        // Make sure bean class is actually resolved at this point, and
        // clone the bean definition in case of a dynamically resolved Class
        // which cannot be stored in the shared merged bean definition.
        Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
        if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
            mbdToUse = new RootBeanDefinition(mbd);
            mbdToUse.setBeanClass(resolvedClass);
        }

        // Prepare method overrides.
        try {
            mbdToUse.prepareMethodOverrides();
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                    beanName, "Validation of method overrides failed", ex);
        }

        try {
            // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
            Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
            if (bean != null) {
                return bean;
            }
        }
        catch (Throwable ex) {
            throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                    "BeanPostProcessor before instantiation of bean failed", ex);
        }

        try {
            Object beanInstance = doCreateBean(beanName, mbdToUse, args);
            if (logger.isTraceEnabled()) {
                logger.trace("Finished creating instance of bean '" + beanName + "'");
            }
            return beanInstance;
        }
        catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
            // A previously detected exception with proper bean creation context already,
            // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
            throw ex;
        }
        catch (Throwable ex) {
            throw new BeanCreationException(
                    mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
        }
    }

1》 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation

    protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
        Object bean = null;
        if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
            // Make sure bean class is actually resolved at this point.
            if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
                Class<?> targetType = determineTargetType(beanName, mbd);
                if (targetType != null) {
                    bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
                    if (bean != null) {
                        bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
                    }
                }
            }
            mbd.beforeInstantiationResolved = (bean != null);
        }
        return bean;
    }

    protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
                InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
                if (result != null) {
                    return result;
                }
            }
        }
        return null;
    }

    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor processor : getBeanPostProcessors()) {
            Object current = processor.postProcessAfterInitialization(result, beanName);
            if (current == null) {
                return result;
            }
            result = current;
        }
        return result;
    }

  这个就相当于可以通过org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation 手动根据类型与BeanName 返回一个对象; 返回之后跳过一些属性注入过程和初始化过程,直接调用org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization 初始化之后方法

2.  如果上面 InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation 没有手动创建对象就进行下面操作

1》org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean 走Spring 创建对象的生命周期

2》 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean:

    /**
     * Populate the bean instance in the given BeanWrapper with the property values
     * from the bean definition.
     * @param beanName the name of the bean
     * @param mbd the bean definition for the bean
     * @param bw the BeanWrapper with bean instance
     */
    @SuppressWarnings("deprecation")  // for postProcessPropertyValues
    protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
        // 检测 mbd 合法性
        if (bw == null) {
            if (mbd.hasPropertyValues()) {
                throw new BeanCreationException(
                        mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
            }
            else {
                // Skip property population phase for null instance.
                return;
            }
        }

        // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
        // state of the bean before properties are set. This can be used, for example,
        // to support styles of field injection.
        // 执行 InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation, 也就是对象创建完成之后的逻辑。
        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                    if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                        return;
                    }
                }
            }
        }

        PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
        // 进行自动注入,这里的自动注入是不在 org.springframework.beans.factory.support.AbstractBeanDefinition#propertyValues 维护的,也就是有setter 方法的。默认的注入模式是NO,现在只支持两种。BY_NAME 和 BY_TYPE
        int resolvedAutowireMode = mbd.getResolvedAutowireMode();
        if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
            MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
            // Add property values based on autowire by name if applicable.
            if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
                autowireByName(beanName, mbd, bw, newPvs);
            }
            // Add property values based on autowire by type if applicable.
            if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
                autowireByType(beanName, mbd, bw, newPvs);
            }
            pvs = newPvs;
        }

        boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
        boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);

        PropertyDescriptor[] filteredPds = null;
        if (hasInstAwareBpps) {
            if (pvs == null) {
                pvs = mbd.getPropertyValues();
            }
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                    // 处理属性,Autowired 和 Resource 在这一步进行处理
                    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;
                }
            }
        }
        if (needsDepCheck) {
            if (filteredPds == null) {
                filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
            }
            checkDependencies(beanName, mbd, filteredPds, pvs);
        }

        if (pvs != null) {
            // 应用解析出来的属性, 调用setter 方法进行自动注入
            applyPropertyValues(beanName, mbd, bw, pvs);
        }
    }
    
    protected void autowireByType(
            String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {

        TypeConverter converter = getCustomTypeConverter();
        if (converter == null) {
            converter = bw;
        }

        Set<String> autowiredBeanNames = new LinkedHashSet<>(4);
        String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
        for (String propertyName : propertyNames) {
            try {
                PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName);
                // Don't try autowiring by type for type Object: never makes sense,
                // even if it technically is a unsatisfied, non-simple property.
                if (Object.class != pd.getPropertyType()) {
                    MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
                    // Do not allow eager init for type matching in case of a prioritized post-processor.
                    boolean eager = !(bw.getWrappedInstance() instanceof PriorityOrdered);
                    DependencyDescriptor desc = new AutowireByTypeDependencyDescriptor(methodParam, eager);
                    Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter);
                    if (autowiredArgument != null) {
                        pvs.add(propertyName, autowiredArgument);
                    }
                    for (String autowiredBeanName : autowiredBeanNames) {
                        registerDependentBean(autowiredBeanName, beanName);
                        if (logger.isTraceEnabled()) {
                            logger.trace("Autowiring by type from bean name '" + beanName + "' via property '" +
                                    propertyName + "' to bean named '" + autowiredBeanName + "'");
                        }
                    }
                    autowiredBeanNames.clear();
                }
            }
            catch (BeansException ex) {
                throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, propertyName, ex);
            }
        }
    }

    protected void autowireByName(
            String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {

        String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
        for (String propertyName : propertyNames) {
            if (containsBean(propertyName)) {
                Object bean = getBean(propertyName);
                pvs.add(propertyName, bean);
                registerDependentBean(propertyName, beanName);
                if (logger.isTraceEnabled()) {
                    logger.trace("Added autowiring by name from bean name '" + beanName +
                            "' via property '" + propertyName + "' to bean named '" + propertyName + "'");
                }
            }
            else {
                if (logger.isTraceEnabled()) {
                    logger.trace("Not autowiring property '" + propertyName + "' of bean '" + beanName +
                            "' by name: no matching bean found");
                }
            }
        }
    }    
View Code

上面可以理解为两步重要步骤,

第一步是根据自动注入模式获取属性;(这里的属性不是@Autowired或者@Resource 指定的属性,是setter 方法修饰的普通属性)

第二步是InstantiationAwareBeanPostProcessor#postProcessProperties 处理属性 (@Autowired、@Resource、@Value 都是这一步完成的)

第三步:调用 applyPropertyValues 进行属性赋值

关于自动注入的模式有下面几种:

    // 不进行自动注入
    int AUTOWIRE_NO = 0;
    // 根据name去容器找
    int AUTOWIRE_BY_NAME = 1;
    // 根据类型去容器找
    int AUTOWIRE_BY_TYPE = 2;
    // 从构造函数进行注入
    int AUTOWIRE_CONSTRUCTOR = 3;
    /** @deprecated */
    // 自动发现
    @Deprecated
    int AUTOWIRE_AUTODETECT = 4;

  默认是AUTOWIRE_NO , 也就是认为属性不需要注入。AUTOWIRE_AUTODETECT 的机制是先找构造如果有参数为0的构造就走根据类型找, 如果不存在无参构造就走构造注入。org.springframework.beans.factory.support.AbstractBeanDefinition#getResolvedAutowireMode:

    public int getResolvedAutowireMode() {
        if (this.autowireMode == AUTOWIRE_AUTODETECT) {
            // Work out whether to apply setter autowiring or constructor autowiring.
            // If it has a no-arg constructor it's deemed to be setter autowiring,
            // otherwise we'll try constructor autowiring.
            Constructor<?>[] constructors = getBeanClass().getConstructors();
            for (Constructor<?> constructor : constructors) {
                if (constructor.getParameterCount() == 0) {
                    return AUTOWIRE_BY_TYPE;
                }
            }
            return AUTOWIRE_CONSTRUCTOR;
        }
        else {
            return this.autowireMode;
        }
    }

注意这个自动注入的模式是在BeanDefinition 中存的,如果通过@Bean 注入,可以手动指定:org.springframework.context.annotation.Bean 如下:

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {

    /**
     * Alias for {@link #name}.
     * <p>Intended to be used when no other attributes are needed, for example:
     * {@code @Bean("customBeanName")}.
     * @since 4.3.3
     * @see #name
     */
    @AliasFor("name")
    String[] value() default {};

    /**
     * The name of this bean, or if several names, a primary bean name plus aliases.
     * <p>If left unspecified, the name of the bean is the name of the annotated method.
     * If specified, the method name is ignored.
     * <p>The bean name and aliases may also be configured via the {@link #value}
     * attribute if no other attributes are declared.
     * @see #value
     */
    @AliasFor("value")
    String[] name() default {};

    /**
     * Are dependencies to be injected via convention-based autowiring by name or type?
     * <p>Note that this autowire mode is just about externally driven autowiring based
     * on bean property setter methods by convention, analogous to XML bean definitions.
     * <p>The default mode does allow for annotation-driven autowiring. "no" refers to
     * externally driven autowiring only, not affecting any autowiring demands that the
     * bean class itself expresses through annotations.
     * @see Autowire#BY_NAME
     * @see Autowire#BY_TYPE
     * @deprecated as of 5.1, since {@code @Bean} factory method argument resolution and
     * {@code @Autowired} processing supersede name/type-based bean property injection
     */
    @Deprecated
    Autowire autowire() default Autowire.NO;

    /**
     * Is this bean a candidate for getting autowired into some other bean?
     * <p>Default is {@code true}; set this to {@code false} for internal delegates
     * that are not meant to get in the way of beans of the same type in other places.
     * @since 5.1
     */
    boolean autowireCandidate() default true;

    /**
     * The optional name of a method to call on the bean instance during initialization.
     * Not commonly used, given that the method may be called programmatically directly
     * within the body of a Bean-annotated method.
     * <p>The default value is {@code ""}, indicating no init method to be called.
     * @see org.springframework.beans.factory.InitializingBean
     * @see org.springframework.context.ConfigurableApplicationContext#refresh()
     */
    String initMethod() default "";

    /**
     * The optional name of a method to call on the bean instance upon closing the
     * application context, for example a {@code close()} method on a JDBC
     * {@code DataSource} implementation, or a Hibernate {@code SessionFactory} object.
     * The method must have no arguments but may throw any exception.
     * <p>As a convenience to the user, the container will attempt to infer a destroy
     * method against an object returned from the {@code @Bean} method. For example, given
     * an {@code @Bean} method returning an Apache Commons DBCP {@code BasicDataSource},
     * the container will notice the {@code close()} method available on that object and
     * automatically register it as the {@code destroyMethod}. This 'destroy method
     * inference' is currently limited to detecting only public, no-arg methods named
     * 'close' or 'shutdown'. The method may be declared at any level of the inheritance
     * hierarchy and will be detected regardless of the return type of the {@code @Bean}
     * method (i.e., detection occurs reflectively against the bean instance itself at
     * creation time).
     * <p>To disable destroy method inference for a particular {@code @Bean}, specify an
     * empty string as the value, e.g. {@code @Bean(destroyMethod="")}. Note that the
     * {@link org.springframework.beans.factory.DisposableBean} callback interface will
     * nevertheless get detected and the corresponding destroy method invoked: In other
     * words, {@code destroyMethod=""} only affects custom close/shutdown methods and
     * {@link java.io.Closeable}/{@link java.lang.AutoCloseable} declared close methods.
     * <p>Note: Only invoked on beans whose lifecycle is under the full control of the
     * factory, which is always the case for singletons but not guaranteed for any
     * other scope.
     * @see org.springframework.beans.factory.DisposableBean
     * @see org.springframework.context.ConfigurableApplicationContext#close()
     */
    String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;

}
View Code

3. 测试:

package cn.qlq.dubbo.injecttest;

import org.springframework.beans.factory.annotation.Autowired;

public class Class1 {

    @Autowired
    private Class3 class3;

    private Class2 class2OldValue;

    private String name;

    public void setName(String name) {
        System.out.println("cn.qlq.dubbo.injecttest.Class1.setName\t" + name);
        this.name = name;
    }

    public void setClass2OldValue(Class2 class2) {
        System.out.println("cn.qlq.dubbo.injecttest.Class1.setClass2OldValue\t" + class2);
        this.class2OldValue = class2;
    }

    public void setClass4(Class4 class4) {
        System.out.println("cn.qlq.dubbo.injecttest.Class1.setClass4\t" + class4);
    }
}

动态注入Spring:

package cn.qlq.dubbo.injecttest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(Class1.class.getName());
        // 下面加了一个引用类型的注入,一个普通数据的注入
        beanDefinitionBuilder.addPropertyReference("class2OldValue", "class2"); // class2 是注入到容器中beanName
        beanDefinitionBuilder.addPropertyValue("name", "beanName-class1");
        // 剩余的属性按照类型自动注入(这个只限定class4 属性有效)
        beanDefinitionBuilder.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
        // 动态注册到容器
        beanDefinitionRegistry.registerBeanDefinition("class1", beanDefinitionBuilder.getBeanDefinition());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
    }
}

debug查看上面 populateBean 源码:

1》 pvs 如下: 这两个是beanDefinitionBuilder.addPropertyXXX 添加进来的

 2》autowireByType 方法内部获取到的propertyNames 属性如下

  然后解析到之后添加到上面的 pvs 属性中

 3》applyPropertyValues 方法参数上的psv如下: 这一步就是遍历这个属性集合,然后调用setter 方法设置值

 

  下面研究@Autowired和@Resource 的注入过程。

 

2. 前置理解

  属性注入开始时间是在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean 属性设置过程中。其利用的也是后置处理器,在populateBean方法中获取到所有的后置处理器,然后判断如果是 InstantiationAwareBeanPostProcessor 类型,则调用 postProcessProperties 进行属性注入。这需要用到 InstantiationAwareBeanPostProcessor 重要的后置处理器:

其实@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.
        }
    }

3. 测试

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======

4. 构造注入过程

  我们知道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);
    }
View Code

这里会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,这样完成构造注入。

5. 属性注入的过程

这里修改测试类,@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     }
View Code

  可以看到是利用反射遍历字段和方法,获取带@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);
        }
    }
View Code

 

核心逻辑如下:

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;
    }
View Code

 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);
    }
View Code

可以看到第一步是调用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;
        }
    }
View Code

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);
        }
    }
View Code

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 自动注入过程调用此方法也是这样的逻辑。  

 

总结:

0. 自动注入的模式有下面五种:

    // 不进行自动注入
    int AUTOWIRE_NO = 0;
    // 根据name去容器找
    int AUTOWIRE_BY_NAME = 1;
    // 根据类型去容器找
    int AUTOWIRE_BY_TYPE = 2;
    // 从构造函数进行注入
    int AUTOWIRE_CONSTRUCTOR = 3;
    /** @deprecated */
    // 自动发现
    @Deprecated
    int AUTOWIRE_AUTODETECT = 4;

  这里的自动注入说的是Spring 自动注入带setter 方法的属性,和@Autowired、@Resource 注解无关。 是说在属性注入 populateBean 方法中如何处理那些setter 方法扫描出来的bean, 默认是不进行注入。

1. Spring 自动注入的方式有: 构造注入、属性注入、setter 注入, 其注入过程也都在上面分析了。

(1) 构造注入的过程是用策略模式解析到需要注入的bean,然后反射创建构造函数。

(2)属性注入和方法注入是在IOC过程中的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean 方法

      注入是在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. 也可以向BeanDefinition内部维护的propertyValues 缓存中添加相关属性 

5. 总结就是:

@Autowired 先根据类型获取,获取到1个则直接注入;获取到多个再根据primary或者属性的名称与beanName 进行匹配。

@Resource 默认先根据属性名称获取,也就是先byName;然后byType,同上面@Autowired 机制一样。

补充:自动注入也可以注入数组、集合、Map ,注入的时候如果容器中没有相关对象会找默认的

1. 源码查看

自动注入相关属性:在扫描到自动注入注解之后会调用到 org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveMultipleBeans

    @Nullable
    private Object resolveMultipleBeans(DependencyDescriptor descriptor, @Nullable String beanName,
            @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) {

        final Class<?> type = descriptor.getDependencyType();

        if (descriptor instanceof StreamDependencyDescriptor) {
            Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
            if (autowiredBeanNames != null) {
                autowiredBeanNames.addAll(matchingBeans.keySet());
            }
            Stream<Object> stream = matchingBeans.keySet().stream()
                    .map(name -> descriptor.resolveCandidate(name, type, this))
                    .filter(bean -> !(bean instanceof NullBean));
            if (((StreamDependencyDescriptor) descriptor).isOrdered()) {
                stream = stream.sorted(adaptOrderComparator(matchingBeans));
            }
            return stream;
        }
        else if (type.isArray()) {
            Class<?> componentType = type.getComponentType();
            ResolvableType resolvableType = descriptor.getResolvableType();
            Class<?> resolvedArrayType = resolvableType.resolve(type);
            if (resolvedArrayType != type) {
                componentType = resolvableType.getComponentType().resolve();
            }
            if (componentType == null) {
                return null;
            }
            Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType,
                    new MultiElementDescriptor(descriptor));
            if (matchingBeans.isEmpty()) {
                return null;
            }
            if (autowiredBeanNames != null) {
                autowiredBeanNames.addAll(matchingBeans.keySet());
            }
            TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
            Object result = converter.convertIfNecessary(matchingBeans.values(), resolvedArrayType);
            if (result instanceof Object[]) {
                Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
                if (comparator != null) {
                    Arrays.sort((Object[]) result, comparator);
                }
            }
            return result;
        }
        else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
            Class<?> elementType = descriptor.getResolvableType().asCollection().resolveGeneric();
            if (elementType == null) {
                return null;
            }
            Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType,
                    new MultiElementDescriptor(descriptor));
            if (matchingBeans.isEmpty()) {
                return null;
            }
            if (autowiredBeanNames != null) {
                autowiredBeanNames.addAll(matchingBeans.keySet());
            }
            TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
            Object result = converter.convertIfNecessary(matchingBeans.values(), type);
            if (result instanceof List) {
                Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
                if (comparator != null) {
                    ((List<?>) result).sort(comparator);
                }
            }
            return result;
        }
        else if (Map.class == type) {
            ResolvableType mapType = descriptor.getResolvableType().asMap();
            Class<?> keyType = mapType.resolveGeneric(0);
            if (String.class != keyType) {
                return null;
            }
            Class<?> valueType = mapType.resolveGeneric(1);
            if (valueType == null) {
                return null;
            }
            Map<String, Object> matchingBeans = findAutowireCandidates(beanName, valueType,
                    new MultiElementDescriptor(descriptor));
            if (matchingBeans.isEmpty()) {
                return null;
            }
            if (autowiredBeanNames != null) {
                autowiredBeanNames.addAll(matchingBeans.keySet());
            }
            return matchingBeans;
        }
        else {
            return null;
        }
    }

  可以看到处理相关的集合数组和map。

2. 测试

接口:

package cn.qlq.dubbo.injecttest;
public interface Interface1 {
}

实现类1:

package cn.qlq.dubbo.injecttest;

import org.springframework.stereotype.Component;

@Component("myImpl1")
public class InterfaceImpl1 implements Interface1 {
}

实现类2:

package cn.qlq.dubbo.injecttest;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
@Primary
public class InterfaceImpl2 implements Interface1 {
}

测试类:

package cn.qlq.dubbo.injecttest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Component
public class Class1 {


    @Autowired
    public void setList(List<Interface1> interface1s) {
        System.out.println("******");
        interface1s.forEach(System.out::println);
        System.out.println("******");
    }

    @Autowired
    public void setArray(Interface1[] interface1s) {
        System.out.println("000000");
        Arrays.stream(interface1s).forEach(System.out::println);
        System.out.println("000000");
    }

    @Autowired
    public void setMap(Map<String, Interface1> map) {
        System.out.println("111111");
        map.forEach((k, v) -> {
            System.out.println(k + "\t" + v);
        });
        System.out.println("111111");
    }
}

结果:

000000
cn.qlq.dubbo.injecttest.InterfaceImpl1@5e99b9c
cn.qlq.dubbo.injecttest.InterfaceImpl2@2fe74516
000000
111111
myImpl1    cn.qlq.dubbo.injecttest.InterfaceImpl1@5e99b9c
interfaceImpl2    cn.qlq.dubbo.injecttest.InterfaceImpl2@2fe74516
111111
******
cn.qlq.dubbo.injecttest.InterfaceImpl1@5e99b9c
cn.qlq.dubbo.injecttest.InterfaceImpl2@2fe74516
******

补充:关于lazy 解决循环依赖的思路

  lazy 可以解决循环依赖的错误,其原理是在获取依赖注入过程中,代码走如下代码:

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(this.getParameterNameDiscoverer());
        if (Optional.class == descriptor.getDependencyType()) {
            return this.createOptionalDependency(descriptor, requestingBeanName);
        } else if (ObjectFactory.class != descriptor.getDependencyType() && ObjectProvider.class != descriptor.getDependencyType()) {
            if (javaxInjectProviderClass == descriptor.getDependencyType()) {
                return (new DefaultListableBeanFactory.Jsr330Factory()).createDependencyProvider(descriptor, requestingBeanName);
            } else {
                Object result = this.getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, requestingBeanName);
                if (result == null) {
                    result = this.doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
                }

                return result;
            }
        } else {
            return new DefaultListableBeanFactory.DependencyObjectProvider(descriptor, requestingBeanName);
        }
    }

代码会走 getLazyResolutionProxyIfNecessary 获取lazy 修饰的代理对象,如果返回Wienull就从容器找对象。

调用到: org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver#getLazyResolutionProxyIfNecessary 拿lazy 修饰的代理对象

    public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, @Nullable String beanName) {
        return (isLazy(descriptor) ? buildLazyResolutionProxy(descriptor, beanName) : null);
    }

    protected boolean isLazy(DependencyDescriptor descriptor) {
        for (Annotation ann : descriptor.getAnnotations()) {
            Lazy lazy = AnnotationUtils.getAnnotation(ann, Lazy.class);
            if (lazy != null && lazy.value()) {
                return true;
            }
        }
        MethodParameter methodParam = descriptor.getMethodParameter();
        if (methodParam != null) {
            Method method = methodParam.getMethod();
            if (method == null || void.class == method.getReturnType()) {
                Lazy lazy = AnnotationUtils.getAnnotation(methodParam.getAnnotatedElement(), Lazy.class);
                if (lazy != null && lazy.value()) {
                    return true;
                }
            }
        }
        return false;
    }

    protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final @Nullable String beanName) {
        BeanFactory beanFactory = getBeanFactory();
        Assert.state(beanFactory instanceof DefaultListableBeanFactory,
                "BeanFactory needs to be a DefaultListableBeanFactory");
        final DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;

        TargetSource ts = new TargetSource() {
            @Override
            public Class<?> getTargetClass() {
                return descriptor.getDependencyType();
            }
            @Override
            public boolean isStatic() {
                return false;
            }
            @Override
            public Object getTarget() {
                Set<String> autowiredBeanNames = (beanName != null ? new LinkedHashSet<>(1) : null);
                Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null);
                if (target == null) {
                    Class<?> type = getTargetClass();
                    if (Map.class == type) {
                        return Collections.emptyMap();
                    }
                    else if (List.class == type) {
                        return Collections.emptyList();
                    }
                    else if (Set.class == type || Collection.class == type) {
                        return Collections.emptySet();
                    }
                    throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(),
                            "Optional dependency not present for lazy injection point");
                }
                if (autowiredBeanNames != null) {
                    for (String autowiredBeanName : autowiredBeanNames) {
                        if (dlbf.containsBean(autowiredBeanName)) {
                            dlbf.registerDependentBean(autowiredBeanName, beanName);
                        }
                    }
                }
                return target;
            }
            @Override
            public void releaseTarget(Object target) {
            }
        };

        ProxyFactory pf = new ProxyFactory();
        pf.setTargetSource(ts);
        Class<?> dependencyType = descriptor.getDependencyType();
        if (dependencyType.isInterface()) {
            pf.addInterface(dependencyType);
        }
        return pf.getProxy(dlbf.getBeanClassLoader());
    }

  可以看到会判断是否有注解Lazy, 如果有相关注解的话先返回一个代理对象。

 

posted @ 2021-08-22 00:06  QiaoZhi  阅读(1069)  评论(0编辑  收藏  举报