Spring中Bean的生命周期实例解析

 更新时间:2023年12月01日 08:41:11   作者:MC-闰土  
这篇文章主要介绍了Spring中Bean的生命周期实例解析,我们定义一个自定义的MySpringBeanPostProcessor,主要是重写了BeanPostProcessor接口的postProcessBeforeInitialization与postProcessAfterInitialization方法,需要的朋友可以参考下

Bean的生命周期

spring bean的生命周期官方的流程图如下:

接下来 我们用代码验证一下是否如图所愿:

首先定义一个testBean

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
 *
 * @author runtu
 * @version TestBean, v2.0 2019/12/13 9:19
 **/
public class TestBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
                      InitializingBean, DisposableBean {
    public TestBean() {
        System.out.println("=====================构造方法========================");
    }
    @Override
    public void setBeanName(String s) {
        System.out.println("=====================BeanNameAware========================");
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("=====================BeanFactoryAware========================");
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("=====================ApplicationContextAware========================");
    }
    @Override
    public void afterPropertiesSet() {
        System.out.println("=====================InitializingBean========================");
    }
    @Override
    public void destroy() {
        System.out.println("=====================DisposableBean========================");
    }
    public void init() {
        System.out.println("=====================init========================");
    }
    public void shutdown() {
        System.out.println("=====================shutdown========================");
    }
}

再定义一个自定义的MySpringBeanPostProcessor,主要是重写了BeanPostProcessor接口的postProcessBeforeInitialization与postProcessAfterInitialization方法。

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
 *
 * @author runtu
 * @version MySpringBeanPostProcessor, v2.0 2019/12/13 9:25
 **/
@Component
public class MySpringBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean,
                                                  String beanName) throws BeansException {
        if (bean instanceof TestBean) {
            System.out.println(
                "=====================postProcessBeforeInitialization========================");
        }
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean,
                                                 String beanName) throws BeansException {
        if (bean instanceof TestBean) {
            System.out.println(
                "=====================postProcessAfterInitialization========================");
        }
        return bean;
    }
}

此处我工程为springboot,把TestBean注入之后,启动项目

@Bean(initMethod = "init", destroyMethod = "shutdown")
public TestBean testBean() {
     return new TestBean();
}

把一些无关的日志剃掉之后,控制台打印结果如下:

# 启动程序之后控制台依次打印:
=====================构造方法========================
=====================BeanNameAware========================
=====================BeanFactoryAware========================
=====================ApplicationContextAware========================
=====================postProcessBeforeInitialization========================
=====================InitializingBean========================
=====================init========================
=====================postProcessAfterInitialization========================
 # 停止程序之后依次打印:
=====================DisposableBean========================
=====================shutdown========================

到此这篇关于Spring中Bean的生命周期实例解析的文章就介绍到这了,更多相关Bean的生命周期内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 关于java.lang.NumberFormatException: null的问题及解决

    关于java.lang.NumberFormatException: null的问题及解决

    这篇文章主要介绍了关于java.lang.NumberFormatException: null的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • github上的java项目怎么运行(面向小白)

    github上的java项目怎么运行(面向小白)

    这篇文章主要介绍了github上的java项目怎么运行(面向小白),今天从github把我以前写的一个小demo下载下来了,第一次下载项目,摸索了一个多小时,才运行起来,需要的朋友可以参考下
    2019-06-06
  • Spring Boot 日志功能深度解析与实践指南

    Spring Boot 日志功能深度解析与实践指南

    本文详细介绍了SpringBoot的日志功能,包括默认日志框架Logback,日志级别配置,日志格式自定义,日志文件输出,日志归档与清理,自定义日志配置,与其他日志框架的集成以及日志性能优化,通过结合实际场景,提供了详细的配置与实践指南,感兴趣的朋友一起看看吧
    2025-01-01
  • 详解快速排序算法中的区间划分法及Java实现示例

    详解快速排序算法中的区间划分法及Java实现示例

    这篇文章主要介绍了详解快速排序算法中的区间划分法及Java实现示例,文中分别介绍了快排时两种区间划分的思路,需要的朋友可以参考下
    2016-04-04
  • JavaWeb实现表单提交的示例详解

    JavaWeb实现表单提交的示例详解

    这篇文章主要介绍了如何利用JavaWeb实现表单提交功能,文中的示例代码讲解详细,对我们学习JavaWeb有一定帮助,感兴趣的可以了解一下
    2022-03-03
  • SpringBoot中使用AOP切面编程实现登录拦截功能

    SpringBoot中使用AOP切面编程实现登录拦截功能

    本文介绍了如何使用AOP切面编程实现Spring Boot中的登录拦截,通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2024-12-12
  • SpringBoot中如何统一接口返回与全局异常处理详解

    SpringBoot中如何统一接口返回与全局异常处理详解

    全局异常处理是个比较重要的功能,一般在项目里都会用到,这篇文章主要给大家介绍了关于SpringBoot中如何统一接口返回与全局异常处理的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-09-09
  • 在MyBatis中开启SQL日志的具体步骤

    在MyBatis中开启SQL日志的具体步骤

    文章详细介绍了在原生MyBatis和SpringBoot中开启MyBatis日志的具体步骤,包括引入日志依赖、配置日志文件和设置日志级别,文章还提供了对比总结和常见问题解答,帮助读者更好地理解和配置MyBatis日志,需要的朋友可以参考下
    2025-11-11
  • JavaEE idea的smart tomcat插件使用

    JavaEE idea的smart tomcat插件使用

    这篇文章主要介绍了JavaEE idea的smart tomcat插件使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-09-09
  • Java语言字典序排序算法解析及代码示例

    Java语言字典序排序算法解析及代码示例

    这篇文章主要介绍了Java语言字典序排序算法解析及代码示例,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01

最新评论