FactoryBean源码解析

FactoryBean和BeanFactory的区别,不是本文重点。阅读本文,需要对spring创建bean的流程比较熟悉

FactoryBean:和普通的bean没啥区别,由Spring管理它的生命周期。即由spring实例化,初始化,调用那些后置处理器。反正FactoryBean本身的创建,和普通bean没啥区别。唯一有区别的地方在FactoryBean的接口方法getObject上。我们在使用FactoryBean的时候,其实我们真正想要的bean是getObject返回的对象。这个对象,我们可以自己实现自定义的实例化,初始化等等操作。但是getObject是延迟加载的,只有显示调用才能触发,spring并不会触发(SmartFactoryBean除外)。

BeanFactory:bean工厂,bean容器等等。和FactoryBean完全是两个维度,不在过多介绍了。

附上使用案例:

@Component
public class FactoryBeanTest implements FactoryBean {

    public FactoryBeanTest(){
        System.out.println("我是FactoryBeanTest的构造方法");
    }

    @Override
    public A getObject() throws Exception {
        System.out.println("开始实例化A");
        return new A();
    }

    @Override
    public Class getObjectType() {
        return A.class;
    }
}

统一下概念:
FactoryBean本身:就是指FactoryBeanTest。
FactoryBean对象:就是指getObject生成的A对象。

下面正式进入FactoryBean的世界。

一、 FactoryBean本身创建过程

其实和普通bean的创建主流程没啥区别,但是还是有些小区别需要提一下。

进入preInstantiateSingletons方法看下:

@Override
    public void preInstantiateSingletons() throws BeansException {
        if (logger.isTraceEnabled()) {
            logger.trace("Pre-instantiating singletons in " + this);
        }

        // Iterate over a copy to allow for init methods which in turn register new bean definitions.
        // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
        List beanNames = new ArrayList<>(this.beanDefinitionNames);

        // Trigger initialization of all non-lazy singleton beans...
        for (String beanName : beanNames) {
            RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
                if (isFactoryBean(beanName)) {
                    Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                    if (bean instanceof FactoryBean) {
                        final FactoryBean factory = (FactoryBean) bean;
                        boolean isEagerInit;
                        if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                            isEagerInit = AccessController.doPrivileged((PrivilegedAction)
                                            ((SmartFactoryBean) factory)::isEagerInit,
                                    getAccessControlContext());
                        }
                        else {
                            isEagerInit = (factory instanceof SmartFactoryBean &&
                                    ((SmartFactoryBean) factory).isEagerInit());
                        }
                        if (isEagerInit) {
                            getBean(beanName);
                        }
                    }
                }
                else {
                    getBean(beanName);
                }
            }
        }
 
····· //省略

FactoryBean本身的创建会被if(isFactoryBean(beanName))判断为true,这里的最大区别就是

Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);

FactoryBean本身的创建,会添加一个&符号。这个符号,会在doGetBean方法里面被特殊处理。
下面的代码就是判断isEagerInit是不是为true,通过SmartFactoryBean可以指定isEagerInit为true,默认为false。这里纯粹是为了判断是否要提前调用getObject而已。

二、getBean(&beanName)特殊处理&符号

说特殊处理其实也没特殊处理,就在方法transformedBeanName:

protected String transformedBeanName(String name) {
        return canonicalName(BeanFactoryUtils.transformedBeanName(name));
    }

它会判断bean是否以&开始,然后去掉&。剩下的就是按照普通bean的创建流程一样,走到doCreateBean等等,不在赘述。

现在的问题是,FactoryBean本身由Spring创建完成了,那么怎么拿到bean呢?

这样分两种情况:
1、拿FactoryBean本身,通过getBean(&beanName)
2、拿FactoryBean的getObject对象,通过getBean(beanName)

我们来详细分析下

对于第一种情况:getBean(&beanName)

protected  T doGetBean(final String name, @Nullable final Class requiredType,
            @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {

        final String beanName = transformedBeanName(name);
        Object bean;

        // Eagerly check singleton cache for manually registered singletons.
        Object sharedInstance = getSingleton(beanName);
        if (sharedInstance != null && args == null) {
            if (logger.isTraceEnabled()) {
                if (isSingletonCurrentlyInCreation(beanName)) {
                    logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
                            "' that is not fully initialized yet - a consequence of a circular reference");
                }
                else {
                    logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
                }
            }
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }
···· //省略

&beanName会被去掉&变成beanName。然后getSingleton肯定会有对象,因为FactoryBean本身被spring创建放入到单例池(一级缓存)中。然后最关键的是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.
        if (BeanFactoryUtils.isFactoryDereference(name)) {
            if (beanInstance instanceof NullBean) {
                return beanInstance;
            }
            if (!(beanInstance instanceof FactoryBean)) {
                throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
            }
        }

        // 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.

        //  如果不是FactoryBean,或者 name以&开头。就把单例池中的FactoryBean本身返回出去。
        if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
            return beanInstance;
        }

        Object object = null;
        if (mbd == null) {
          //如果是FactoryBean,且要拿getObject的对象,那么先从缓存factoryBeanObjectCache中拿。它里面放的都是FactoryBean对应的getObject对象。
            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());
                  //缓存池拿不到,那怎么办?很简单啊,调用FactoryBean的getObject方法呀!!!
            object = getObjectFromFactoryBean(factory, beanName, !synthetic);
        }
        return object;
    }

我们猜想getObjectFromFactoryBean方法会调用FactoryBean的getObject方法,并且会把得到的object放入factoryBeanObjectCache缓存中。下面来验证:

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 = factory.getObject()得到真实的object
                    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 (shouldPostProcess) {
                            if (isSingletonCurrentlyInCreation(beanName)) {
                                // Temporarily return non-post-processed object, not storing it yet..
                                return object;
                            }
                            beforeSingletonCreation(beanName);
                            try {
              //调用后置处理器,毕竟真实的object也是bean,也需要执行初始化后置处理器。
                                object = postProcessObjectFromFactoryBean(object, beanName);
                            }
                            catch (Throwable ex) {
                                throw new BeanCreationException(beanName,
                                        "Post-processing of FactoryBean's singleton object failed", ex);
                            }
                            finally {
                                afterSingletonCreation(beanName);
                            }
                        }
                        if (containsSingleton(beanName)) {
                //添加到factoryBeanObjectCache缓存中
                            this.factoryBeanObjectCache.put(beanName, object);
                        }
                    }
                }
                return object;
            }
        }
        else {
        //非单例模式,调用完getObject,无需添加到缓存中
            Object object = doGetObjectFromFactoryBean(factory, beanName);
            if (shouldPostProcess) {
                try {
                    object = postProcessObjectFromFactoryBean(object, beanName);
                }
                catch (Throwable ex) {
                    throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
                }
            }
            return object;
        }
    }

一路分析下来,发现FactoryBean还挺灵活。它可以创建并初始化任意类型的对象,创建的具体过程由程序员掌控,不用交给spring。但是,创建完成的bean,还是托管给了spring容器,满足IOC。

你可能感兴趣的:(FactoryBean源码解析)