spring的ioc創(chuàng)建bean的最核心代碼 我們來看看他具體做了啥 怎么做
整體的邏輯
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
BeanWrapper 挪丢,包含了真正的bean對象和bean的class,以及PropertyDescriptor集合
BeanWrapper instanceWrapper = null;
單例的情況下嘗試從factoryBeanInstanceCache獲取 instanceWrapper
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
如果沒有則需要自己創(chuàng)建
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
final Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass();
如果不是NullBean蹬刷,則將resolvedTargetType 屬性設(shè)置為當(dāng)前的WrappedClass
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
這邊主要是尋找?guī)讉€meta,@PostConstruct,@Autowire,@Value,@Resource绸硕,@PreDestory等
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
如果當(dāng)前bean是單例,且支持循環(huán)依賴,且當(dāng)前bean正在創(chuàng)建巍糯,通過往singletonFactories添加一個objectFactory尝江,
這樣后期如果有其他bean依賴該bean 可以從singletonFactories獲取到bean涉波,getEarlyBeanReference可以對返回的bean進(jìn)行修改,這邊目前除了可能會返回動態(tài)代理對象 其他的都是直接返回bean
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isDebugEnabled()) {
logger.debug("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
Object exposedObject = bean;
try {
填充bean的屬性
populateBean(beanName, mbd, instanceWrapper);
初始化bean
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);
}
}
如果earlySingletonExposure炭序,嘗試從緩存獲取該bean(一般存放在singletonFactories對象通過調(diào)用getObject 把對象存入earlySingletonObjects)啤覆,分別從singletonObjects和earlySingletonObjects獲取對象
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
如果獲取到對象了
if (earlySingletonReference != null) {
當(dāng)exposedObject (初始化之后的bean等于原始的bean,說明不是proxy)惭聂,則把緩存中的bean賦值給exposedObject
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
檢測該bean的dependon的bean是否都已經(jīng)初始化好了
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
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.");
}
}
}
}
try {
注冊DisposableBean
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
createBeanInstance的源碼分析
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
獲取beanclass
Class<?> beanClass = resolveBeanClass(mbd, beanName);
如果beanclass為空窗声,且beanclass不是public 且沒有權(quán)限訪問構(gòu)造函數(shù)和方法則拋出異常
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());
}
Supplier類似于factoryBean,然后從Supplier.get()的bean辜纲,并把bean包裝成BeanWrapper笨觅,然后初始化BeanWrapper
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
嘗試從factoryMethod獲取instanceSupplier
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
這邊是相當(dāng)于檢測是否曾經(jīng)創(chuàng)建過這個bean,如果是查看下具體采用哪個方法
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
說明曾經(jīng)解析過構(gòu)造函數(shù)或者factoryMethod
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
如果autowireNecessary為true說明是采用構(gòu)造函數(shù)注入
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
如果已經(jīng)解析過了則按照邏輯選擇
if (resolved) {
if (autowireNecessary) {
return autowireConstructor(beanName, mbd, null, null);
}
else {
return instantiateBean(beanName, mbd);
}
}
說明是第一次創(chuàng)建該bean 根據(jù)SmartInstantiationAwareBeanPostProcessor獲取構(gòu)造函數(shù)侨歉,目前看基本上都是空的實(shí)現(xiàn)屋摇,除了AutowiredAnnotationBeanPostProcessor
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
開始生成實(shí)例
return autowireConstructor(beanName, mbd, ctors, args);
}
采用普通的方式生成實(shí)例
return instantiateBean(beanName, mbd);
}
AutowiredAnnotationBeanPostProcessor的determineCandidateConstructors
尋找被@Autowired的構(gòu)造函數(shù),如果沒有就返回null
我們的lookup就是在這邊通過反射發(fā)現(xiàn)的(其他的xml形式的replace-method和lookup在BeanDefinitionParserDelegate中)
if (!this.lookupMethodsChecked.contains(beanName)) {
try {
ReflectionUtils.doWithMethods(beanClass, method -> {
Lookup lookup = method.getAnnotation(Lookup.class);
if (lookup != null) {
Assert.state(beanFactory != null, "No BeanFactory available");
LookupOverride override = new LookupOverride(method, lookup.value());
try {
RootBeanDefinition mbd = (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName);
mbd.getMethodOverrides().addOverride(override);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(beanName,
"Cannot apply @Lookup to beans without corresponding bean definition");
}
}
});
}
catch (IllegalStateException ex) {
throw new BeanCreationException(beanName, "Lookup method resolution failed", ex);
}
this.lookupMethodsChecked.add(beanName);
}
檢測緩存中是否已經(jīng)存在對應(yīng)的構(gòu)造函數(shù)
Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
雙重檢測
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
Constructor<?>[] rawCandidates;
try {
獲取聲明的構(gòu)造函數(shù)
rawCandidates = beanClass.getDeclaredConstructors();
}
catch (Throwable ex) {
throw new BeanCreationException(beanName,
"Resolution of declared constructors on bean Class [" + beanClass.getName() +
"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
}
List<Constructor<?>> candidates = new ArrayList<>(rawCandidates.length);
Constructor<?> requiredConstructor = null;
Constructor<?> defaultConstructor = null;
這個只對Kotlin有用 對于java 一般默認(rèn)的返回null
Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass);
int nonSyntheticConstructors = 0;
for (Constructor<?> candidate : rawCandidates) {
如果不是合成幽邓,則nonSyntheticConstructors加1
if (!candidate.isSynthetic()) {
nonSyntheticConstructors++;
}
else if (primaryConstructor != null) {
continue;
}
AnnotationAttributes ann =
尋找構(gòu)造上是否存在@Autowired炮温,@Value,@Inject牵舵,如果存在就返回required=true柒啤,一個bean的構(gòu)造函數(shù)如果超過一個@Autowired就拋出異常
findAutowiredAnnotation(candidate);
如果沒有
if (ann == null) {
一般都是返回class本身倦挂,如果是cglib就返回原始的類
Class<?> userClass = ClassUtils.getUserClass(beanClass);
if (userClass != beanClass) {
try {
獲取帶有參數(shù)的構(gòu)造函數(shù)
Constructor<?> superCtor =
userClass.getDeclaredConstructor(candidate.getParameterTypes());
繼續(xù)尋找是否攜帶了上面三個注解
ann = findAutowiredAnnotation(superCtor);
}
catch (NoSuchMethodException ex) {
// Simply proceed, no equivalent superclass constructor found...
}
}
}
ann!=null 代表找到了
if (ann != null) {
如果requiredConstructor 不為null 說明重復(fù)定義
if (requiredConstructor != null) {
throw new BeanCreationException(beanName,
"Invalid autowire-marked constructor: " + candidate +
". Found constructor with 'required' Autowired annotation already: " +
requiredConstructor);
}
這邊只是確認(rèn)下required返回的是true還是false
boolean required = determineRequiredStatus(ann);
如果required是true
if (required) {
但是candidates不為空說明重復(fù)了
if (!candidates.isEmpty()) {
throw new BeanCreationException(beanName,
"Invalid autowire-marked constructors: " + candidates +
". Found constructor with 'required' Autowired annotation: " +
candidate);
}
requiredConstructor = candidate;
}
candidates.add(candidate);
}
else if (candidate.getParameterCount() == 0) {
defaultConstructor = candidate;
}
}
if (!candidates.isEmpty()) {
// Add default constructor to list of optional constructors, as fallback.
if (requiredConstructor == null) {
if (defaultConstructor != null) {
candidates.add(defaultConstructor);
}
else if (candidates.size() == 1 && logger.isWarnEnabled()) {
logger.warn("Inconsistent constructor declaration on bean with name '" + beanName +
"': single autowire-marked constructor flagged as optional - " +
"this constructor is effectively required since there is no " +
"default constructor to fall back to: " + candidates.get(0));
}
}
candidateConstructors = candidates.toArray(new Constructor<?>[0]);
}
else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
}
else if (nonSyntheticConstructors == 2 && primaryConstructor != null
&& defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {
candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor};
}
else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {
candidateConstructors = new Constructor<?>[] {primaryConstructor};
}
else {
candidateConstructors = new Constructor<?>[0];
}
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
return (candidateConstructors.length > 0 ? candidateConstructors : null);
initBeanWrapper
初始BeanWrapperImpl担巩,設(shè)置ConversionService
(可以查看每個類型是否可以轉(zhuǎn)換成其他類型方援,并提供轉(zhuǎn)換的方法)和注冊
一些列的ResourceEditor到bw,比如 doRegisterEditor(registry,
Resource.class, baseEditor);如果遇到Resource類型的使用baseEditor涛癌,進(jìn)
行編輯犯戏。
protected void initBeanWrapper(BeanWrapper bw) {
bw.setConversionService(getConversionService());
registerCustomEditors(bw);
}
protected void registerCustomEditors(PropertyEditorRegistry registry) {
PropertyEditorRegistrySupport registrySupport =
(registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
if (registrySupport != null) {
registrySupport.useConfigValueEditors();
}
if (!this.propertyEditorRegistrars.isEmpty()) {
for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
try {
registrar.registerCustomEditors(registry);
}
catch (BeanCreationException ex) {
Throwable rootCause = ex.getMostSpecificCause();
if (rootCause instanceof BeanCurrentlyInCreationException) {
BeanCreationException bce = (BeanCreationException) rootCause;
String bceBeanName = bce.getBeanName();
if (bceBeanName != null && isCurrentlyInCreation(bceBeanName)) {
if (logger.isDebugEnabled()) {
logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() +
"] failed because it tried to obtain currently created bean '" +
ex.getBeanName() + "': " + ex.getMessage());
}
onSuppressedException(ex);
continue;
}
}
throw ex;
}
}
}
如果存在用戶自定義的類型轉(zhuǎn)換 也設(shè)置
if (!this.customEditors.isEmpty()) {
this.customEditors.forEach((requiredType, editorClass) ->
registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass)));
}
}
MergedBeanDefinitionPostProcessors---AutowiredAnnotationBeanPostProcessor
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
獲取@Autowired注解的屬性
InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null);
校驗(yàn)
metadata.checkConfigMembers(beanDefinition);
}
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
獲取cacheKey
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
如果metadata為空,或者clazz拳话!=metadata.targetClass則說明沒有緩存無效先匪,重新buildAutowiringMetadata,然后把結(jié)果緩存起來
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
synchronized (this.injectionMetadataCache) {
metadata = this.injectionMetadataCache.get(cacheKey);
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
if (metadata != null) {
metadata.clear(pvs);
}
分別從class的field和method獲取@Autowired弃衍,并放入緩存(會迭代查詢其父類class)
metadata = buildAutowiringMetadata(clazz);
this.injectionMetadataCache.put(cacheKey, metadata);
}
}
}
return metadata;
}
public void checkConfigMembers(RootBeanDefinition beanDefinition) {
Set<InjectedElement> checkedElements = new LinkedHashSet<>(this.injectedElements.size());
for (InjectedElement element : this.injectedElements) {
把method或者field放入externallyManagedConfigMembers緩存中
Member member = element.getMember();
if (!beanDefinition.isExternallyManagedConfigMember(member)) {
beanDefinition.registerExternallyManagedConfigMember(member);
checkedElements.add(element);
if (logger.isDebugEnabled()) {
logger.debug("Registered injected element on class [" + this.targetClass.getName() + "]: " + element);
}
}
}
this.checkedElements = checkedElements;
}
填充屬性-populateBean--先對于unsatisfiedNonSimpleProperties(未在xml配置該property 該property是一個特殊的對象(普通的javabean的類))屬性進(jìn)行處理呀非,在對一些注解注入的屬性處理,最終對自動注入的屬性(即包含set方法的)屬性進(jìn)行處理
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
如果實(shí)力為空但是還有屬性镜盯,拋出異常
if (bw == null) {
if (mbd.hasPropertyValues()) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
}
else {
直接返回
return;
}
}
boolean continueWithPropertyPopulation = true;
這邊主要是根據(jù)InstantiationAwareBeanPostProcessor 判斷是否繼續(xù)給該bean配置屬性
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
continueWithPropertyPopulation = false;
break;
}
}
}
}
if (!continueWithPropertyPopulation) {
return;
}
設(shè)置PropertyValues
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
如果該bean是支持按照名字或者類型自動注入的岸裙,
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
深度拷貝PropertyValues,當(dāng)然對于對象來說只能公用一個
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
按照名稱開始注入(這邊不是真正的注入速缆,只是尋找可注入的bean)
必須有set方法
必須不能是忽略的類型或者接口
spring配置的properties中沒有改屬性
該屬性不能是 a primitive, an enum, a String or other CharSequence, a Number, a Date,
* a URI, a URL, a Locale or a Class.
其中primitive( boolean, byte,
* char, short, int, long, float, or double)或者其包裝類
如果class是array 其元素也不能是上述屬性
-------
也就是說自動注入的時候 如果不顯示的配置<properties> 對于非對象的類型是不會注入的降允,但是對于對象是會主動去尋找的
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
autowireByName(beanName, mbd, bw, newPvs);
}
按照類型開始注入(這邊不是真正的注入,只是尋找可注入的bean)
必須有set方法
必須不能是忽略的類型或者接口
spring配置的properties中沒有改屬性
該屬性不能是 a primitive, an enum, a String or other CharSequence, a Number, a Date,
* a URI, a URL, a Locale or a Class.
其中primitive( boolean, byte,
* char, short, int, long, float, or double)或者其包裝類
如果class是array 其元素也不能是上述屬性
-------
也就是說自動注入的時候 如果不顯示的配置<properties> 對于非對象的類型是不會注入的艺糜,但是對于對象是會主動去尋找的
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
autowireByType(beanName, mbd, bw, newPvs);
}
pvs = newPvs;
}
boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
如果存在InstantiationAwareBeanPostProcessors或者有依賴檢測
if (hasInstAwareBpps || needsDepCheck) {
if (pvs == null) {
pvs = mbd.getPropertyValues();
}
獲得一組PropertyDescriptor拟糕,filterPropertyDescriptorsForDependencyCheck
---獲取該bean下面的所有屬性和方法
---刪除如果是cglib的本身的屬性和方法
---刪除如果是ignoredDependencyTypes
---刪除如果是set方法且需要忽略的(即實(shí)現(xiàn)了xxawared接口的)也需要刪除
---最終保留的屬性可以自動注入
PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
if (hasInstAwareBpps) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
對@Autowired,@Resource等屬性的注入
pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
if (pvs == null) {
return;
}
}
}
}
依賴檢測
if (needsDepCheck) {
checkDependencies(beanName, mbd, filteredPds, pvs);
}
}
對自動注入的時候 set方法的注入
if (pvs != null) {
applyPropertyValues(beanName, mbd, bw, pvs);
}
}
初始化bean-initializeBean
第一種:通過@PostConstruct 和 @PreDestroy 方法 實(shí)現(xiàn)初始化和銷毀bean之前進(jìn)行的操作
第二種是:通過 在xml中定義init-method 和 destory-method方法
第三種是: 通過bean實(shí)現(xiàn)InitializingBean和 DisposableBean接口
三種銷毀方式都是把bean轉(zhuǎn)換成DisposableBean
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
首先調(diào)用回調(diào)方法xxxaware
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
調(diào)用spring的擴(kuò)展點(diǎn) 在初始化之前調(diào)用
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
先調(diào)用InitializingBean的afterPropertiesSet,在調(diào)用我們定義的init方法
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
調(diào)用spring的擴(kuò)展點(diǎn) 在初始化之后調(diào)用倦踢,注意這邊就是生成我們配置的動態(tài)代理對象的關(guān)鍵代碼
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
注冊registerDisposableBeanIfNecessary
protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);
只有非原型且是DisposableBean,或者有destroy method 或者注冊了 DestructionAwareBeanPostProcessors.
最終在緩存中存放beanName和DisposableBean的映射關(guān)系侠草,然后注冊鉤子的時候調(diào)用
if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
針對單例或者scope注冊
if (mbd.isSingleton()) {
最終在緩存中存放beanName和DisposableBean的映射關(guān)系辱挥,然后注冊鉤子的時候調(diào)用
registerDisposableBean(beanName,
new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
}
else {
// A bean with a custom scope...
Scope scope = this.scopes.get(mbd.getScope());
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
}
注冊了一個回調(diào),根據(jù)scope的類型(比如是一個線程边涕,一個request晤碘,servlet,事務(wù))當(dāng)這幾種結(jié)束的時候回調(diào)這個DisposableBeanAdapter的destory方法
scope.registerDestructionCallback(beanName,
new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
}
}
}