本文代码GitHub地址

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 生命周期测试的更多相关文章

  1. Spring点滴四:Spring Bean生命周期

    Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...

  2. 大厂高频面试题Spring Bean生命周期最详解

    Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...

  3. Spring Bean生命周期,好像人的一生。。

    大家好,我是老三,上节我们手撸了一个简单的IOC容器五分钟,手撸一个Spring容器!,这节我们来看一看Spring中Bean的生命周期,我发现,和人的一生真的很像. 简单说说IoC和Bean IoC ...

  4. spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理

    1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...

  5. Spring Bean 生命周期之destroy——终极信仰

    上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...

  6. 常见问题:Web/Servlet生命周期与Spring Bean生命周期

    Servlet生命周期 init()初始化阶段 Servlet容器加载Servlet(web.xml中有load-on-startup=1;Servlet容器启动后用户首次向Servlet发请求;Se ...

  7. 睡前聊一聊"spring bean 生命周期"

    spring bean 生命周期=实属初销+2个常见接口+3个Aware型接口+2个生命周期接口 实属初销:spring bean生命周期只有四个阶段,即实例化->属性赋值->初始化-&g ...

  8. Spring Bean 生命周期2

    在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使用Sin ...

  9. Spring bean 生命周期验证

    一.从源码注释看bean生命周期 从JDK源码上看,BeanFactory实现类需要支持Bean的完整生命周期,完整的初始化方法及其标准顺序(格式:接口 方法)为: 1.BeanNameAware s ...

随机推荐

  1. MonolithFirst

    As I hear stories about teams using a microservices architecture, I've noticed a common pattern. Alm ...

  2. Docker for windows on VMware

    工作环境 操作系统:Windows 10 Pro x64 Hyper-V:已卸载. VMware:已安装. Virtual Box:无安装. 前言 鉴于Hyper-V在Windows桌面系统下的&qu ...

  3. genymotion中app打开后屏幕是倒的问题

    屏幕是倒的是因为你使用了虚拟机的默认分辨率. 解决办法: 选择需要的设置的虚拟机,点击后面的设置按钮 弹窗中选择其他的分辨率 如果之前是1024x600,前面的数值大于后面的.则换成前面的数值小于后面 ...

  4. 在MFC中对Excel的一些操作

    首先要在程序中加载CExcel.h和CExcel.cpp文件,这里面包装了很多函数和对Excel文件的操作,下面所有程序中的m_excel都是类CExcel的对象,如: private: _Appli ...

  5. 【codeforces 698B】 Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...

  6. linux清除全屏快捷键(Ctrl+L)

    Linux用户基本上都习惯使用clear命令或Ctrl+L组合快捷键来清空终端屏幕.这样做其实并没有真正地清空屏幕,但当用鼠标向上滚时,你仍然能看到之前的命令操作留下来的输出.

  7. Appium 【已解决】提示报错:Attempt to re-install io.appium.android.ime without first uninstalling.

    详细报错:Failed to install D:\AutoTest\appium\Appium\node_modules\appium\build\unicode_ime_apk\UnicodeIM ...

  8. MYSQL—— 基础入门,select 查询涉及到的关键字组合详解(进阶篇)

    SELECT查询组合使用的关键字很多,首先将最简单常用的关键字进行区分及使用,后续再继续补充............ 以下所有的关键字组合使用,主要以两个表students与students_scor ...

  9. Boosting(提升方法)之GBDT

    一.GBDT的通俗理解 提升方法采用的是加法模型和前向分步算法来解决分类和回归问题,而以决策树作为基函数的提升方法称为提升树(boosting tree).GBDT(Gradient Boosting ...

  10. 【官网翻译】性能篇(四)为电池寿命做优化——使用Battery Historian分析电源使用情况

    前言 本文翻译自“为电池寿命做优化”系列文档中的其中一篇,用于介绍如何使用Battery Historian分析电源使用情况. 中国版官网原文地址为:https://developer.android ...