一、方式一,使用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. 搜索法 | 1091bfs搜索:三维bfs

    首先我们来理解样例输入: 尺寸:3行4列5片   阈值:2 1 1 1 1 1 1 1 1 1 1 1 1 ------- 0 0 1 1 0 0 1 1 0 0 1 1 ------- 0 1 1 ...

  2. iptables 表和链的对应关系

    filter表 主要用于对数据包进行过滤,根据具体的规则决定是否放行该数据包(如DROP.ACCEPT.REJECT.LOG).filter 表对应的内核模块为iptable_filter,包含三个规 ...

  3. Java 并发系列之七:java 阻塞队列(7个)

    1. 基本概念 2. 实现原理 3. ArrayBlockingQueue 4. LinkedBlockingQueue 5. LinkedBlockingDeque 6. PriorityBlock ...

  4. Spring Security教程之Jsp标签(八)

    目录 1.1     authorize 1.2     authentication 1.3     accesscontrollist Spring Security也有对Jsp标签的支持的标签库 ...

  5. Orm 入门总指南

    注意:下面的pdf文件强烈建议下载或在线查看 1)旗舰版帮助文档点击查看或下载 2)http://pan.baidu.com/s/1hq7krFu(新手手册下载)(强烈推荐) 3)性能及规范下载,网友 ...

  6. linux ----------- 在VM上 的安装 centos

    1.虚拟机的安装步骤 2.什么是虚拟机 通过软件来模拟硬件的功能 3.主流的虚拟机 Vmware Workstation 简称Vmware 4.虚拟机下载地址https://www.vmware.co ...

  7. mySql使用手册-官方文档

    https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format oracle to_ ...

  8. 67 GC 垃圾回收机制

    GC是JVM中自带的一个线程 他会不定时来堆中回收那些不再使用的对象,以释放内存 可以通过System.gc()来主动建议jvm尽快调度gc来回收垃圾 不同的平台的jvm对GC的策略是有所不同的 我们 ...

  9. 『2019Summer Algorithms』

    一个暑假两次集训,感觉学了好多好多的东西,也挖了好多好多的坑,于是就决定写一篇关于算法的总结,用于熟悉新算法,也留下一点对新算法的理解. AC自动机 简单的说就是在\(trie\)树上实现\(KMP\ ...

  10. Mysql系列(二)—— Mysql支持的数据类型

    Mysql版本众多,每个版本支持的数据类型繁多且不一样,本篇文章中主要基于MySQL Community Server 5.7.22介绍常用的数据类型,包括其特点以及区别. 一.数据类型 正确的定义表 ...