Spring注解驱动开发 第十节Bean的后置处理器

Spring注解驱动开发 第十节Bean的后置处理器

首先要提到的是Bean的后置处理器与之前的初始化方法与销毁方法有什么区别呢,其实后置处理器的粒度要比初始化方法小,因为后置处理器是在bean初始化方法调用的前后做相关操作的。具体详细的介绍在代码中的注释都有介绍。

/**
 * Bean后置处理器
 */
@Component
public class MyBeanPostProessor implements BeanPostProcessor{

    /**
     * 参数bean就是我们注册的实例,beanName就是实例的名称。
     * 返回值可以返回参数bean,也可以把参数bean包装后进行返回。
     * 后置处理器是在初始化方法调用时的前后添加相关操作的。
     * postProcessBeforeInitialization在我们以前学过的JSR250、initMethod方法之前调用。
     * 而postProcessAfterInitialization是在之后调用。
     */
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization  ....."+beanName);
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization  ....."+beanName);
        return bean;
    }
}

其中postProcessBeforeInitialization是在初始化之前调用,postProcessAfterInitialization在初始化之后调用。
查看打印结果:

四月 23, 2019 3:36:21 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f410acf: startup date [Tue Apr 23 15:36:21 GMT+08:00 2019]; root of context hierarchy
postProcessBeforeInitialization  .....org.springframework.context.event.internalEventListenerProcessor
postProcessAfterInitialization  .....org.springframework.context.event.internalEventListenerProcessor
postProcessBeforeInitialization  .....org.springframework.context.event.internalEventListenerFactory
postProcessAfterInitialization  .....org.springframework.context.event.internalEventListenerFactory
postProcessBeforeInitialization  .....mainConfig4
postProcessAfterInitialization  .....mainConfig4
black构造器被调用了....
postProcessBeforeInitialization  .....black
black afterPropertiesSet ...
postProcessAfterInitialization  .....black
Dog 默认构造器加载了...
postProcessBeforeInitialization  .....dog
Dog @PostConstruct ...
postProcessAfterInitialization  .....dog
Blue被加载到spring容器中了...
postProcessBeforeInitialization  .....blue
Blue init...
postProcessAfterInitialization  .....blue
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig4
black
dog
myBeanPostProessor
blue
Blue destory...
Dog destory ...
black destroy ...
四月 23, 2019 3:36:21 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f410acf: startup date [Tue Apr 23 15:36:21 GMT+08:00 2019]; root of context hierarchy

Process finished with exit code 0

可以看到,bean的任何初始化方法调用前都会调用bean的后置处理器,这也证实了我们之前说的完全正确。

下面就来说一下bean后置处理器的原理

在这里插入图片描述
首先在后置处理器上打上断点,debug运行一下,我们查看一下方法调用栈。
在这里插入图片描述
我们的运行起点在test05上,经过spring的层层调用,我们把调用方法锁定在initializeBean上,点击它。
在这里插入图片描述
但是在这个之前的上一层有一个doCreateBean方法调用了这个方法
在这里插入图片描述
从这两张图看得出,是先给bean属性赋值,然后在进行bean后置处理器操作与初始化方法调用操作。当我们点击applyBeanPostProcessorsBeforeInitialization方法,也是上图的初始化方法调用之前的后置处理器方法调用。
在这里插入图片描述
上图就不用多说什么了,我们重写的方法被spring调用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值