Spring系列(八):Spring生命周期中BeanPostProcessor接口用法介绍
今天给大家介绍BeanPostProcessor接口用法,希望对大家能有所帮助!
1、BeanPostProcessor 概念介绍
BeanPostProcessor接口通常被称为Bean的后置处理器,它是Spring中定义的接口,可以在Spring容器的创建过程中(主要在Bean初始化前后进行工作)回调BeanPostProcessor中定义的两个方法。
2、BeanPostProcessor接口方法
postProcessBeforeInitialization:在每一个bean对象的初始化方法调用之前回调。
postProcessAfterInitialization:在每个bean对象的初始化方法调用之后被回调。
说明:以上两个方法的返回值都不能为null,否则在后续的初始化方法会报空指针或者通过getBean()方法无法获取Bean对象。主要原因是如果返回值为null的情况下以上两个方法从Spring容器获取bean实例,没有再次放入Spring容器中去,这样容器中就没有了。
2.1 BeanPostProcessor源码内容
package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;public interface BeanPostProcessor {@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}
}
3、BeanPostProcessor 接口的作用
可以Spring容器中完成bean实例化、配置以及其他初始化方法前后根据业务的场景增加相应的逻辑处理。典型的案例AOP的实现。
4、代码示例
4.1 新建Person.java 类文件
package com.spring.bean;public class Person {private String name;private Integer age;private String address;public Person(String name, Integer age, String address) {this.name = name;this.age = age;this.address = address;}public Person() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age='" + age + '\'' +", address='" + address + '\'' +'}';}
}
4.2、新建MyBeanPostProcessor.java
package com.spring.bean;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;@Component
public class MyBeanPostProcessor implements BeanPostProcessor {/*** 实例化、依赖注入完毕,在调用显示的初始化之前完成一些自定义的初始化逻辑* 方法返回值不能为null* 返回null那么在后续初始化方法会报空指针异常或者通过getBean()方法获取不到bena实例* 如果设置为null情况下,后置处理器从Spring IoC容器中取出bean实例对象没有再次放回IoC容器中*/public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("postProcessBeforeInitialization执行了"+beanName);return bean;}/*** 实例化、依赖注入、初始化完成后时执行* 方法返回值也不能为null* 如果返回null那么在后续初始化方法将报空指针异常或者通过getBean()方法获取不到bena实例对象* 如果设置为null情况下,后置处理器从Spring IoC容器中取出bean实例对象没有再次放回IoC容器中*/public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("postProcessAfterInitialization"+beanName);return bean;}
}
4.3、新建TestBeanPostProcessorConfig.java 配置类
package com.spring.config;import com.spring.bean.UserInfo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;@Configuration
@ComponentScan(value = "com.spring.bean",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Component.class, Controller.class}),@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {UserInfo.class}),
})
public class TestBeanPostProcessorConfig {}
4.4、新建测试类 TestBeanPostProcessor.java
package com.spring.test;import com.spring.config.TestBeanPostProcessorConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestBeanPostProcessor {public static void main(String[] args) {AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestBeanPostProcessorConfig.class);}
}输出结果:postProcessBeforeInitialization执行了testBeanPostProcessorConfig
postProcessAfterInitializationtestBeanPostProcessorConfig
UserInfo构造器执行了
postProcessBeforeInitialization执行了userInfo
PostConstruct 初始化方法执行
postProcessAfterInitializationuserInfoProcess finished with exit code 0
IT技术分享社区
个人博客网站:https://programmerblog.xyz
文章推荐程序员效率:画流程图常用的工具程序员效率:整理常用的在线笔记软件远程办公:常用的远程协助软件,你都知道吗?51单片机程序下载、ISP及串口基础知识硬件:断路器、接触器、继电器基础知识
Spring系列(八):Spring生命周期中BeanPostProcessor接口用法介绍相关推荐
- Vue生命周期中对mounted、beforeUpdate、updated的理解
Vue生命周期中对mounted.beforeUpdate.updated的理解 前言 mounted.beforeUpdate.updated 前言 以下文章纯为个人理解,如有错误,请求评论区指正呀 ...
- Vue生命周期中的created和mounted的区别
之前大部分时候都是用angular开发项目,vue写的很少,最近有必要把vue和webpack重新整理一下了.下面我们先看一下Vue生命周期中的created和mounted的区别. 我们先看一张图( ...
- Vue生命周期中mounted、created、methods、computed、watched等的区别
1.Vue生命周期中mounted和created的区别 https://blog.csdn.net/xdnloveme/article/details/78035065. 2.[Vue] 生命周期, ...
- Activity生命周期中onStart()和onResume()的区别
Activity生命周期中onStart()和onResume()的区别 在讲onStart()和onResume()的区别之前,必须清楚Activity的四种状态: 1.Running状态:一个新的 ...
- Spring系列之Bean生命周期
本文主要以ApplicationContext和BeanFactory容器为例,介绍两者的生命周期流程. ApplicationContext ApplicationContext容器中,Bean的生 ...
- Spring系列之Spring框架和SpringAOP集成过程分析(十)
转载请注明出处:https://blog.csdn.net/zknxx/article/details/80724180 在开始这个系列之前大家先想一下我们是怎么在项目中使用SpringAOP的(这里 ...
- Spring系列之Spring Web MVC-20
目录 Spring Web MVC DispatcherServlet 上下文层次结构 特殊Bean Web MVC 配置 程序配置 工作原理 异常 视图解析 配置 重定向 转发 内容协商 过滤器 F ...
- Spring系列之Spring常用注解总结
参看博客:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺 ...
- Spring系列 1.Spring概述及IOP
Spring概述 简介 Spring : 春天 ->给软件行业带来了春天 2002年,Rod Jahnson首次推出了Spring框架雏形interface21框架. 2004年3月24日,Sp ...
最新文章
- 定了!5G商用牌照近期发放​​​​,透露两大信息(附:2019年5G行业关键材料及市场研究报告)...
- PMP之路 – 第2天 (做模拟题)
- rm -fr后的恢复
- 装机人员工具_吕梁采购气伏式包装机-哪家好-强盛包装机械
- 移动APP漏洞自动化检测平台建设
- 一段简单的python代码_Python趣味打怪:60秒学会一个例子,147段简单代码助你从入门到大师 | 中文资源...
- cudnn的下载地址
- Python中的堆实现:heapq 模块——利用堆结构实现快速访问数据流中的中位数
- 将Chrome设置为Jupyter_notebook的默认浏览器
- Jsoup代码解读之五-parser(中)
- 先装vs还是先装sql_【家装话题】装修师先装门还是先装地板?
- 平塘天眼和大数据有什么关系_贵州平塘的中国天眼,值得去吗?除了天眼,平塘还有什么好玩?...
- 【英语学习】【English L06】U07 Jobs L1 A computer programmer
- [修正] Firemonkey Windows 控件有虚线残影问题
- 硝烟中的Scrum和XP-我们如何实施Scrum 4 (Part 1/2)
- 稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器...
- Java之JMX 详解
- 译文(Artistic Style Transfer with Internal-external Learning and Contrastive Learning)
- KindEditor上传图片及使用
- ie加载项存在残留是什么_残余IE加载项无法修复
热门文章
- 邮件系统磁盘监控脚本
- python将ros下bag文件的所有topic解析为csv格式
- ioctl之FIONREAD
- xvid 数据编码和解码
- 数字后端——时序验证
- 【Python3 SelectKBest 调用personer出现的错误】
- 机器学习——SVM之交叉验证对参数(C,gamma)进行优化以及选择
- linux的进程与库之间的通信两种方式
- oracle 9.2.0.4,CentOS 4.7 安装Oracle 9.2.0.4的一些问题
- bat自动输入用户名和密码_「小白到大牛之路6」交换机后台管理之重复输入用户名和密码...