Spring框架之所以NB,BeanPostProcessor功不可没。BeanPostProcessor通过生命周期回调实现对Bean的加强比方,其实Spring的AOP性能次要就是通过BeanPostProcessor实现的,以及,Spring重要的主动拆卸性能,也是通过BeanPostProcessor实现的。

BeanPostProcessor失效原理

BeanPostProcessor指的是Spring容器中所有实现了BeanPostProcessor接口的类,通过如下形式失效:

  1. BeanPostProcessor注册:所有实现了BeanPostProcessor接口的类,在Spring初始化过程中注册到Spring容器中
  2. Bean生命周期的初始化前、后,Spring别离回调BeanPostProcessor接口的postProcessBeforeInitialization、postProcessAfterInitialization,从而实现对bean的加强。

具体的回调过程,在AbstractAutowireCapableBeanFactory的initializeBean办法中:

    protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {        if (System.getSecurityManager() != null) {            AccessController.doPrivileged((PrivilegedAction<Object>) () -> {                invokeAwareMethods(beanName, bean);                return null;            }, getAccessControlContext());        }        else {            invokeAwareMethods(beanName, bean);        }        Object wrappedBean = bean;        if (mbd == null || !mbd.isSynthetic()) {            wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);        }        try {            invokeInitMethods(beanName, wrappedBean, mbd);        }        catch (Throwable ex) {            throw new BeanCreationException(                    (mbd != null ? mbd.getResourceDescription() : null),                    beanName, "Invocation of init method failed", ex);        }        if (mbd == null || !mbd.isSynthetic()) {            wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);        }        return wrappedBean;    }

beanPostProcessor的注册

Spring初始化时,refresh()办法中注册beanPostProcessor:

@Override    public void refresh() throws BeansException, IllegalStateException {        synchronized (this.startupShutdownMonitor) {            // Prepare this context for refreshing.            prepareRefresh();            // Tell the subclass to refresh the internal bean factory.            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();            // Prepare the bean factory for use in this context.            prepareBeanFactory(beanFactory);            try {                // Allows post-processing of the bean factory in context subclasses.                postProcessBeanFactory(beanFactory);                // Invoke factory processors registered as beans in the context.                invokeBeanFactoryPostProcessors(beanFactory);                // Register bean processors that intercept bean creation.                registerBeanPostProcessors(beanFactory);    ...

registerBeanPostProcessors办法负责注册beanPostProcessors,所有以后容器中实现了BeanPostProcessor接口的类,依照如下程序顺次注册到容器的beanPostProcessors中:

  1. 首先,实现了PriorityOrdered接口的BeanPostProcessor
  2. 其次,实现了Ordered接口的BeanPostProcessor
  3. 最初,其余一般BeanPostProcessor

这样,所有的BeanPostProcessor就会在bean实例化、初始化之前的Spring启动阶段注册到Spring容器中。之后的Bean初始化过程中,Spring会主动查看所有的BeanPostProcessor,并顺次回调两个接口办法从而实现BeanPostProcessor对bean的加强。

此外,还能够通过编程的形式注册BeanPostProcessor:通过ConfigurableBeanFactory的addBeanPostProcessor办法注册,编程形式注册的BeanPostProcessor优先于Spring主动注册的BeanPostProcessor,并且,编程形式注册的状况下BeanPostProcessor的程序与BeanPostProcessor的Ordered接口无关、仅与注册先后顺序无关。

AOP与BeanPostProcessor

因为Spring AOP就是通过BeanPostProcessor实现代理对象的主动创立的,所以,如果一个AOP对象同时实现了BeanPostProcessor接口,该对象的AOP性能就没有方法实现,因为该AOP对象在Spring启动的过程中作为BeanPostProcessor被注册到Spring容器中,与实现AOP动静代理的BeanPostProcessor是处于同一级别的、从而不会被AOP的BeanPostProcessor加强,因而也就无奈实现AOP。

同时,须要留神的是,如果你通过@Autowired或@Resource主动拆卸你的BeanPostProcessor类,在没有指定bean name的状况下,有可能会产生不可意料的后果,因为主动拆卸过程中,如果不能通过name匹配到bean,Spring会依据type主动匹配,因为容器中有很多BeanPostProcessor,所以,可能就会产生错乱......

上一篇 Spring FrameWork从入门到NB - 定制Bean