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 ...
随机推荐
- 基于Spring Cloud、JWT 的微服务权限系统设计
基于Spring Cloud.JWT 的微服务权限系统设计 https://gitee.com/log4j/pig https://github.com/kioyong/spring-cloud-de ...
- 【基于url权限管理 shiro(一)】--基础
只要有用户参与的系统一般都要有权限管理,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己被授权的资源.权限管理包括用户认证和授权两部分. 用户认证 1.概 ...
- 图解HTTPS建立过程
阅读目录 准备工作(对应图中prepare1234) 发起链接 最后 关于网络安全加密的介绍可以看之前文章: 1. 网络安全——数据的加密与签名,RSA介绍2. Base64编码.MD5.SHA1-S ...
- python资料分享
python入门资料分享:链接:https://pan.baidu.com/s/1aATizMh5e0ON6xfmtxXPzA 密码:m8bf 提高资料:链接:https://pan.baidu.c ...
- IntelliJ IDEA 配置maven
以下内容引自http://blog.csdn.net/qq_32588349/article/details/51461182. 使用IntelliJ IDEA 配置Maven(入门) ...
- iOS指纹识别Touch ID的安全性探讨
苹果公司在 iPhone 5s 的发布会上公布了全新的指纹识别安全技术,也就是 Touch ID,开创了生物安全识别技术在便携设备上使用的新篇章.此后,苹果还将此技术带到了 iPad 上.此前没有任何 ...
- docker环境部署
docker环境部署 1 查看当前系统版本 只支持CentOS7版本的系统,如果不是的话,可以让项目方进行重装或者系统内核升级. [root@bogon bin]# cat /etc/redhat-r ...
- js实现 页面加载 完成 后顺序 执行
function addLoadEvent(func){ var oldonLoad = window.onload; if(typeof window.onload != 'function'){ ...
- python接口自动化(二十)--token登录(详解)
简介 为了验证用户登录情况以及减轻服务器的压力,减少频繁的查询数据库,使服务器更加健壮.有些登录不是用 cookie 来验证的,是用 token 参数来判断是否登录.token 传参有两种一种是放在请 ...
- 【JVM虚拟机】(5)---深入理解JVM-Class中常量池
深入理解Class---常量池 一.概念 1.jvm生命周期 启动:当启动一个java程序时,一个jvm实例就诞生了,任何一个拥有main方法的class都可以作为jvm实例运行的起点. 运行:mai ...