- 从企业级开发到云原生微服务:Spring Boot实战
- 汪云飞等
- 154字
- 2025-02-17 20:16:14
3.3 对Bean的处理(BeanPostProcessor)
可以通过实现BeanPostProcessor接口,在构造时对容器内所有或者部分指定Bean进行处理。和@PostConstruct与@PreDestroy不同的是,它针对的是IoC容器里的所有的Bean。

通过覆写postProcessBeforeInitialization和postProcessAfterInitialization方法,所有的Bean在初始化之前都会执行 postProcessBeforeInitialization里的处理逻辑,在初始化之后都会执行postProcessAfterInitialization里的处理逻辑。执行结果如图3-14所示。

图3-14
如果想要缩小Processor的处理范围,则可以通过判断Bean类型来实现。
