BeanPostProcessor即,Bean的后置处理器,它的作用就是在Bean的初始化方法前跟后进行拦截处理。我们都知道,要想在Bean的初始化方法前后进行工作,那必须在Bean实例创建完成之后,init方法执行之前,后置处理器就已经在容器中了,所以我们来到向容器中添加后置处理器的类AbstractApplicationContext,其中refresh()中的registerBeanPostProcessors(beanFactory)就是首先向容器中注册我们所需要的所有后置处理器。如何注册后置处理器我们暂不作分析,着重说一下,后置处理器是如何工作的。
实现类:
@Component public class BeanPostProcessorImpl implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // 返回一个传过来的对象 // 在初始化方法调用之前进行后置处理工作 // 什么时候调用呢?在init-method方法之前就调用了 System.out.println("postProcessBeforeInitialization======" + beanName + "======" + bean); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization======" + beanName + "======" + bean); return bean; } }
配置类:
@Configuration @ComponentScan("com.nmys.story.springCore.springioc.beanpostprocessor_") public class Config01 { @Bean(initMethod = "init",destroyMethod = "destroy") public Train train(){ return new Train(); } }
实体类:
/** * @author 70KG * @Title: Train * @Description: * @date 2018/7/23下午11:31 * @From www.nmyswls.com */ public class Train { private String name = "火车"; private Integer length = 150; public Train() { System.out.println("Train构造方法执行。。。。。。"); } public void init() { System.out.println("Train的init方法执行。。。。。。"); } public void destroy() { System.out.println("Train的destroy方法执行。。。。。。"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getLength() { return length; } public void setLength(Integer length) { this.length = length; } }
测试类:
public class Test01 { @Test public void test() { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config01.class); ac.close(); } }
打印结果:
Train构造方法执行。。。。。。 postProcessBeforeInitialization======train======com.nmys.story.springCore.springioc.beanpostprocessor_.Train@5aa9e4eb Train的init方法执行。。。。。。 postProcessAfterInitialization======train======com.nmys.story.springCore.springioc.beanpostprocessor_.Train@5aa9e4eb Train的destroy方法执行。。。。。。
结果分析:
首先Train构造方法执行创建对象,然后执行了后置处理器的Before方法,然后才调用init方法,init方法执行完成,再执行后置处理器的After方法,最后容器关闭执行销毁方法。
AnnotationConfigApplicationContext构造方法中的refresh(); -> AbstractApplicationContext550行finishBeanFactoryInitialization(beanFactory);完成剩余Bean的初始化工作 -> AbstractApplicationContext869行beanFactory.preInstantiateSingletons(); -> DefaultListableBeanFactory760行getBean(beanName); -> AbstractBeanFactory317行createBean(beanName, mbd, args); -> AbstractAutowireCapableBeanFactory503行doCreateBean(beanName, mbdToUse, args); -> AbstractAutowireCapableBeanFactory580行populateBean(beanName, mbd, instanceWrapper);在执行初始化之前,首先给实例赋值 -> AbstractAutowireCapableBeanFactory581行exposedObject = initializeBean(beanName, exposedObject, mbd);准备执行初始化方法 -> AbstractAutowireCapableBeanFactory1700行wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); -> AbstractAutowireCapableBeanFactory1704行invokeInitMethods(beanName, wrappedBean, mbd); -> AbstractAutowireCapableBeanFactory1712行wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
分析AbstractAutowireCapableBeanFactory的applyBeanPostProcessorsBeforeInitialization方法:
@Override public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { Object current = beanProcessor.postProcessBeforeInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { AccessControlContext acc = null; if (System.getSecurityManager() != null && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) { acc = this.applicationContext.getBeanFactory().getAccessControlContext(); } if (acc != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareInterfaces(bean); return null; }, acc); } else { invokeAwareInterfaces(bean); } return bean; }
<!--more-->
private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } } }
BeanPostProcessor就是在Bean实例创建之后,在进行populateBean赋值之后,init初始化方法之前进行一次调用,init方法之后进行一次调用,这样一来,整个Bean的生命周期,全部掌控在了Spring之下,包括Bean实例创建new Instance(),赋值前后populateBean(),初始化前后init(),销毁前后destroy()。从此Bean便身不由己了。