《Spring实战》里的图,乍一看很混乱,debug了一下,清楚了些
原来中间那一大段都在一段逻辑里,可以debug下这段加深印象
初始化前后调用了许多Processors进行处理的意义是什么?
实现接口的Processor均可以对bean进行处理,自己定义的processor也可以
Class AbstractAutowireCapableBeanFactory{
//省略其他方法 protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { AbstractAutowireCapableBeanFactory.this.invokeAwareMethods(beanName, bean); return null; } }, this.getAccessControlContext()); } else { //处理BeanNameAware、BeanFactoryAwrae、BeanClassLoaderAware this.invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { //Processors调用BeforeInitialization方法,ApplicationAware接口在这被处理 wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName); } try { //afterPropertiesSet调用和init-method方法调用 this.invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable var6) { throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6); } if (mbd == null || !mbd.isSynthetic()) { //Processors调用AfterInitialization方法 wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; } }
最后的销毁动作调用了DisposableBean的destory()方法,其中调用了destory-method
class DisposableBeanAdapter implements DisposableBean{ //省略以上很多方法 public void destroy() { //省略以上一些逻辑 if (this.destroyMethod != null) { this.invokeCustomDestroyMethod(this.destroyMethod); } else if (this.destroyMethodName != null) { Method methodToCall = this.determineDestroyMethod(); if (methodToCall != null) { this.invokeCustomDestroyMethod(methodToCall); } } } }