一、方式一,使用ApplicationListener<E extends ApplicationEvent>监听ContextRefreshedEvent事件

/**
* @author zhangboqing
* @date 2019-11-03
*/
@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private MyService myService; @Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
System.out.println(">>>>>>>>>>>>>> ApplicationListener:" + myService.sayHello());
}
}

二、方式二,使用SmartInitializingSingleton

/**
* @author zhangboqing
* @date 2019-11-03
*/
@Component
public class MySmartInitializingSingleton implements SmartInitializingSingleton {
@Autowired
private MyService myService; @Override
public void afterSingletonsInstantiated() {
System.out.println(">>>>>>>>>>>>>> SmartInitializingSingleton:" + myService.sayHello());
}
}

三、方式三,使用SmartLifecycle

/**
* @author zhangboqing
* @date 2019-11-03
*/
@Component
public class MySmartLifecycle implements SmartLifecycle { @Autowired
private MyService myService; @Override
public void start() {
System.out.println(">>>>>>>>>>>>>> SmartLifecycle:" + myService.sayHello());
} @Override
public boolean isAutoStartup() {
return true;
} @Override
public void stop(Runnable callback) { } @Override
public void stop() { } @Override
public boolean isRunning() {
return false;
} @Override
public int getPhase() {
return ;
}
}

四、方式四,使用ApplicationRunner

/**
* @author zhangboqing
* @date 2019-11-07
*/
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Autowired
private MyService myService; @Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(">>>>>>>>>>>>>> ApplicationRunner:" + myService.sayHello());
}
}

五、方式五,使用CommandLineRunner

/**
* @author zhangboqing
* @date 2019-11-07
*/
@Component
public class MyCommandLineRunner implements CommandLineRunner { @Autowired
private MyService myService; @Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>> CommandLineRunner:" + myService.sayHello());
}
}

【Spring Boot】Spring Boot之五种容器启动后进行相关应用初始化操作方法的更多相关文章

  1. spring boot, 容器启动后执行某操作

    常有在spring容器启动后执行某些操作的需求,现做了一个demo的实现,做一下记录,也希望可以给需要的同学提供参考. 1.spring启动后,以新线程执行后续需要的操作,所以执行类实现Runnabl ...

  2. Spring源码分析专题 —— IOC容器启动过程(上篇)

    声明 1.建议先阅读<Spring源码分析专题 -- 阅读指引> 2.强烈建议阅读过程中要参照调用过程图,每篇都有其对应的调用过程图 3.写文不易,转载请标明出处 前言 关于 IOC 容器 ...

  3. web容器启动后自动执行程序的几种方式比较

    1.       背景 1.1.       背景介绍 在web项目中我们有时会遇到这种需求,在web项目启动后需要开启线程去完成一些重要的工作,例如:往数据库中初始化一些数据,开启线程,初始化消息队 ...

  4. Centos7 docker容器启动后添加端口映射

    docker容器启动后添加端口映射的两种方法: 一.通过修改防火墙策略添加端口映射 docker容器已创建好,但是想在容器内配置tomcat监控,需要新的端口去访问,但是映射时没有映射多余端口,此时, ...

  5. docker容器启动后添加端口映射

    DOCKER 给运行中的容器添加映射端口 方法1 1.获得容器IP 将container_name 换成实际环境中的容器名 docker inspect `container_name` | grep ...

  6. web容器启动加载WebApplicationContext和初始化DispatcherServlet

    原文地址:http://blog.csdn.net/zghwaicsdn/article/details/51186915 ContextLoaderListener监听器,加载ROOT WebApp ...

  7. @EnableAsync annotation metadata was not injected Spring容器启动后访问Servlet报错

    @EnableAsync annotation metadata was not injected 2015年12月20日 20:06:54 7570 在初始化spring事务部分碰到该错误, 详细错 ...

  8. docker 容器启动后立马退出的解决方法

    原因: 容器同时只能管理一个进程,如果这个进程结束了容器就退出了,但是不表示容器只能运行一个进程(其他进程可在后台运行),但是要使容器不退出必须要有一个进程在前台执行.   解决方案: 启动脚本最后一 ...

  9. zookeeper启动后没有相关进程

    查看状态报错,报错,百度硕士nc问题,让看.out文件,但是这哥文件是空的,那就看log 016-12-15 14:08:19,355 [myid:] - INFO [main:QuorumPeer$ ...

随机推荐

  1. TensorFlow常用激活函数及其特点和用法(6种)详解

    http://c.biancheng.net/view/1911.html 每个神经元都必须有激活函数.它们为神经元提供了模拟复杂非线性数据集所必需的非线性特性.该函数取所有输入的加权和,进而生成一个 ...

  2. vue.config.js 配置 scss,less,sass全局配置 vuecli3

    module.exports = { /* 部署生产环境和开发环境下的URL:可对当前环境进行区分,baseUrl 从 Vue CLI 3.3 起已弃用,要使用publicPath */ public ...

  3. vb.net 判断某个文件是否已经打开了

    '   判断这个excel文件是否已经打开了: 如果打开了,不能下载 Try Dim fs AsFileStream = NewFileStream(excelFileName, FileMode.O ...

  4. Salesforce 开发整理(三)权限共享

    Salesforce提供对象的访问权限可以通过 安全性控制 → 共享设置,可以查看每个对象在系统内部默认的访问权限 共用读写:对象的记录任何用户都可以进行读写操作 公用只读:对象的记录任何用户都可以查 ...

  5. Spring 事物隔离级别,事物传播行为

    Spring 框架中对于事物的管理,主要定义了一下四种属性: 事物的隔离(Isolation)级别 事物的传播行为(Propagation Behavior) 事物的超时时间(TImeout) 是否为 ...

  6. springboot修改页面不用重启的设置(idea)

       1) “File” -> “Settings” -> “Build,Execution,Deplyment” -> “Compiler”,选中打勾 “Build project ...

  7. 【Java】15分钟快速体验阿里Java诊断工具Arthas

    [墙裂推荐]15分钟快速体验阿里Java诊断工具Arthas : https://alibaba.github.io/arthas/arthas-tutorials?language=cn&i ...

  8. Orm 常见查询实例

    一.Moon.Orm框架总述 (您还用hibernate?实体框架?) 1.框架名:Moon 意思是月亮,而非Mono.因为很喜欢明月,所以以此为名.它是一个.NET下的Orm框架. 2.发展历史:历 ...

  9. this指北 (一篇读懂)

    this 关键字 涵义 this关键字是一个非常重要的语法点.毫不夸张地说,不理解它的含义,大部分开发任务都无法完成. 前一章已经提到,this可以用在构造函数之中,表示实例对象.除此之外,this还 ...

  10. Spring的JdbcTemplate使用教程

    Spring对数据库的操作在jdbc上面做了基本的封装,让开发者在操作数据库时只需关注SQL语句和查询 结果处理器,即可完成功能(当然,只使用JdbcTemplate,还不能摆脱持久层实现类的编写). ...