(一)Spring IoC原始碼-2.bean的載入-03從FactoryBean例項中獲取目標例項
無論是已經載入到了單例bean還是建立bean後,都需要通過bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
從bean例項中獲取目標物件。
無論是從快取中獲取到的bean還是通過不同的scope策略建立的bean都只是最原始的bean狀態,並不一定是我們最終想要的bean。舉個例子,假如我們需要對工廠bean,即FactoryBean例項進行處理,那麼這裡得到的其實是工廠bean的初始狀態,但我們真正想要的是工廠bean中定義的factory-method方法中返回的bean。getObjectForBeanInstance方法就是完成這個工作的。
@Nullable
protected Object getObjectForBeanInstance(
@Nullable Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
if (beanInstance == null) {
return null;
}
// 驗證引數是否合法
if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
}
//如果不是bean例項不是FactoryBean,直接返回
if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
return beanInstance;
}
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());
//將獲取物件的工作交給getObjectFromFactoryBean方法
object = getObjectFromFactoryBean(factory, beanName, !synthetic);
}
//返回獲取的物件
return object;
}
從上面的程式碼來看,這個方法將核心的功能委託給了getObjectFromFactoryBean方法實現。
@Nullable
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) {
//委託給doGetObjectFromFactoryBean方法
object = doGetObjectFromFactoryBean(factory, beanName);
//
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;
}
}
再看doGetObjectFromFactoryBean方法,看到以do開頭的方法,我們就知道這就是我們要找的方法。
@Nullable
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((PrivilegedExceptionAction<Object>) () ->
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;
}
如果bean宣告為FactoryBean型別,則當提取bean時候提取的不是FactoryBean,而是FactoryBean中的getObject方法返回的bean。
從上面的原始碼中將FactoryBean的實現原理總結如下。
通過上述的分析,我們知道最後返回的是通過FactoryBean中對應的getObject方法返回的bean。這是不是和工廠方法模式有些相似呢。下圖是工廠方法模式的UML圖。
對比一下,FactoryBean可以看做是IFactory;getObject方法可以看做getProduct方法;而FactoryBean的具體實現,比如TransactionProxyFactoryBean,可以看做具體的工廠實現FactoryA;通過getObject方法生成的TransactionProxy可以看做是具體的產品實現PorductA。
最後總結下FactoryBean與BeanFactory的區別。
BeanFactory是用於管理bean的一個工廠。
FactoryBean是一種特殊的bean。在BeanFactory中管理兩種bean,一種是標準的Java bean,另一種是工廠bean, 即實現了FactoryBean介面的bean。
通過beanFactory.getBean(beanName)從BeanFactory獲取bean例項時,對於標準的Java bean,返回的是類自身的例項。而對於工廠bean,返回的不是自身的例項,而是該工廠bean的getObject方法所返回的例項。如果想要獲取工廠bean的例項,可以通過getBean(&+beanName)這種方法來獲取。