Spring

Spring-IOC-FactoryBean检测与获取Bean

2017-09-04  本文已影响45人  zhanglbjames

主要讲解getObjectForInstance方法,对FactoryBean验证,并获取需要的Bean实例。

主要步骤:

  1. 对FactoryBean正确性的验证

beanName含有 &前缀,但是又不是FactoryBean,则验证不通过并抛出异常。

  1. 对非FactoryBean或者beanName含有&前缀的FactoryBean则不做任何处理,直接返回。
  2. 如果是beanName不含有&前缀的FactoryBean,则先尝试从factoryBeanObjectCache中获取由Factory创建的bean。

factoryBeanObjectCache用于存放被FactoryBean创建的单例bean对象,保存的映射为 FactoryBean -> object。


  1. factoryBeanObjectCache中不存在,则将FactoryBean中获取bean的工作委托给getObjectFormFactoryBean方法(主要是调用FactoryBean.getObject()方法)。
FactoryBean检测与获取Bean
beans.factory.support.AbstractBeanFactory(XmlBeanFactory 继承自这个类,拥有这个方法)
    /**
     * Get the object for the given bean instance, either the bean
     * instance itself or its created object in case of a FactoryBean.
     * @param beanInstance the shared bean instance
     * @param name name that may include factory dereference prefix
     * @param beanName the canonical bean name
     * @param mbd the merged bean definition
     * @return the object to expose for the bean
     */
    protected Object getObjectForBeanInstance(
            Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {

        //1. Don't let calling code try to dereference the factory if the bean isn't a factory.
        if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
            throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
        }

        //2. 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.
        if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
            return beanInstance;
        }

        // 3.
        Object object = null;
        if (mbd == null) {
            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());
            // 4. 
            object = getObjectFromFactoryBean(factory, beanName, !synthetic);
        }
        return object;
    }

接下来主要看一下第四步是如何操作的?

beans.factory.support.FactoryBeanRegistrySupport(XmlBeanFactory 继承自这个类,拥有这个方法)
    /**
     * Obtain an object to expose from the given FactoryBean.
     * @param factory the FactoryBean instance
     * @param beanName the name of the bean
     * @param shouldPostProcess whether the bean is subject to post-processing
     * @return the object obtained from the FactoryBean
     * @throws BeanCreationException if FactoryBean object creation failed
     * @see org.springframework.beans.factory.FactoryBean#getObject()
     */
    protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
        if (factory.isSingleton() && containsSingleton(beanName)) {
            synchronized (getSingletonMutex()) {
                Object object = this.factoryBeanObjectCache.get(beanName);
                if (object == null) {
                    object = doGetObjectFromFactoryBean(factory, beanName);
                    // Only post-process and store if not put there already during getObject() call above
                    // (e.g. because of circular reference processing triggered by custom getBean calls)
                    Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
                    if (alreadyThere != null) {
                        object = alreadyThere;
                    }
                    else {
                        if (object != null && shouldPostProcess) {
                            try {
                                object = postProcessObjectFromFactoryBean(object, beanName);
                            }
                            catch (Throwable ex) {
                                throw new BeanCreationException(beanName,
                                        "Post-processing of FactoryBean's singleton object failed", ex);
                            }
                        }
                        this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT));
                    }
                }
                return (object != NULL_OBJECT ? object : null);
            }
        }
        else {
            Object object = doGetObjectFromFactoryBean(factory, beanName);
            if (object != null && shouldPostProcess) {
                try {
                    object = postProcessObjectFromFactoryBean(object, beanName);
                }
                catch (Throwable ex) {
                    throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
                }
            }
            return object;
        }
    }
    /**
     * Obtain an object to expose from the given FactoryBean.
     * @param factory the FactoryBean instance
     * @param beanName the name of the bean
     * @return the object obtained from the FactoryBean
     * @throws BeanCreationException if FactoryBean object creation failed
     * @see org.springframework.beans.factory.FactoryBean#getObject()
     */
    private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)
            throws BeanCreationException {

        Object object;
        try {
            if (System.getSecurityManager() != null) {
                AccessControlContext acc = getAccessControlContext();
                try {
                    object = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                        @Override
                        public Object run() throws Exception {
                                return factory.getObject();
                            }
                        }, acc);
                }
                catch (PrivilegedActionException pae) {
                    throw pae.getException();
                }
            }
            else {
                object = factory.getObject();
            }
        }
        catch (FactoryBeanNotInitializedException ex) {
            throw new BeanCurrentlyInCreationException(beanName, ex.toString());
        }
        catch (Throwable ex) {
            throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
        }

        // Do not accept a null value for a FactoryBean that's not fully
        // initialized yet: Many FactoryBeans just return null then.
        if (object == null && isSingletonCurrentlyInCreation(beanName)) {
            throw new BeanCurrentlyInCreationException(
                    beanName, "FactoryBean which is currently in creation returned null from getObject");
        }
        return object;
    }
  1. getObjectFromFactoryBean方法内获取单例独占锁,是为了保证单例在容器中的全局唯一性,避免重复创建,使用缓存提高性能,也就是已经加载过则记录复用,下次从factoryBeanObjectCache缓存中获取,否则的话就调用getObject直接获取了。

  2. doGetObjectFromFactoryBean中最核心的是调用FactoryBean的getObject方法获取bean。

  3. getObjectFromFactoryBean方法内,并没有直接返回获取的bean,而是更根据情况做了一些后处理操作。

下面来看一下从FactoryBean中获取Bean对象的后处理方法:
初始化后处理(XmlBeanFactory 继承自这个类,拥有这个方法)
beans.factory.support.AbstractAutowireCapableBeanFactory
    /**
     * Applies the {@code postProcessAfterInitialization} callback of all
     * registered BeanPostProcessors, giving them a chance to post-process the
     * object obtained from FactoryBeans (for example, to auto-proxy them).
     * @see #applyBeanPostProcessorsAfterInitialization
     */
    @Override
    protected Object postProcessObjectFromFactoryBean(Object object, String beanName) {
        return applyBeanPostProcessorsAfterInitialization(object, beanName);
    }
    @Override
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
            throws BeansException {

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

通过迭代获取所有BeanPostProcessor后处理器,对获取的对象进行后处理。注意这里调用的是后处理器的postProcessAfterInitialization方法,即初始化后处理方法

FactoryBean是什么,有什么用处?

<bean>标签 的class属性如果配置为FactoryBean时,通过getBean()方法返回的不是FactoryBean本身,而是FactoryBean#getObject()方法返回的对象,这主要是Spring通过反射机制检查配置的class属性配置的类是否实现了FactoryBean接口,如果实现了这个接口则Spring就会调用其getObject方法。

如果希望返回FactoryBean实现类本身,则需要在getBean(beanName)中的beanName前显示的加上"&"前缀,比如getBean("&aBeanName")

FactoryBean的妙用

在自定义Bean的类定义中继承FactoryBean接口,然后实现getObject接口方法,可以实现自定义的Bean的配置,比如在bean配置文件中,多个<property>子标签配置bean的属性值,可以在getObject方法中通过csv的字符串格式(对属性分隔表示)来配置创建出来的Bean的属性。

和普通Bean的区别

普通Bean在BeanFactory中是通过getBean方法获取的,而FactoryBean则是BeanFactory#getBean获取FactoryBean,如果想获取FactoryBean工厂方法返回的Bean,则需调用FactoryBean的getObject方法创。

上一篇下一篇

猜你喜欢

热点阅读