八、spring生命周期之BeanPostProcessor
BeanPostProcessor我们一般称为Bean的后置处理器,它与我们前面介绍的InitialingBean
、init-method
等一样,都是在bean的初始化时被调用,具体的用法我们在举例中进行学习。
要使用BeanPostProcessor
非常简单,只要实现这个接口即可。
在此之前,我们来先看下我们的Cat
类,我们的目的是将这个Cat注册进spring的ioc容器中,并让这个BeanPostProcessor起到作用。
public class Cat implements InitializingBean,DisposableBean {
public Cat(){
System.out.println("cat constructor...");
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("cat...destroy...");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("cat...afterPropertiesSet...");
}
//对象创建并赋值之后调用
@PostConstruct
public void initMethod(){
System.out.println("Cat....@PostConstruct...");
}
//容器移除对象之前
@PreDestroy
public void detoryMethod(){
System.out.println("Cat....@PreDestroy...");
}
public void initCat() {
System.out.println("Cat.initCat()......");
}
public void cleanup() {
System.out.println("Cat.cleanup()......");
}
}
再来看下我们的配置类
@Configuration
public class MainConfigOfLifeCycle {
// @Scope("prototype")
@Bean(initMethod="initCat",destroyMethod="cleanup")
public Cat cat(){
return new Cat();
}
@Bean
public MyBeanPostProcessor myBeanPostProcessor() {
return new MyBeanPostProcessor();
}
}
结合之前学习过的内容, 就能知道我们为这个Cat类应用了三种初始化及销毁方法,
分别是@PostConstruct
和@Predestroy
还有就是实现 InitializingBean,DisposableBean这两个接口的afterPropertiesSet()
和destro()
方法
另外就是标注在@Bean(initMethod="initCat",destroyMethod="cleanup")定义的两个方法,也存在于Cat类中,
现在我们就要加入BeanPostProcessor
/**
* 后置处理器:初始化前后进行处理工作
* 将后置处理器加入到容器中,只需要继承BeanPostProcessor接口并实现其两个方法即可。
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
return bean;
}
}
现在一切具备,再写一个测试方法:
@Test
public void test01(){
//1、创建ioc容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("容器创建完成...");
//applicationContext.getBean("car");
//关闭容器
applicationContext.close();
}
观察控制台打印结果
cat constructor...// 创建cat对象
postProcessBeforeInitialization...cat=>com.atguigu.bean.Cat@6591f517 //在所有初始化方法之前被调用
Cat....@PostConstruct...
cat...afterPropertiesSet...
Cat.initCat()......
postProcessAfterInitialization...cat=>com.atguigu.bean.Cat@6591f517// 在所有初始化方法之后被调用
容器创建完成...
Cat....@PreDestroy...// 销毁方法不需要关注
cat...destroy...
Cat.cleanup()......
通过上面这个例子,已经很显然BeanPostProcessor的用法,它是在容器中bean初始化前后被调用。
源码调用分析:
我们在eclipse中启用debug模式,
看看这个方法的源码,直接在方法注释中进行分析
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
invokeAwareMethods(beanName, bean);
return null;
}
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
// 初始化前调用BeanPostProcessor
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
//初始化方法
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;
}
就看到这里,里面内容比较多,可以进invokeInitMethods看看几个初始化方法的调用顺序,JSR-250注解@PostConstruct
是如何起作用的等等 ,我们这里只是来源码中找找它在源码中的位置。
又是一个夜晚,一船的清梦。
八、spring生命周期之BeanPostProcessor的更多相关文章
- spring生命周期
Github地址 最近在整合mybatis-spring. 公司里面已经有一个叫做kylin-datasource的开发包,以前能够提供master和slave2个数据源,最近更新了2.0版本,支持自 ...
- 说下spring生命周期
面试官:说下spring生命周期 程序员:不会 那你先回去等消息吧 Bean实现了BeanNameAware,Spring会将Bean的ID透传给setBeanName java.后端开发.程 ...
- Spring生命周期详解
导读 Spring中Bean的生命周期从容器的启动到停止,涉及到的源码主要是在org.springframework.context.support.AbstractApplicationContex ...
- 【源码】spring生命周期
一.spring生命周期 1. 实例化Bean 对于BeanFactory容器,当客户向容器请求一个尚未初始化的bean时,或初始化bean的时候需要注入另一个尚未初始化的依赖时,容器就会调用crea ...
- Spring学习总结(4)-Spring生命周期的回调
参考文档:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans ...
- spring源码学习(四)-spring生命周期用到的后置处理器
生命周期的九大后置处理器 第一次调用后置处理器org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory# ...
- Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method
项目中用到了 afterPropertiesSet: 于是具体的查了一下到底afterPropertiesSet到底是什么时候执行的.为什么一定要实现 InitializingBean; **/ @C ...
- spring 生命周期最详解
转载. https://blog.csdn.net/qq_23473123/article/details/76610052 目的 在大三开始学习spring时,老师就说spring bean周期非常 ...
- spring生命周期流程图
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
随机推荐
- BZOJ 4103: [Thu Summer Camp 2015]异或运算 可持久化trie
开始想了一个二分+可持久化trie验证,比正解多一个 log 仔细思考,你发现你可以直接按位枚举,然后在可持久化 trie 上二分就好了. code: #include <bits/stdc++ ...
- 2019.12.09 java循环(do……while)
class Demo05{ public static void main(String[] args) { int sum=0; int i=1; do{ sum+=i; i++; }while(i ...
- rustup 使用
rustup 可以帮助我们安装不同版本的rust 编程需要的工具连,同时可以方便的进行不同版本 之间的切换,类似nodejs 的nvm,n, ruby 的 rvm python 的 venv ... ...
- circus 做为批处理的守护进程
circus 是集成了zeromq,使用python编写的一个进程以及socket 管理工具,使用circus 的进程管理,我们可以用来进行批任务的 处理,同时又能保证任务的准确 项目使用docker ...
- 洛谷 题解 UVA572 【油田 Oil Deposits】
这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...
- 【JZOJ6227】【20190621】ichi
题目 $n , m ,d,x\le 10^5 , $强制在线 题解 对原树做dfs,得到原树的dfs序 对kruksal重构树做dfs,得到重构树的dfs序 那么就是一个三维数点问题 强制在线并且卡空 ...
- 编译.tex文件时出现的问题
Error: “the second byte of the cjk code is out of range” 发生在使用pdflatex和bibtex时.可能是.bib文档中出现了中文字符,包括中 ...
- last-child为啥不生效
last-child基本定义 指定属于其父元素的最后一个子元素的 p 元素的背景色 如果你这样写是不会生效的 <div class="limit-border"> &l ...
- pytorch中tensor数据和numpy数据转换中注意的一个问题
转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ...
- debian源配置实例
debian9 官方源: deb http://ftp.debian.org/debian stretch main contribdeb http://ftp.debian.org/debian s ...