Spring Bean 生命周期测试
Bean的生命周期是开始创建到销毁的过程。需要实现相关的类BeanNameAware ,DisposableBean, InitializingBean ,并注册InstantiationAwareBeanPostProcessor。
Bean类实现BeanNameAware ,DisposableBean, InitializingBean 接口
package com.bean.life.entity; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.stereotype.Component; @Component
public class User implements BeanFactoryAware
, BeanNameAware
, InitializingBean
, DisposableBean
{
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware setBeanFactory");
} @Override
public void setBeanName(String s) {
System.out.println("BeanNameAware接口: setBeanName = " + s);
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean接口: afterPropertiesSet");
} @Override
public void destroy() throws Exception {
System.out.println("DisposableBean接口: destroy"); }
@PostConstruct
public void init(){
System.out.println("PostConstruct");
} @PreDestroy
public void destory(){
System.out.println("PreDestroy");
}
}
注册InstantiationAwareBeanPostProcessor接口。
这里的InstantiationAwareBeanPostProcessorAdapter是 InstantiationAwareBeanPostProcessor 的子孙类。
package com.bean.life.entity; import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Component; import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor; @Component
public class MyInstantiationAwareBeanPostProcessor extends
InstantiationAwareBeanPostProcessorAdapter { public MyInstantiationAwareBeanPostProcessor() {
super();
} @Override
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException { if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: determineCandidateConstructors: " + beanName);
} return super.determineCandidateConstructors(beanClass, beanName);
} @Override
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: getEarlyBeanReference: " + beanName);
} return super.getEarlyBeanReference(bean, beanName);
} @Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInstantiation: " + beanName);
} return super.postProcessBeforeInstantiation(beanClass, beanName);
} @Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInstantiation : " + beanName);
} return super.postProcessAfterInstantiation(bean, beanName);
} @Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessPropertyValues: " + beanName);
}
return super.postProcessPropertyValues(pvs, pds, bean, beanName);
} @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInitialization: " + beanName);
} return super.postProcessBeforeInitialization(bean, beanName);
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInitialization: " + beanName);
} return super.postProcessAfterInitialization(bean, beanName);
}
}
输出
MyBeanFactoryPostProcessor --- BeanFactoryPostProcessor
MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInstantiation: user
MyInstantiationAwareBeanPostProcessor接口: determineCandidateConstructors: user
MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInstantiation : user
MyInstantiationAwareBeanPostProcessor接口: postProcessPropertyValues: user
BeanNameAware接口: setBeanName = user
BeanFactoryAware setBeanFactory
MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInitialization: user
PostConstruct
InitializingBean接口: afterPropertiesSet
MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInitialization: user PreDestroy
DisposableBean接口: destroy
生命周期图解

Spring 实现按 init-method 和destory-method的三种方式
- 方式1:xml配置方式
package com.bean.life.entity; public class User
{ public void init(){
System.out.println("PostConstruct");
} public void destory(){
System.out.println("PreDestroy");
}
}
<bean id="user" class="com.bean.life.entity" init-method="init" destroy-method="destroy">
</bean>
- 方式2:注解配置
添加@PostConstruct 和@PreDestroy进行指定
package com.bean.life.entity; @Component
public class User
{ @PostConstruct
public void init(){
System.out.println("PostConstruct");
} @PreDestroy
public void destory(){
System.out.println("PreDestroy");
}
}
- 方式3: 使用注解@Bean
public @interface Bean {
@AliasFor("name")
String[] value() default {};
@AliasFor("value")
String[] name() default {};
Autowire autowire() default Autowire.NO;
String initMethod() default "";
String destroyMethod() default "(inferred)";
}
@Configuration
public class UserConfig{ @Bean(initMethod="init",destoryMethod="destory")
public User user(){
return new User();
} }
Spring Bean 生命周期测试的更多相关文章
- Spring点滴四:Spring Bean生命周期
Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...
- 大厂高频面试题Spring Bean生命周期最详解
Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...
- Spring Bean生命周期,好像人的一生。。
大家好,我是老三,上节我们手撸了一个简单的IOC容器五分钟,手撸一个Spring容器!,这节我们来看一看Spring中Bean的生命周期,我发现,和人的一生真的很像. 简单说说IoC和Bean IoC ...
- spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理
1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...
- Spring Bean 生命周期之destroy——终极信仰
上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...
- 常见问题:Web/Servlet生命周期与Spring Bean生命周期
Servlet生命周期 init()初始化阶段 Servlet容器加载Servlet(web.xml中有load-on-startup=1;Servlet容器启动后用户首次向Servlet发请求;Se ...
- 睡前聊一聊"spring bean 生命周期"
spring bean 生命周期=实属初销+2个常见接口+3个Aware型接口+2个生命周期接口 实属初销:spring bean生命周期只有四个阶段,即实例化->属性赋值->初始化-&g ...
- Spring Bean 生命周期2
在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使用Sin ...
- Spring bean 生命周期验证
一.从源码注释看bean生命周期 从JDK源码上看,BeanFactory实现类需要支持Bean的完整生命周期,完整的初始化方法及其标准顺序(格式:接口 方法)为: 1.BeanNameAware s ...
随机推荐
- 找不到 blog.csdn.net 的服务器 DNS 地址
找不到 blog.csdn.net 的服务器 DNS 地址 csdn的博客用win7的电脑打不开是怎么回事?手机可以正常打开,csdn的bbs 下载什么的都可以正常使用. blog.csdn.net显 ...
- DCOS实践分享(4):如何基于DC/OS整合SMACK(Spark, Mesos, Akka, Cassandra, Kafka)
这篇文章入选CSDN极客头条 http://geek.csdn.net/news/detail/71572 当前,要保证业务的市场竞争力,仅靠设计一个可用并且好看的产品,已经完全不能满足要求.全球消费 ...
- 测试APPEND INSERT是否产生UNDO信息的过程
D:\>sqlplus test/testSQL*Plus: Release 11.1.0.6.0 - Production on 星期三 06月 29 19:46:41 2016Copyrig ...
- golang string和[]byte的对比
golang string和[]byte的对比 为啥string和[]byte类型转换需要一定的代价?为啥内置函数copy会有一种特殊情况copy(dst []byte, src string) in ...
- BZOJ_4002_[JLOI2015]有意义的字符串_矩阵乘法
BZOJ_4002_[JLOI2015]有意义的字符串_矩阵乘法 Description B 君有两个好朋友,他们叫宁宁和冉冉.有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求 Input 一行 ...
- hibernate深度学习 游离状态 HQL
当我学完这个之后 我仿佛都懂了 = =或许这就是 hibernate的力量吧. 操纵持久化对象(Session) 1.1. 在hibernate中java对象的状态 Hibernate 把对象分为 4 ...
- ReentrantLock之非公平锁源码分析
本文分析的ReentrantLock所对应的Java版本为JDK8. 在阅读本文前,读者应该知道什么是CAS.自旋. 由于ReentrantLock的公平锁和非公平锁中有许多共同代码,本文只会对这两种 ...
- python接口自动化(二十四)--unittest断言——中(详解)
简介 上一篇通过简单的案例给小伙伴们介绍了一下unittest断言,这篇我们将通过结合和围绕实际的工作来进行unittest的断言.这里以获取城市天气预报的接口为例,设计了 2 个用例,一个是查询北京 ...
- Python全国二级等级考试(2019)
一.前言 2018年9月随着全国计算机等级考试科目中加入“二级Python”,也确立了Python在国内的地位,猪哥相信Python语言势必会像PS那般普及.不久的将来,谁会Python谁就能获得女神 ...
- vue.js框架原理浅析
vue.js是一个非常优秀的前端开发框架,不是我说的,大家都知道. 首先我现在的能力,独立阅读源码还是有很大压力的,所幸vue写的很规范,通过方法名基本可以略知一二,里面的原理不懂的地方多方面查找资料 ...