FAQ

Spring BeanPostProsser最佳实践

2021-11-10  本文已影响0人  liaijuyyer

BeanPostProcessor接口的作用是在Spring bean 实例化完成后(执行完initializeBean)之后,初始化之前(执行bean的初始化方法)对Bean添加一些自定义的处理逻辑。也就是说执行beanProcessor方法是在bean实例化之后 此时bean的属性值都已经被赋值好了。另外需要注意的是 beanProcessor的实现类一定要被Spring托管才能生效。

一、BeanPostProcessor示例

二、为什么BeanPostProcessor必须被Spring托管才能生效
关于这一点可以查看源码

可以发现它最终还是需要通过遍历Spring容器中所有的beanName才能够将这个bean注册为BeanProcessor 如果这个BeanPostProcessor的实现类都没有被Spring托管 也就不可能被注册为beanProcessor 这也就是为什么beanProcessor的实现类必须被spring托管才能生效。

三、BeanPostProcessor执行的流程

//第一步 容器刷新
1、AbstractApplicationContext.refresh()
//第二步 完成beanFactory初始化
2、AbstractApplicationContext.finishBeanFactoryInitialization(beanFactory);  
//第三步 默认的beanFactory开始预实例化单例bean
3、DefaultListableBeanFactory.preInstantiateSingletons();
//第四步 开始根据bean的名称获取bean
4、AbstractBeanFactory.getBean(String name);
//第五步 执行doCreateBean方法 开始创建bean
5、AbstractAutowireCapableBeanFactory.doCreateBean()
//第六步 对bean进行初始化
AbstractAutowireCapableBeanFactory.initializeBean()  
//第七步 开始遍历beanProcessor 执行beanProcessor的postProcessBeforeInitialization方法
AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization();
//第八步 执行beanPostProcessor的后置方法
AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization();

initializeBean源码

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
        //此处省略若干方法
        Object wrappedBean = bean;
        if (mbd == null || !mbd.isSynthetic()) {
      //执行beanPostProcessor的前置方法
            wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
        }

        try {
      //执行bean的初始化方法
            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()) {
      //执行beanPostProcessor的后置方法
            wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }

        return wrappedBean;
    }

applyBeanPostProcessorsBeforeInitialization源码

@Override
    public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
    //遍历beanPostProcessor 执行前置方法
        for (BeanPostProcessor processor : getBeanPostProcessors()) {
            Object current = processor.postProcessBeforeInitialization(result, beanName);
            if (current == null) {
                return result;
            }
            result = current;
        }
        return result;
    }

applyBeanPostProcessorsAfterInitialization源码

@Override
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor processor : getBeanPostProcessors()) {
            Object current = processor.postProcessAfterInitialization(result, beanName);
            if (current == null) {
                return result;
            }
            result = current;
        }
        return result;
    }
上一篇下一篇

猜你喜欢

热点阅读