Spring中Bean创建完成后执行指定代码的几种实现方式

  • 1、 实现ApplicationListener接口
  • 2、 实现InitializingBean接口
  • 3、 使用@PostConstruct注解

在实际开发中经常会遇到在spring容器加载完某个bean之后,需要执行一些业务代码的场景。比如初始化配置、缓存等。有以下几种方式可以实现此需求

1、 实现ApplicationListener接口

实现ApplicationListener接口并实现方法onApplicationEvent()方法,Bean在创建完成后会执行onApplicationEvent方法

@Component
public class DoByApplicationListener implements ApplicationListener<ContextRefreshedEvent> {public DoByApplicationListener() {System.out.println("DoByApplicationListener constructor");}@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {if (event.getApplicationContext().getParent() == null) {System.out.println("DoByApplicationListener do something");}}
}

2、 实现InitializingBean接口

实现InitializingBean接口并实现方法afterPropertiesSet(),Bean在创建完成后会执行afterPropertiesSet()方法

@Component
public class DoByInitializingBean implements InitializingBean {public DoByInitializingBean() {System.out.println("DoByInitializingBean constructor");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("InitByInitializingBean do something");}
}

3、 使用@PostConstruct注解

在Bean的某个方法上使用@PostConstruct注解,Bean在创建完成后会执行该方法

@Component
public class DoByPostConstructAnnotation {public DoByPostConstructAnnotation() {System.out.println("DoByPostConstructAnnotation constructor");}@PostConstructpublic void init(){System.out.println("InitByPostConstructAnnotation do something");}
}

转载自:https://segmentfault.com/a/1190000019622443?utm_source=tag-newest

Spring中Bean创建完成后执行指定代码的几种实现方式相关推荐

  1. 介绍Spring Boot 启动时,自动执行指定方法的 7 种方法

    目录 前言 实现方式 1.实现ServletContextListener接口contextInitialized方法 2.静态代码块方式 3.@PostConstruct注解方式 4. 实现Serv ...

  2. Spring中Bean管理操作基于XML配置文件方法实现

    Spring中Bean管理操作基于XML配置文件方法实现 基于XML配置文件方式实现 1.基于`xml`方式创建对象 2.基于`xml`方式注入属性 1.创建类,定义属性和对应的set方法 2.在Sp ...

  3. Spring中bean的执行初始化和销毁方法的4种方式详解

    一.引入 在java的实际开发过程中,我们可能需要在spring实例化一个bean的过程中,使用到初始化一个对象(bean)后立即初始化(加载)一些数据,或者在销毁一个对象之前进行执行一些事情等等. ...

  4. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章

    前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...

  5. 面试问题:Spring中Bean 的生命周期

    Spring Bean生命周期执行流程 在传统的 Java 应用中,Bean 的生命周期很简单,使用关键字 new 实例化 Bean,当不需要该 Bean 时,由 Java 自动进行垃圾回收. Spr ...

  6. 面试题------Spring中Bean的初始化以及销毁init-method、destory-method

    面试题------Spring中Bean的生命周期 通过Spring工厂,可以控制bean的生命周期. 在xml中配置Bean的初始化和销毁方法 通过init-method属性指定初始化后的调用方法. ...

  7. Spring中bean的初始化和销毁几种实现方式详解

    关联博文:Spring中Bean的作用域与生命周期 Bean的生命周期 : 创建bean对象 – 属性赋值 – 初始化方法调用前的操作 – 初始化方法 – 初始化方法调用后的操作 – --- 销毁前操 ...

  8. Spring中bean标签的属性和值:

    Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...

  9. Spring 中 Bean 的生命周期

    本文作者: 伯乐在线 - yemengying 智商捉鸡?,实在没办法一下子理解Spring IoC和AOP的实现原理,看的闹心也不太懂,所以...决定拆成小的不能在小的一个个问题,一点点啃.今天先来 ...

最新文章

  1. 学好C++,一个项目就够
  2. angular4创建html,angular 4减少html内容
  3. ACM入门之【线段树习题】
  4. 中国城中村改造建设前景规划及投融资模式分析报告2022年版
  5. boost::spirit模块将 QString 数据类型用作 Qi 属性的测试程序
  6. 查看mongodb数据路径_Mac OS 中安装和使用 MongoDB 的方法
  7. git 合并多次commit
  8. 嵌入式linux系统中设备驱动程序
  9. PHP 使用 OSS上传文件
  10. verilog存小数_FPGA定点小数计算(Verilog版)第二篇——乘法运算
  11. 如何为Kafka挑选合适的分区数
  12. C语言考试题及答案(8),2015年计算机二级C语言测试题及答案(8)
  13. CoreAnimation-CABasicAnimation
  14. python django异步访问_初试Ajax异步请求(基于Django框架)
  15. java调用python机器学习模型的坑
  16. 小程序为什么有的方法要写在methods,有的可直接写在page下
  17. 零基础学Arcgis(六)|空间数据采集与管理(3)数据编辑
  18. Win11机械硬盘磁盘碎片整理方法
  19. 常用的PDF分割压缩软件有哪些?
  20. Win10电脑桌面上没有‘我的电脑’图标

热门文章

  1. 深度学习和目标检测系列教程 4-300:目标检测入门之目标变量和损失函数
  2. 数据分析如何入门,以及如何做职业规划?
  3. 我如何一分钱没花学完AI课程,入职新浪算法工程师
  4. 飞桨上线万能转换小工具,教你玩转TensorFlow、Caffe等模型迁移
  5. 数字图像处理与Python实现笔记
  6. Spring Boot-@PropertySource注解
  7. spring boot+mybatis-plus+SQL server调用有返回值的存储过程
  8. Vue 3 —— 当 data 和 ref 相同时 运行时错误 [ReferenceError: xxx is not defined]
  9. PaddleOCR——Visual Studio 2019 环境下C++推理部署 CMake 编译解决方案
  10. Intellij IDEA + Maven——jar项目改成war项目相互转换