【spring原始碼系列】之【FactoryBean型別的介面】

小豬爸爸發表於2022-06-12

1.概述

目前我們知道,spring建立bean有多種方式,比如xml方式建立,比如@Component,@Service,@Controler,@Repository註解建立,比如@Autowired依賴注入建立,後續還有通過springboot方式的配置註解@Configuration與@Bean方式結合建立,這裡不一一介紹,等分析spring boot原始碼的時候再做總結。

就spring本身,提供了一種介面方式建立bean,就是本節要討論的通過FactoryBean介面方式建立。

2.例項

FactoryBean介面的實現類FactoryBeanDemo:

package com.wzj.FactoryBean;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Service;

@Service
public class FactoryBeanDemo implements FactoryBean {

    @Override
    public Object getObject() throws Exception {
        return new FactoryB();
    }

    @Override
    public Class<?> getObjectType() {
        return FactoryB.class;
    }
}

通過FactoryBean實現類,完成自定義類FactoryB的例項化,FactoryB:

package com.wzj.FactoryBean;

import lombok.Data;

@Data
public class FactoryB {
    private String name = "wzj";
}

測試類:

public class TestSpring {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void testFactoryBean() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        FactoryB factoryB = (FactoryB)applicationContext.getBean("factoryBeanDemo");
        System.out.println(factoryB);

        FactoryBeanDemo factoryBeanDemo = (FactoryBeanDemo)applicationContext.getBean("&factoryBeanDemo");
        System.out.println(factoryBeanDemo);
    }

測試結果:

可以看出,當獲取名稱為factoryBeanDemo的例項時,得到的是getObject()方法裡建立的FactoryB型別的物件,而獲取加字首&factoryBeanDemo的例項時,得到的是FactoryBeanDemo本身的例項。

3.原始碼

step1: FactoryBean 介面的呼叫入口在例項化和 IOC/DI 做完後,就會呼叫 FactoryBean 型別的介面如下圖所示

				// Create bean instance.
				// 建立bean例項
				if (mbd.isSingleton()) {
					sharedInstance = getSingleton(beanName, () -> {
						try {
							return createBean(beanName, mbd, args);
						}
						catch (BeansException ex) {
							// Explicitly remove instance from singleton cache: It might have been put there
							// eagerly by the creation process, to allow for circular reference resolution.
							// Also remove any beans that received a temporary reference to the bean.
							destroySingleton(beanName);
							throw ex;
						}
					});
					// FactoryBean的呼叫入口
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
				}

step2: 如果要獲取到 FactoryBean 類本身,就必須加上&符號,比如 beanFactory.getBean("&beanName") ,如下:

	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.
		// 如果為name不為空,且以字首&打頭,直接返回bean本身
		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.
		if (!(beanInstance instanceof FactoryBean)) {
			return beanInstance;
		}

		
	public static boolean isFactoryDereference(@Nullable String name) {
		return (name != null && name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));
	}
	String FACTORY_BEAN_PREFIX = "&";

stet3: BeanFactory.getBean("beanName")只能獲取到 getObject()方法返回的例項。getObject 方法返回的例項會有單獨的快取儲存,跟其他例項不是同一個快取,對應的快取是:factoryBeanObjectCache

                // 如果是不是以字首&打頭,並且是FactoryBean型別的
		Object object = null;
		if (mbd != null) {
			mbd.isFactoryBean = true;
		}
		else {
			// 從快取裡拿FactoryBean例項
			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;
	}
	protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
		if (factory.isSingleton() && containsSingleton(beanName)) {
			synchronized (getSingletonMutex()) {
				// 先從快取factoryBeanObjectCache取
				Object object = this.factoryBeanObjectCache.get(beanName);
				// 如果快取為空,
				if (object == null) {
					// 呼叫getObject方法
					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 = 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)) {
							// 最後放到快取中
							this.factoryBeanObjectCache.put(beanName, object);
						}
					}
				}
				return object;
			}
		}
                ......

小結:具體程式碼參考 getSingleton 方法之後 getObjectForBeanInstance

  • 如果bean例項不是 FactoryBean 型別的或者 name 以&開始的則直接返回例項。
  • 如果bean是 FacotyBean 並且不是以&開頭, 會通過方法doGetObjectFromFactoryBean 呼叫FactoryBean 內部繼承實現的 getObject 方法,並且判斷一級快取中如果存在該 bean 例項把例項快取到factoryBeanObjectCache 對應的 map 中,這個是單獨快取 FactoryBean 型別例項的 map。

4.總結

靈活建立所需例項物件的時候,通過實現FactoryBean介面的getObject方法定義例項化過程。

比如MyBatis提供mybatis-spring專案中的 org.mybatis.spring.SqlSessionFactoryBean

public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
	// ...省略其他程式碼
	
	public SqlSessionFactory getObject() throws Exception {
	if (this.sqlSessionFactory == null) {
	  afterPropertiesSet();
	}

	return this.sqlSessionFactory;
	}
}

sqlSessionFactory是SqlSessionFactoryBean的一個屬性,它的賦值是在通過回撥afterPropertiesSet()方法進行的。 因為SqlSessionFactoryBean實現了InitializingBean介面,所以在Spring初始化Bean的時候,能回撥afterPropertiesSet()方法。

public void afterPropertiesSet() throws Exception {
    // buildSqlSessionFactory()方法會根據mybatis的配置進行初始化。
	this.sqlSessionFactory = buildSqlSessionFactory();
}

在上面的afterPropertiesSet()方法中,buildSqlSessionFactory()方法會根據mybatis的配置,完成客戶所需要的的sessionFactory的初始化。

相關文章