一、方式一,使用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. 【louguP2234】[HNOI2002]营业额统计(链表)

    题目链接 离线用链表维护,先按权值排序,建立链表,记录每一天在链表的位置,然后按天数从大到小查询,查询完删除 #include<algorithm> #include<iostrea ...

  2. Spring Security教程(四)

    在前面三个博客的例子中,登陆页面都是用的Spring Security自己提供的,这明显不符合实际开发场景,同时也没有退出和注销按钮,因此在每次测试的时候都要通过关闭浏览器来注销达到清除session ...

  3. MySQL5.7调优参数

    1. 更改MySQL Data File位置 datadir=/data/mysqlsocket=/data/mysql/mysql.sock 2. 调整OS参数 * soft nproc 10240 ...

  4. Android实时打印内核log

    由于访问的是/proc/kmsg,因此需要先获取root权限: adb root adb shell "while true; do cat; sleep 1; done < /pro ...

  5. python入门之函数的嵌套

    目录 函数的嵌套调用 函数的嵌套定义 函数的嵌套调用 在函数内调用函数 def index(): print('from index') def func(): index() print('from ...

  6. FastReport 程序员手册

    一.使用TfrxReport 组件工作1.加载并存储报表默认情况下,报表窗体同项目窗体构存储在同一个DFM文件中.多数情况下,无须再操作,因而你就不必采用特殊方法加载报表.如果你决定在文件中存储报表窗 ...

  7. pandas 学习 第十一篇:处理缺失值

    Pandas中的缺失值是指nan.None和NaT.如果需要把inf 和 -inf视为缺失值,需要设置 pandas的选项: pandas.options.mode.use_inf_as_na = T ...

  8. docker镜像上传阿里云仓库

    推送至阿里云镜像:1.首先在阿里云上选择容器镜像服务,创建命名空间以及镜像仓库2. docker login --username=zhangshitongsky@163.com registry.c ...

  9. 浅谈maven setting.xml 设置的mirrorof标签作用。

    https://blog.csdn.net/whbing1471/article/details/53983779 A 看这一段 背景:写好的java项目放置到linux服务器上进行编辑的时候,由于m ...

  10. Asp.Net Core Web Api 使用 Swagger 生成 api 说明文档

    最近使用 Asp.Net Core Web Api 开发项目服务端.Swagger 是最受欢迎的 REST APIs 文档生成工具之一,进入我的视野.以下为学习应用情况的整理. 一.Swagger 介 ...