Springboot 项目启动后执行某些自定义代码
Springboot 项目启动后执行某些自定义代码
Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner。
这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方法。我们可以通过实现ApplicationRunner和CommandLineRunner,来实现,他们都是在SpringApplication 执行之后开始执行的。
CommandLineRunner接口可以用来接收字符串数组的命令行参数,ApplicationRunner 是使用ApplicationArguments 用来接收参数的
代码示例
@Component//被spring容器管理
@Order(1)//如果多个自定义ApplicationRunner,用来标明执行顺序
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
System.out.println("-------------->" + "项目启动,now=" + new Date());
myTimer();
}
public static void myTimer(){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("------定时任务--------");
}
}, 0, 1000);
}
}
执行结果
2018-02-08 14:10:16.490 INFO 10236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
-------------->项目启动,now=Thu Feb 08 14:10:16 CST 2018
------定时任务--------
2018-02-08 14:10:16.497 INFO 10236 --- [ main] com.mlxs.springboot01.web.MainApp : Started MainApp in 5.595 seconds (JVM running for 6.334)
------定时任务--------
------定时任务--------
------定时任务--------
------定时任务--------
------定时任务--------
------定时任务--------
Springboot 项目启动后执行某些自定义代码的更多相关文章
- springboot2.X 在项目启动后执行一段自定义代码
场景: 项目需要在项目启动后从数据库初始化一些数据进入redis , 但是没有很适合 的监听器去实现 , 监听 老是在dao初始化之前触发. 解决方法:自定义类实现 ApplicationRunner ...
- Springboot项目启动后自动创建多表关联的数据库与表的方案
文/朱季谦 在一些项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表. 若要解决这类需求,其实现在已有不少开源框架都能实现自动生成数据 ...
- springboot工程启动即执行一段代码
最近在做一个项目, 需要Tomcat启动后就执行一段代码 在这里需要用到CommandLineRunner这个接口, Spring boot的CommandLineRunner接口主要用于实现在应用初 ...
- Springboot项目启动后访问不到Controller
1.搭建一个简单的Springboot项目,最开始将启动类main函数与controller放到一个类里,可以正常启动和访问,但是将两个分开再启动时访问就会报错:This application ha ...
- SpringBoot#应用启动后执行某些逻辑
// 方式1 @Component public class WhenStartupA implements InitializingBean { @Override public void afte ...
- springboot启动后执行一段代码的方式
文章转载自: https://www.cnblogs.com/zuidongfeng/p/9926471.html https://blog.csdn.net/zknxx/article/detail ...
- SpringBoot项目启动时执行初始化操作
SpringBooot中的CommandLineRunner接口会在所有Spring Beans初始化之后,SpringApplication.run()之前执行. 1.添加pom引用 <?xm ...
- springboot项目启动后tomcat服务器自动关闭 解决方法
需要在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
- SpringBoot项目启动后自动打开浏览器
编写一个类,注册为Spring的Bean,然后实现CommandLineRunner接口,重写run()方法即可 @Component public class OpenBrowser impleme ...
随机推荐
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
JPA Audit 在spring jpa中,支持在字段或者方法上进行注解@CreatedDate.@CreatedBy.@LastModifiedDate.@LastModifiedBy,从字面意思 ...
- 用jenkins创建节点
原料:(1)jre下载链接:https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html ( ...
- python学习_1
1.python2和python3 从宏观上讲,python2源码不标准.混乱.重复,和龟叔的理念背道而驰. 在python3上,实现了源码的统一化和标准化,去除了重复的代码. 2.编译型语言和解释型 ...
- scrapy formRequest 表单提交
scrapy.FormRequest 主要用于提交表单数据 先来看一下源码 参数: formdata (dict or iterable of tuples) – is a dictionary ( ...
- 江苏省选2019Round2游记
JSOI2019R2过去了. 翻盘变翻车... Day 0 打板子.写了几棵主席树. 然后啃CF 331D3.啃不动 Day 1 开T1.这什么玩意啊...换换换 开T2.一眼\(10\)分的状压,码 ...
- Eclipse 从git导入maven多模块项目
原文地址:https://blog.csdn.net/xiongyouqiang/article/details/78903975 以https://github.com/xiongyouqiang/ ...
- 类ArrayList
什么是ArrayList类 Java提供了一个容器 java.util.ArrayList 集合类,他是大小可变的数组的实现,存储在内的数据称为元素.此类提供一些方法来操作内部存储的元素. Array ...
- protobuf使用详解
https://blog.csdn.net/skh2015java/article/details/78404235 原文地址:http://blog.csdn.net/lyjshen/article ...
- CSS3动画效果transition
1.transition的浏览器支持情况 IE10+支持,IE6\7\8\9都不支持!目前,其他浏览器最新版本都支持,不需要再加前缀 -webkit- 之类的了 2. 还是一步一步说说怎么用trans ...
- [转帖]Windows Server 2016各种版本介绍
Windows Server 2016各种版本介绍 http://www.5sharing.com/js/zx/872.html windows server的版本 时间:2018-10-06 10: ...