美文网首页
2021-11-28 Spring源码中的deference是什

2021-11-28 Spring源码中的deference是什

作者: 归去来ming | 来源:发表于2021-11-28 23:15 被阅读0次
    // BeanFactory接口
    /**
    Used to dereference a FactoryBean instance and distinguish it from beans created by the FactoryBean. For example, if the bean named myJndiObject is a FactoryBean, getting &myJndiObject will return the factory, not the instance returned by the factory.
    */
    String FACTORY_BEAN_PREFIX = "&";
    

    看来说的就是这个&。继续看AbstractBeanFactory类中的getObjectForBeanInstance方法:

    protected Object getObjectForBeanInstance(
                Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
    
        // Don't let calling code try to dereference the factory if the bean isn't a factory.
        // 1,name带&,beanInstance是FactoryBean,说明这是要获取FactoryBean本身的实例
        if (BeanFactoryUtils.isFactoryDereference(name)) {
            if (beanInstance instanceof NullBean) {
                return beanInstance;
            }
            if (!(beanInstance instanceof FactoryBean)) {
                throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
            }
            if (mbd != null) {
                mbd.isFactoryBean = true;
            }
            return beanInstance;
        }
    
        // Now we have the bean instance, which may be a normal bean or a FactoryBean.
        // If it's a FactoryBean, we use it to create a bean instance, unless the
        // caller actually wants a reference to the factory.
        // 2,name不带&,beanInstance不是FactoryBean,说明是一个普通的bean
        if (!(beanInstance instanceof FactoryBean)) {
            return beanInstance;
        }
    
        // 3,name不带&,beanInstance是FactoryBean,说明需要FactoryBean去创建name的实例
        Object object = null;
        if (mbd != null) {
            mbd.isFactoryBean = true;
        }
        else {
            object = getCachedObjectForFactoryBean(beanName);
        }
        if (object == null) {
            // Return bean instance from factory.
            FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
            // Caches object obtained from FactoryBean if it is a singleton.
            if (mbd == null && containsBeanDefinition(beanName)) {
                mbd = getMergedLocalBeanDefinition(beanName);
            }
            boolean synthetic = (mbd != null && mbd.isSynthetic());
            object = getObjectFromFactoryBean(factory, beanName, !synthetic);
        }
        return object;
    }
    

    进到BeanFactoryUtils的isFactoryDereference方法里:

    /**
    Return whether the given name is a factory dereference (beginning with the factory dereference prefix).
    Params:
    name – the name of the bean
    Returns:
    whether the given name is a factory dereference
    See Also:
    BeanFactory.FACTORY_BEAN_PREFIX
    */
    public static boolean isFactoryDereference(@Nullable String name) {
        return (name != null && name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));
    }
    

    从这个方法也进一步证实,deference说的就是这个&。

    相关文章

      网友评论

          本文标题:2021-11-28 Spring源码中的deference是什

          本文链接:https://www.haomeiwen.com/subject/tlckxrtx.html