Spring中BeanPostProcessor
Spring提供了很多扩展接口,BeanPostProcessor接口和InstantiationAwareBeanPostProcessor接口就是其中两个。
BeanPostProcessor
- import org.springframework.beans.factory.config.BeanPostProcessor;
- public class MyBeanPostProcessor implements BeanPostProcessor {
- public MyBeanPostProcessor() {
- super();
- System.out.println("这是BeanPostProcessor实现类构造器!!");
- }
- @Override
- public Object postProcessAfterInitialization(Object bean, String arg1)
- throws BeansException {
- System.out.println("bean处理器:bean创建之后..");
- return bean;
- }
- @Override
- public Object postProcessBeforeInitialization(Object bean, String arg1)
- throws BeansException {
- System.out.println("bean处理器:bean创建之前..");
- return bean;
- }
- }
BeanPostProcessor接口定义如下:
- public interface BeanPostProcessor {
- /**
- * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
- * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
- * or a custom init-method). The bean will already be populated with property values.
- */
- //实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务
- Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
- /**
- * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
- * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
- * or a custom init-method). The bean will already be populated with property values.
- */
- //实例化、依赖注入、初始化完毕时执行
- Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
- }
由方法名字也可以看出,前者在实例化及依赖注入完成后、在任何初始化代码(比如配置文件中的init-method)调用之前调用;后者在初始化代码调用之后调用。
注意:
1、接口中的两个方法都要将传入的bean返回,而不能返回null,如果返回的是null那么我们通过getBean方法将得不到目标。
2、BeanFactory和ApplicationContext对待bean后置处理器稍有不同。ApplicationContext会自动检测在配置文件中实现了BeanPostProcessor接口的所有bean,并把它们注册为后置处理器,然后在容器创建bean的适当时候调用它,因此部署一个后置处理器同部署其他的bean并没有什么区别。而使用BeanFactory实现的时候,bean 后置处理器必须通过代码显式地去注册,在IoC容器继承体系中的ConfigurableBeanFactory接口中定义了注册方法:
- /**
- * Add a new BeanPostProcessor that will get applied to beans created
- * by this factory. To be invoked during factory configuration.
- * <p>Note: Post-processors submitted here will be applied in the order of
- * registration; any ordering semantics expressed through implementing the
- * {@link org.springframework.core.Ordered} interface will be ignored. Note
- * that autodetected post-processors (e.g. as beans in an ApplicationContext)
- * will always be applied after programmatically registered ones.
- * @param beanPostProcessor the post-processor to register
- */
- void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);
另外,不要将BeanPostProcessor标记为延迟初始化。因为如果这样做,Spring容器将不会注册它们,自定义逻辑也就无法得到应用。假如你在<beans />元素的定义中使用了'default-lazy-init'属性,请确信你的各个BeanPostProcessor标记为'lazy-init="false"'。
InstantiationAwareBeanPostProcessor
- package org.springframework.beans.factory.config;
- import java.beans.PropertyDescriptor;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.PropertyValues;
- public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
- Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;
- boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;
- PropertyValues postProcessPropertyValues(
- PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
- throws BeansException;
- }
其使用方法与上面介绍的BeanPostProcessor接口类似,只时回调时机不同。
Spring中BeanPostProcessor相关推荐
- 【Spring】Spring中BeanPostProcessor
1.概述 转载:Spring中BeanPostProcessor 对文章:SpringBoot : 定制化Bean的利器:BeanPostProcessor & BeanFactoryPost ...
- Spring中BeanPostProcessor 执行过程
1.刷新容器 2.在refresh()方法中 执行 // Instantiate all remaining (non-lazy-init) singletons. // 初始化剩下的非延迟加载(no ...
- Spring中的后置处理器BeanPostProcessor讲解
Spring中提供了很多PostProcessor供开发者进行拓展,例如:BeanPostProcessor.BeanFactoryPostProcessor.BeanValidationPostPr ...
- 9种设计模式在Spring中的运用,一定要非常熟练!
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:iCoding91 https://blog.csdn.ne ...
- 一文读懂Spring中的AOP机制
一.前言 这一篇我们来说一下 Spring 中的 AOP 机制,为啥说完注解的原理然后又要说 AOP 机制呢? 1.标记日志打印的自定义注解 @Target({ElementType.METHOD}) ...
- Spring 中的各种注解,光会用可不够哦!
来源:https://digdeep.cnblogs.com/digdeep/p/4525567.html 1. Java中的注解 2. 使用 元注解 来自定义注解 和 处理自定义注解 3. spri ...
- spring中那些让你爱不释手的代码技巧
紧接上文<spring中这些能升华代码的技巧,可能会让你爱不释手>.本文继续总结我认为spring中还不错的知识点,希望对您有所帮助. 一. @Conditional的强大之处 不知道你们 ...
- spring中这些能升华代码的技巧,可能会让你爱不释手
前言 最近越来越多的读者认可我的文章,还是件挺让人高兴的事情.有些读者私信我说希望后面多分享spring方面的文章,这样能够在实际工作中派上用场.正好我对spring源码有过一定的研究,并结合我这几年 ...
- 【Spring源码】Spring中的AOP底层原理分析
AOP中的几个概念 Advisor 和 Advice Advice,我们通常都会把他翻译为通知,其实很不好理解,其实他还有另外一个意思,就是"建议",我觉得把Advice理解为&q ...
最新文章
- Halcon算子翻译——while
- Linux下两个进程可以同时打开同一个文件,这时如下描述错误的是:
- 如何在SQL Server中使用触发器
- Oracle数据库实用脚本
- apache起步命令加-k参数和不加的区别
- pdf转换成可在线浏览的电子杂志zmaker_pdf
- 一位程序员 8 年的物联网奋斗史
- 《深入浅出统计学》笔记二--第二章:集中趋势的量度,第三章:分散性与变异性的量度
- 掩膜裁剪tif步骤_ENVI中掩膜掩膜操作及影像分类教程
- “奋斗者”号下潜10909米:我们为什么要做深海探索?
- 游戏建模:模型场景制作过程分享
- 冷静 仔细 认真 分析
- Java集合(一):集合的概念
- Tailscale组成局域网(以文明6联机为例)
- 无力回天...机关算尽,还是死在上线之中.............
- 3.5mm耳机、麦克风的插座、插头定义
- Windows 系统错代码
- 【备战2020】高考数学全套知识点
- 2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(2)
- VsCode插件之vscode-icons
热门文章
- C语言二叉搜索树返回key的树级(附完整源码)
- QT绘制堆叠水平条形图
- QT的QParameter类的使用
- c++STL容器的Vector
- mysql5.5 配置_MySQL5.5 安装配置方法教程
- android自定义url协议,Android自定义URL方案…?
- MapReduce原理与设计思想(转载:http://blog.jobbole.com/80619/)
- Oracle数据库表信息,序列,视图等导出,导入。(数据库备份和恢复)
- 用 Freemarker 生成 word 文档
- 2进程之间的关系:进程组,会话,守护进程