SpringBoot项目启动执行任务的几种方式
1、直接在启动类下面调用方法
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
System.out.println("在启动类添加初始下方法");
}
}
2、使用@PostConstruct注解
@Component
public class MyInitStart2 {
@PostConstruct
public void run() {
System.out.println("在MyInitStart2中添加初始下方法");
}
}
3、实现CommandLineRunner接口
@Component
public class MyInitStart3 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("在MyInitStart3中添加初始下方法");
}
}
4、实现ApplicationRunner接口
@Component
public class MyInitStart4 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("在MyInitStart4中添加初始下方法");
}
}
5、实现ApplicationListener接口
@Component
public class MyInitStart5 implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("在MyInitStart5中添加初始下方法");
}
}
6、实现InitializingBean接口
@Component
public class MyInitStart6 implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("在MyInitStart6中添加初始下方法");
}
}
7、使用过滤器
@Component
public class MyFilter implements Filter {
/**
* init() :该方法在tomcat容器启动初始化过滤器时被调用,它在 Filter 的整个生命周期只会被调用一次。
* doFilter() :容器中的每一次请求都会调用该方法, FilterChain(放行) 用来调用下一个过滤器 Filter。
* destroy(): 当容器销毁过滤器实例时调用该方法,在方法中销毁或关闭资源,在过滤器 Filter 的整个生命周期也只会被调用一次。
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter 前置");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Filter 处理中");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
System.out.println("Filter 后置");
}
}
8、使用定时器方式
@Component
// 启用定时任务
@EnableScheduling
public class ScheduledTasks {
// 每 5 秒执行一次任务。
@Scheduled(cron = "0/5 * * * * ?")
public void performingTasks() {
System.out.println("在定时任务中添加初始化方法");
// 停止定时任务 代码实现省略...
}
}
以上几种方法的打印顺序:
Filter 前置(过滤器)
在MyInitStart6中添加初始下方法(实现InitializingBean接口)
在MyInitStart2中添加初始下方法(使用@PostConstruct注解)
在MyInitStart5中添加初始下方法(实现ApplicationListener接口)
在MyInitStart4中添加初始下方法(实现ApplicationRunner接口)
在MyInitStart3中添加初始下方法(实现CommandLineRunner接口)
在启动类添加初始下方法(启动类)
在定时任务中添加初始化方法(定时任务)
SpringBoot项目启动执行任务的几种方式的更多相关文章
- SSM项目启动的三种方式
SSM整合Maven项目的三种启动方式 项目部署如图: 1.从父工程的的tomcat插件中直接启动 2.从web子工程的tomcat插件中启动,(需要先执行父工程的install) 如果没有执行父工程 ...
- gradle springboot 项目运行的三种方式
一.java -jar 二.eclipse中 Java Application 三.命令行 gradle bootRun
- springboot项目启动成功后执行一段代码的两种方式
springboot项目启动成功后执行一段代码的两种方式 实现ApplicationRunner接口 package com.lnjecit.lifecycle; import org.springf ...
- SpringBoot项目启动后再请求远程接口的实现方式
场景 有一个SpringBoot项目需要在启动后请求另一个远程服务拿取配置,而不是加载过程中去请求,可能会出现类没有实例化的场景,因此需要实现项目完全启动后再进行请求的场景. 解决 一般会有两种实现方 ...
- Springboot 项目启动后执行某些自定义代码
Springboot 项目启动后执行某些自定义代码 Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRun ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...
- springboot项目启动之后初始化自定义配置类
前言 今天在写项目的时候,需要再springboot项目启动之后,加载我自定义的配置类的一些方法,百度了之后特此记录下. 正文 方法有两种: 1. 创建自定义类实现 CommandLineRunner ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- Springboot项目启动不了。也不打印任何日志信息。
Springboot项目启动不了.也不打印任何日志信息. <!-- 在创建Spring Boot工程时,我们引入了spring-boot-starter,其中包含了spring-boot-sta ...
随机推荐
- P3193 [HNOI2008] GT考试 题解
之前学矩阵乘的时候做的题,当时因为不会\(kmp\)搜索一稀里糊涂过去了,现在填个坑. 头图 是\(Logos\)! P3193 [HNOI2008] GT考试 题链:洛谷 题库 题目大意: 求有多少 ...
- springboot项目启动会报4个加载不到的debug提示,可改可不改
1. 因为启动的时候会报提示: Unable to locate LocaleResolver with name 'localeResolver': using default [org.sprin ...
- rsync备份服务器部署详情
rsync -avz --bwlimit=1024M /data/wanxhe rsync_backup@10.x.x.38::backup/gpu007/data/ --password-fil ...
- Nginx 调试模块 echo-nginx-module
引言 Nginx 作为一个高性能的 HTTP 和反向代理 Web 服务器.如今很多项目都会选择 Nginx 作为反向代理服务器,但是避免不了在使用的过程中,会遇到各种各样的问题.因此 echo-ngi ...
- vue.js的M-V-VM思想
MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式. Model 指代的就是vue对象的data属性里面的数据.这里的数据要显示到页面中. View 指代的就 ...
- .net C# System.Text.Json 如何将 string类型的“true”转换为布尔值 解决方案
直接上解决方法的代码 先定义一个转换顺,代码如下: public sealed class AnhBoolConverter : JsonConverter<bool?> { public ...
- MyBatis完成CRUD 详细细节内容
1. MyBatis完成CRUD 详细细节内容 @ 目录 1. MyBatis完成CRUD 详细细节内容 每博一文案 2. MyBatis工具类SqlSessionUtil的封装 3. 准备工作 3. ...
- 编译安装mysql5.7.20
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \ ...
- 通过USB口扩展wan口上网(4G上网卡)
通过USB口扩展wan口上网(4G上网卡) 一.前言 现爱快可支持通过USB口扩展wan口上网,不再居于地点的限制,随时随地流畅上网. 二.具体配置 现在有两种设备可以实现通过USB口转化为wan口上 ...
- vue3拉取代码install 报错 npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: npm ERR! Found: vue@3.2.31
先看报错 说明安装的包和现有的包已经冲突了版本不一致 我们先试一下npm install @vue/cli -- force然后再试一下npm install @vue/cli --legacy-pe ...