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 项目启动后执行某些自定义代码的更多相关文章

  1. springboot2.X 在项目启动后执行一段自定义代码

    场景: 项目需要在项目启动后从数据库初始化一些数据进入redis , 但是没有很适合 的监听器去实现 , 监听 老是在dao初始化之前触发. 解决方法:自定义类实现 ApplicationRunner ...

  2. Springboot项目启动后自动创建多表关联的数据库与表的方案

    文/朱季谦 在一些项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表. 若要解决这类需求,其实现在已有不少开源框架都能实现自动生成数据 ...

  3. springboot工程启动即执行一段代码

    最近在做一个项目, 需要Tomcat启动后就执行一段代码 在这里需要用到CommandLineRunner这个接口, Spring boot的CommandLineRunner接口主要用于实现在应用初 ...

  4. Springboot项目启动后访问不到Controller

    1.搭建一个简单的Springboot项目,最开始将启动类main函数与controller放到一个类里,可以正常启动和访问,但是将两个分开再启动时访问就会报错:This application ha ...

  5. SpringBoot#应用启动后执行某些逻辑

    // 方式1 @Component public class WhenStartupA implements InitializingBean { @Override public void afte ...

  6. springboot启动后执行一段代码的方式

    文章转载自: https://www.cnblogs.com/zuidongfeng/p/9926471.html https://blog.csdn.net/zknxx/article/detail ...

  7. SpringBoot项目启动时执行初始化操作

    SpringBooot中的CommandLineRunner接口会在所有Spring Beans初始化之后,SpringApplication.run()之前执行. 1.添加pom引用 <?xm ...

  8. springboot项目启动后tomcat服务器自动关闭 解决方法

    需要在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  9. SpringBoot项目启动后自动打开浏览器

    编写一个类,注册为Spring的Bean,然后实现CommandLineRunner接口,重写run()方法即可 @Component public class OpenBrowser impleme ...

随机推荐

  1. Swift构造

    构造就是将结构体.类或枚举的实例准备好以便使用的过程.这个过程包括: (1)为实例中的每个存储属性设置初始值. (2)执行必要的准备和初始化工作. 实例的构造过程是通过构造器来完成的. 可以在结构体. ...

  2. Vue.js01:跑马灯效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. win7 wifi热点设置

    1.创建wifi热点 netsh wlan set hostednetwork mode=allow ssid=pengyanPC key=11111111 2.启动wifi热点 netsh wlan ...

  4. centos6.5搭建hadoop完整教程

    https://blog.csdn.net/hanzl1/article/details/79040380 博客地址http://blog.csdn.net/pucao_cug/article/det ...

  5. ARTS打卡第三周

    Algorithm 题目描述 Given an array of integers, find if the array contains any duplicates. Your function ...

  6. 基于H5的摄像头视频数据流采集

    最近,为了支持部门团队的项目,通过H5实现摄像头的视频流数据的捕获,抓取到视频流后,传输到视频识别服务器进行后续的逻辑处理. 视频数据的采集过程,其实是比较没有谱的过程,因为之前没有研究过HTML5操 ...

  7. React 系列教程2:编写兰顿蚂蚁演示程序

    简介 最早接触兰顿蚂蚁是在做参数化的时候,那时候只感觉好奇,以为是很复杂的东西.因无意中看到生命游戏的 React 实现,所以希望通过兰顿蚂蚁的例子再学习一下 React. 兰顿蚂蚁的规则非常简单: ...

  8. Git使用注意事项

    第一次用git时push时,突然想到我没有设置ssh key,却也可以push代码到自己仓库,那我本地登陆的账号Git是存在哪儿了呢? Git本地账户凭证管理 在第一次push到远程仓库时,git会提 ...

  9. postman Installation has failed: There was an error while installing the application. Check the setup log for more information and contact the author

    Error msg: Installation has failed: There was an error while installing the application. Check the s ...

  10. OpenStack 命令行速查表

    OpenStack 命令行速查表   updated: 2017-07-18 08:53 Contents 认证 (keystone) 镜像(glance) 计算 (nova) 实例的暂停.挂起.停止 ...