doCreateBean - populateBean

属性扫描完成之后, 就可以开始属性注入了.

代码块:

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

boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
        isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
    if (logger.isTraceEnabled()) {
        logger.trace("Eagerly caching bean '" + beanName +
                "' to allow for resolving potential circular references");
    }
    //这里创建了一个匿名的 ObjectFactory 实现类, 他是一个工厂, 可以用来获取对象
    //addSingletonFactory中, 将这个工厂放到 singletonFactories 中去了. singletonFactories 是spring的三级缓存
    addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}

// Initialize the bean instance.
Object exposedObject = bean;
try {
    //设置属性,非常重要
    populateBean(beanName, mbd, instanceWrapper);
    //执行后置处理器,aop就是在这里完成的处理
    exposedObject = initializeBean(beanName, exposedObject, mbd);
}

这里只看 populateBean() 方法

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

protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
    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.
    // 需不需要spring来设置属性
    // 实例化之后的 bean的后置处理器
    // org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation
    if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
                InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                //如果返回为 false, 则会终止属性注入
                if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                    return;
                }
            }
        }
    }

    PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);

    int resolvedAutowireMode = mbd.getResolvedAutowireMode();
    //如果给一个类设置了 : AUTOWIRE_BY_NAME 和 AUTOWIRE_BY_TYPE, 那么类中的属性, 会根据规则自动注入, 而不需要@Autowired或@Resource了
    //默认情况下, 是 AUTOWIRE_NO, 所以这里默认是不执行
    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;
                //这里也是调用的实例化后的后置处理器, 只是调用的方法不一样
                PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
                if (pvsToUse == null) {
                    if (filteredPds == null) {
                        filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
                    }
            //这里的 postProcessPropertyValues 是一个过时方法 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) { applyPropertyValues(beanName, mbd, bw, pvs); } }

上面的注释里面说到, spring默认使用 AUTOWIRE_NO 模式, 那属性注入方法  autowireByName 和 autowireByType 都没进去.

也没看到别的属性注入方法了, 那么, 这里是怎么注入的呢?

要想弄明白这个问题, 得先弄明白里面调用了两次后置处理器的方法, 都干了些什么.

 

postProcessAfterInstantiation

https://www.cnblogs.com/elvinle/p/13384114.html

 

postProcessProperties

 https://www.cnblogs.com/elvinle/p/13384328.html

postProcessProperties 就是进行 @Autowired 和 @Resource 注入的.

注意到这里, AUTOWIRE_BY_NAME  和 AUTOWIRE_BY_TYPE 是在 他们之前 执行的 .

 

你可能感兴趣的:(doCreateBean - populateBean)