SpringBoot项目启动后执行某个方法的方式(开机启动)

摘要:
spring boot项目启动的自动触发方法场景:这样做的原因是前端数据总是在变化,这导致我在每次打包后清除缓存并处理缓存数据。因此,本文旨在在项目启动后自动执行指定的方法。本文的目的是同步缓存的数据。

springboot项目启动自动触发方法场景:

做这个的原因:前端数据一直在变化,导致我每次打包之后需要清缓存处理缓存数据,故而有了本文,在项目启动之后自动执行指定方法,本文作用是实现同步缓存数据。
开始配置,有两种方式:ApplicationRunner和CommandLineRunner

实现ApplicationRunner接口

import org.service.PushMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

/**
 * 实现ApplicationRunner接口,执行顺序按照value值决定,值小先执行
 */
@Slf4j
@Component
@Order(value = 1)
public class MyApplicationRunner implements ApplicationRunner {
	@Autowired
	private PushMessageService pushMessageService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		log.info("测试服务MyApplicationRunner");
		pushMessageService.resetRedis();
	}
}

实现CommandLineRunner接口

import org.service.PushMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

/**
 * 实现MyCommandLineRunner接口,执行顺序按照value值决定,值小先执行
 */
@Slf4j
@Component
@Order(value = 2)
public class MyCommandLineRunner implements CommandLineRunner {
	@Autowired
	private PushMessageService pushMessageService;

	@Override
	public void run(String... args) throws Exception {
		log.info("执行MyCommandLineRunner");
		// 同步缓存中的通知消息数目
		pushMessageService.resetRedis();
	}
}

@Component注解相应的类,并重写run方法,若存在多个启动执行的任务,可利用在类上使用@Order注解来指定顺序。

在上述两个类中PushMessageService 为自己的service接口,具体是什么随意,使用方式和controller使用一样,这样项目启动之后日志打印顺序为:

测试服务MyApplicationRunner
执行MyCommandLineRunner

免责声明:文章转载自《SpringBoot项目启动后执行某个方法的方式(开机启动)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇android根据图片路径显示图片SqlServer--视图下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

帆软—FineBI5.1忘记管理员登录密码及用户名

1、 查找数据库。脚本文件查找数据库。脚本文件。...

PartⅠ邮件伪造

什么是伪造发件人邮件地址简单邮件传输协议 (Simple Mail Transfer Protocol, SMTP) 即简单邮件传输协议,是在Internet传输email的事实标准。正如名字所暗示的那样,它其实是一个非常简单的传输协议,无需身份认证,而且发件人的邮箱地址是可以由发信方任意声明的,利用这个特性可以伪造任意发件人。如何识别虚假(欺骗性)电子邮件...

Android Exception 10(server)' ~ Channel is unrecoverably broken and will be disposed!)

08-1119:22:35.028:W/MemoryDealer(2123):madvise(0x43e1600012288,MADV_REMOVE)返回操作不支持传输端点08-1119:22-35.038:W/InputDispatcher(2714):channel'4236b890com.tongyan.activi...

试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)

解决方法:iis应用程序池--˃高级设置--˃启用32位应用程序˂!body{font-family:"Verdana";font-weight:normal;font-size:.7em;color:black;}p{font-family:"Verdana";font-weight:normal;color:black;margin-top:-5px}b...

CentOS7 初始化配置

允许新TCP连接net.ipv4.TCP _ tw_ reuse=1net.ipv4.TCP _ mem=945000009150000009270000000net.ipv4 TCP _ fin_ Timeout=1#启用keepalive时,TCP发送keepalive消息的频率。默认值为2小时net.ipv4.tcp _keepalive_Time=3...

CorelDRAW 编写和运行宏指令

在开发和运行CorelDRAW宏之前,必须安装VBA组件。在CoerlDRAW11和12中安装CorelDRAWVBAVBA是典型安装的一部分。2如果安装开始,请单击安装CorelDRAW12 Graphics Suite。CorelDRAWVBA工具栏CorelDRaw工具栏提供了几个快速的VBA函数和对VB编辑器的访问。但是,您可以通过在CorelDRA...