spring原始碼學習(5.1.0版本)——Bean的初始化(中)
目錄
applyBeanPostProcessorsBeforeInstantiation
postProcessBeforeInstantiation
applyBeanPostProcessorsAfterInitialization
postProcessAfterInitialization與wrapIfNecessary
無代理Bean以及無自定義TargetSource Bean的代理的生成
前言
上篇部落格主要介紹了getBean方法,Bean的初始化會有三個主要的方法
- createBean:初始化Bean,此時還沒有進行屬性填充
- populateBean:進行屬性填充
- initializeBean:呼叫生命週期回撥
這篇部落格主要介紹createBean方法
本文難免有錯誤,歡迎大家指出錯誤
createBean
該方法在AbstractAutowireCapableBeanFactory中
/** * Central method of this class: creates a bean instance, * populates the bean instance, applies post-processors, etc. * @see #doCreateBean */ @Override protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { if (logger.isTraceEnabled()) { logger.trace("Creating instance of bean '" + beanName + "'"); } RootBeanDefinition mbdToUse = mbd; // Make sure bean class is actually resolved at this point, and // clone the bean definition in case of a dynamically resolved Class // which cannot be stored in the shared merged bean definition. 英文講的有點抽象,這裡舉一個例子,並不是所有的BeanDefinition都有BeanClass屬性值的,例如使用@Bean註解 初始化的Bean,這種情況下,就需要通過beanName獲得Bean的類名 Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } // Prepare method overrides. try { 對於 methodOverrides的校驗,即 lookup-method和 replaced-method屬性指定的方法, 如果指定的方法不存在,會丟擲異常,這兩個屬性的效果都是通過動態代理實現的 mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. 這裡初始化有自定義TargetSource的代理類 Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } try { 這裡會幹兩件事: 1、初始化Bean 2、如果有切面應用於Bean,則會為其生成代理 Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isTraceEnabled()) { logger.trace("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) { // A previously detected exception with proper bean creation context already, // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry. throw ex; } catch (Throwable ex) { throw new BeanCreationException( mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex); } }
關於TargetSource,請檢視:https://my.oschina.net/lixin91/blog/688188,spring定義的TargetSource在大多數情況下已經夠我們使了,截至5.1.0版本,TargetSource的種類遠不止《spring揭祕》中所寫的五種,這點會在springAOP原理學習時進行補充
有自定義TargetSource代理類的生成
resolveBeforeInstantiation
該方法位於AbstractAutowireCapableBeanFactory類中
@Nullable protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) { Object bean = null; if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { // Make sure bean class is actually resolved at this point. 不是合成類,存在InstantiationAwareBeanPostProcessors例項,這個類用於處理動態代理 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { Class<?> targetType = determineTargetType(beanName, mbd); if (targetType != null) { 接下里展開此方法 bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); if (bean != null) { 展開此方法 bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } } } mbd.beforeInstantiationResolved = (bean != null); } return bean; }
applyBeanPostProcessorsBeforeInstantiation
該方法位於AbstractAutowireCapableBeanFactory類中
/**
* Apply InstantiationAwareBeanPostProcessors to the specified bean definition
* (by class and name), invoking their {@code postProcessBeforeInstantiation} methods.
* <p>Any returned object will be used as the bean instead of actually instantiating
* the target bean. A {@code null} return value from the post-processor will
* result in the target bean being instantiated.
* @param beanClass the class of the bean to be instantiated
* @param beanName the name of the bean
* @return the bean object to use instead of a default instance of the target bean, or {@code null}
* @see InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
*/
@Nullable
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
其中某個InstantiationAwareBeanPostProcessor的此方法用於例項化代理類
Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
if (result != null) {
return result;
}
}
}
return null;
}
postProcessBeforeInstantiation
該方法位於AbstractAutoProxyCreator中
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
Object cacheKey = getCacheKey(beanClass, beanName);
若targetSourcedBeans中含有beanName,說明beanName的代理物件已經生成了
if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
adviseBeans中儲存是否需要為某個Bean生成代理,如果裡面存在cacheKey,
說明之前已經初始化過Bean的代理類了,此時不需要再次初始化
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
不代理的條件一:不是 Advice、Pointcut、Advisor、AopInfrastructureBean類的子類
不代理的條件二:shouldSkip返回 true,預設返回 false
不代理的條件三:AspectJAwareAdvisorAutoProxyCreator:應該跳過所有 AspectJPointcutAdvisor指定的增強 bean
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
嘗試根據beanClass、beanName獲得自定義的TargetSource
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
if (StringUtils.hasLength(beanName)) {
在這裡放入targetSourcedBeans快取中,這樣在函式開頭就可以返回
this.targetSourcedBeans.add(beanName);
}
getAdvicesAndAdvisorsForBean方法的作用:
Return whether the given bean is to be proxied, what additional advices (e.g. AOP Alliance interceptors) and advisors to apply.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
建立代理,這裡暫不展開此方法,在springaop篇在解析
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
return null;
}
applyBeanPostProcessorsAfterInitialization
該方法位於AbstractAutowireCapableBeanFactory類中
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
如果之前兩步有返回代理Bean,那麼將會跑到這個方法裡,完成生命週期,但是有個BeanPostProcessor會處理無自定義TargetSource,但又想生成代理的情況,接下來展開介紹此postProcessAfterInitialization方法,
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
postProcessAfterInitialization與wrapIfNecessary
這兩個方法位於均AbstractAutowireCapableBeanFactory類中
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
如果之前兩部有返回代理Bean,那麼將會跑到這個方法裡接下來展開介紹postProcessAfterInitialization方法,
這個方法用於處理無自定義TargetSource,但又想生成代理的情況
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
/**
* Wrap the given bean if necessary, i.e. if it is eligible for being proxied.
* @param bean the raw bean instance
* @param beanName the name of the bean
* @param cacheKey the cache key for metadata access
* @return a proxy wrapping the bean, or the raw bean instance as-is
*/
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
對於有自定義TargetSource的Bean來說,上文已經生成代理了,所以不在生成代理,直接返回
if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
在advisedBean中有快取,並且顯示不應該生成代理,直接返回
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
不代理的條件一:不是 Advice、Pointcut、Advisor、AopInfrastructureBean類的子類
不代理的條件二:shouldSkip返回 true,預設返回 false
不代理的條件三:AspectJAwareAdvisorAutoProxyCreator:應該跳過所有 AspectJPointcutAdvisor指定的增強 bean
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
建立代理
// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
Object proxy = createProxy(
對於一般的代理類,其也會使用TargetSource,而且是SingletonTargetSource,這說明spring
的動態代理與我們平常的認知還是有點區別的(多了個TargetSource)
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
放入快取,表明不需要生成代理(由於已經生成,並且是單例)
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
無代理Bean以及無自定義TargetSource Bean的代理的生成
doCreateBean
/**
* Actually create the specified bean. Pre-creation processing has already happened
* at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks.
* <p>Differentiates between default bean instantiation, use of a
* factory method, and autowiring a constructor.
* @param beanName the name of the bean
* @param mbd the merged bean definition for the bean
* @param args explicit arguments to use for constructor or factory method invocation
* @return a new instance of the bean
* @throws BeanCreationException if the bean could not be created
* @see #instantiateBean
* @see #instantiateUsingFactoryMethod
* @see #autowireConstructor
*/
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper負責Bean屬性的填充
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
createBeanInstance方法會建立Bean例項,呼叫建構函式初始化Bean
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
獲得初始化好的Bean,此時並未進行建構函式以外的依賴注入
final Object bean = instanceWrapper.getWrappedInstance();
獲得Bean的class型別,並將其儲存到RootBeanDefinition中,後續就不需要通過解析獲得class型別
Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
// Allow post-processors to modify the merged bean definition.
這個屬性鎖住了RootBeanDefinition的兩個屬性,之前部落格有講,這裡不贅述
上鎖的目的是確保MergedBeanDefinitionPostProcessor只處理一次該Bean的RootBeanDefinition物件
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
讓MergedBeanDefinitionPostProcessor處理RootBeanDefinition
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
程式碼跑到這裡,Bean已經初步例項化了,接下來進行建構函式以外的依賴注入
// Eagerly cache singletons to be able to resolve circular references
// even when triggered by lifecycle interfaces like BeanFactoryAware.
這裡可以看到setter迴圈依賴的處理機制,允許setter迴圈依賴的條件是:
1、Bean是單例
2、允許迴圈依賴
3、當前的Bean正處於建立階段(未進行依賴注入,故沒有完全建立完畢)
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isTraceEnabled()) {
logger.trace("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
這一步是解決setter迴圈依賴的關鍵,在DefaultSingletonBeanRegistry的解析中,已經解析過spring是如何通過三級快取解決迴圈依賴的
這裡不在贅述
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
// Initialize the bean instance.
Object exposedObject = bean;
try {
進行建構函式以外的依賴注入,下一篇部落格會展開講解
populateBean(beanName, mbd, instanceWrapper);
進行生命週期回撥,包括:
1、BeanNameAware、BeanClassLoaderAware、BeanFactoryAware
2、生命週期:BeanPostProcessor.postProcessBeforeInitialization
3、生命週期:InitializingBean.afterPropertiesSet
4、生命週期:呼叫初始化方法:init-method
5、生命週期:BeanPostProcessor的postProcessAfterInitialization
這裡可能會生成代理
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw (BeanCreationException) ex;
}
else {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
}
}
if (earlySingletonExposure) {
注意到getSingleton函式的allowEarlyReference屬性值為false,意味著只能從singletonObjects快取或是
earlySingletonObjects快取中獲得例項,此時singletonObjects快取中是不存在例項的,
只能從earlySingletonObjects快取中獲得
Object earlySingletonReference = getSingleton(beanName, false);
如果earlySingletonReference不為空,說明出現setter迴圈依賴
if (earlySingletonReference != null) {
由上文可以看出這兩者指向相同的記憶體,這裡為什麼要判斷exposedObject與bean相等呢?
其實我們忽略了生成代理的情況,initializeBean方法中可能會生成代理,如果生成了代理,
這句if是不成立的,如果沒有生成代理,earlySingletonReference才是最終的結果
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
對於代理,需要確保所有的依賴都已經生成
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
actualDependentBeans用於儲存未建立的依賴Bean
Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
已經建立完畢的Bean會儲存到alreadyCreated中,若alreadyCreated不包含 dependentBean會返回 false
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
如果actualDependentBeans不為空,則丟擲異常,因為beanName已經建立完畢,但是beanName的依賴卻沒有建立完畢
if (!actualDependentBeans.isEmpty()) {
throw new BeanCurrentlyInCreationException(beanName,
"Bean with name '" + beanName + "' has been injected into other beans [" +
StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
// Register bean as disposable.
try {
如果bean實現了銷燬回撥,則會在這裡註冊,儲存到某個快取中,在spring銷燬Bean時,
會根據該快取,呼叫銷燬回撥
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
createBeanInstance:建立Bean例項
/**
* Create a new instance for the specified bean, using an appropriate instantiation strategy:
* factory method, constructor autowiring, or simple instantiation.
* @param beanName the name of the bean
* @param mbd the bean definition for the bean
* @param args explicit arguments to use for constructor or factory method invocation
* @return a BeanWrapper for the new instance
* @see #obtainFromSupplier
* @see #instantiateUsingFactoryMethod
* @see #autowireConstructor
* @see #instantiateBean
*/
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
// Make sure bean class is actually resolved at this point.
Class<?> beanClass = resolveBeanClass(mbd, beanName);
isNonPublicAccessAllowed預設返回 true,即非 public的類同樣可以例項化
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
}
如果我們配置了AbstractBeanDefinition的InstanceSupplier屬性,則會根據InstanceSupplier的值
初始化Bean,而不是建構函式與工廠方法,這是java8推出時出現的,以後有空看下咋回事
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
如果工廠方法不為空,則通過工廠方法例項化,使用@Bean初始化的Bean,在這裡初始化
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
夾在橫線中間的程式碼,在我們初始化自己的Bean時,是不會執行的
----------------------------------------------------------------------------------------------------------------------------
// Shortcut when re-creating the same bean...
boolean resolved = false;
boolean autowireNecessary = false;
第一次初始化時,傳入的建構函式引數為空,會跑到下面的if語句中
if (args == null) {
在第一次初始化時,並不存在已經解析好的建構函式或是工廠方法,所以下面這些語句並不會執行
synchronized (mbd.constructorArgumentLock) {
判斷是否存在已經解析好的建構函式或是工廠方法
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
如果建構函式引數已解析,即知道要注入什麼給建構函式引數,
則autowireNecessary為true,建構函式所需要的引數由constructorArgumentsResolved
提供,若constructorArgumentsResolved為空,說明我們沒有配置建構函式引數,此時spring
會使用預設建構函式
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
if (resolved) {
if (autowireNecessary) {
有參建構函式所需要的所有資訊都儲存在RootBeanDefinition中,
從而避免再次解析
return autowireConstructor(beanName, mbd, null, null);
}
else {
使用預設建構函式初始化
return instantiateBean(beanName, mbd);
}
}
---------------------------------------------------------------------------------------------------------------------------------
Bean首次初始化時,會在以下程式碼執行初始化
// Candidate constructors for autowiring?
獲得建構函式列表,判斷使用哪個構造器:
如果實現了 SmartInstantiationAwareBeanPostProcessor,
可以通過方法 determineCandidateConstructors指定,否則返回 null
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
第一次初始化時,具有有參建構函式的Bean會在這裡初始化,這個函式內部會通過createArgumentArray
方法解析獲得建構函式引數,並且會有建構函式迴圈依賴的檢查,這句程式碼執行完畢後,RootBeanDefinition的關於建構函式的四個屬性會發生變化
1、constructorArgumentsResolved變為true
2、resolvedConstructorOrFactoryMethod不為空
3、constructorArgumentValues不為空
4、preparedConstructorArguments不為空
以後初始化時,就可以直接利用上述資訊,跑到橫向內的程式碼執行初始化
return autowireConstructor(beanName, mbd, ctors, args);
}
// Preferred constructors for default construction?
從5.1.0開始有這一段程式碼,但是這個函式一般返回null,if語句內的程式碼基本沒用,所以還是會執行預設建構函式初始化
ctors = mbd.getPreferredConstructors();
if (ctors != null) {
return autowireConstructor(beanName, mbd, ctors, null);
}
使用預設建構函式初始化
// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);
}
收穫
透過本次部落格的學習,可以看出BeanDefinition並不是一開始就儲存了Bean的所有資訊,在Bean第一次進行初始化時,才會進行對應的解析,並且將解析的結果儲存在BeanDefinition中
Bean初始化的軌跡
spring初始化Bean的結構十分複雜,這裡畫一張圖,以捋清楚Bean初始化的順序,對於搞清楚setter迴圈依賴的解決有一定幫助