springboot 容器启动事件
在springboot 容器启动时,我们需要在启动过程中做一些操作,比如启动容器后,执行某些代码。
spring 提供了监听器,我们可以方便的实现这些操作。
在容器启动开始时:
package com.neo.filter; import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener; public class ApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent arg0) {
System.err.println("ApplicationStartingEventListener");
} }
在容器启动完成后执行操作:
package com.neo.filter; import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered; public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent>,Ordered { @Override
public void onApplicationEvent(ApplicationStartedEvent ev) {
System.out.println("ApplicationStartedEventListener1");
}
@Override
public int getOrder() {
return 1;
} }
如果需要有顺序执行,我们可以实现Ordered接口,只越小,越先执行。
package com; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import com.neo.filter.ApplicationStartedEventListener;
import com.neo.filter.ApplicationStartedEventListener2;
import com.neo.filter.ApplicationStartingEventListener; @SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication app=new SpringApplication(DemoApplication.class);
app.addListeners(new ApplicationStartedEventListener());
app.addListeners(new ApplicationStartingEventListener());
app.addListeners(new ApplicationStartedEventListener2());
app.run(args);
}
}
springboot 容器启动事件的更多相关文章
- springboot容器启动顺序之@Configuration ContextRefreshedEvent事件初始化 ApplicationRunner
笔者最近遇到一个问题 我们根据自己业务需要 需要首次启动springboot项目时 把数据库数据同步至本地缓存(比如ehcache)但有一个要求 在缓存未载入成功 不允许有流量打入 一开始我们使用 ...
- spring boot容器启动详解
目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...
- springboot web - 启动(2) run()
接上一篇 在创建 SpringApplication 之后, 调用了 run() 方法. public ConfigurableApplicationContext run(String... arg ...
- 【玩转SpringBoot】通过事件机制参与SpringBoot应用的启动过程
生命周期和事件监听 一个应用的启动过程和关闭过程是归属到“生命周期”这个概念的范畴. 典型的设计是在启动和关闭过程中会触发一系列的“事件”,我们只要监听这些事件,就能参与到这个过程中来. 要想监听事件 ...
- springboot(八) 嵌入式Servlet容器自动配置原理和容器启动原理
1.嵌入式Servlet容器自动配置原理 1.1 在spring-boot-autoconfigure-1.5.9.RELEASE.jar => springboot自动配置依赖 jar包下,E ...
- springboot之启动原理解析
前言 SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起一步步揭开SpringBoot的神秘面 ...
- 深入理解SpringBoot之启动探究
SpringApplication是SpringBoot的启动程序,我们通过它的run方法可以快速启动一个SpringBoot应用.可是这里面到底发生了什么?它是处于什么样的机制简化我们程序启动的?接 ...
- SpringBoot一站式启动流程源码分析
一.前言 由上篇文章我们得知,SpringBoot启动时,就是有很简单的一行代码.那我们可以很清楚的看到这行代码的主角便是SpringApplication了,本文我们就来聊一聊这货,来探寻Sprin ...
- springboot之启动原理解析及源码阅读
前言 SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起一步步揭开SpringBoot的神秘面 ...
随机推荐
- Mybatis 表名是变量时
写语句时这样就可以了 <select id="selectTotal" resultType="java.util.Map" parameterType= ...
- TZOJ 1513 Farm Tour(最小费用最大流)
描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...
- f5 V11 TMSH命令行操作手册
1.命令行登录工具:“SshClient.exe” 2.查看当前系统配置: # show running-config # show running-config net interface:网络接口 ...
- day 12 内置函数,装饰器,递归函数
内置函数 内置函数:python给咱们提供了一些他认为你会经常用到的函数,68种 内置函数 abs() dict() help() min() setattr() all() di ...
- ios 后台发送邮件之SKPSMTPMessage的使用
skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git 1.由于skpsmtpmessage是非ARC ...
- 函数的动态参数与命名空间 day10
一.动态参数(不定长参数)def func(*args): 形参: 3种 动态参数 args元组,他包含了你所有的位置参数. *args 动态参数,不定长参数 **kwargs 他包含了关键字动态参数 ...
- 11.Mysql视图
11.视图11.1 什么是视图 视图view是一张虚拟表,它不存储数据,数据仍在表里,视图由一条查询表的select语句创建,视图只存储select语句. 可以将复杂的查询语句封装成视图,用户可以从视 ...
- hdu 5441 Travel (2015长春网赛)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题目大意是给一个n个城市(点)m条路线(边)的双向的路线图,每条路线有时间值(带权图),然后q个询问,每个 ...
- java中 this 关键字的三种用法
Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...
- django admin管理后台中文添加问题
django版本号 1.7.8 #create database mydb character set utf8;#django-admin.py startproject mysite#设置sett ...