SpringBoot实现启动项目后立即执行的方法总结
作者:JK凯 时间:2022-07-31 15:01:16
在项目开发中某些场景必须要用到启动项目后立即执行方式的功能,如我们需要去初始化数据到redis缓存,或者启动后读取相应的字典配置等,这篇文章主要聊聊实现立即执行的几种方法。
一、CommandLineRunner和ApplicationRunner
这两者的实现方法一样,都是去继承相应的接口然后重写run方法即可,也都是SpringBoot框架所提供给我们的接口,也是项目中最常用的,比较灵活,有多个
CommandLineRunner或ApplicationRunner实现类时可以通过@Order注解或实现Ordered接口重写getOrder方法实现按指定顺序执行
1. CommandLineRunner的实现
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunnerImpl方法执行");
}
}
2. ApplicationRunner的实现
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner方法执行");
}
3. 多个CommandLineRunner或ApplicationRunner实现类时指定顺序执行
通过@Order注解指定,数字越小越先执行
@Component
@Order(1) //通过order注解指定
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner方法执行");
}
}
通过实现Ordered接口并重写getOrder方法实现,数字越小越先执行
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
@Component
public class ApplicationRunnerImpl implements ApplicationRunner, Ordered {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner方法执行");
}
@Override
public int getOrder() {
return 1;
}
}
4.CommandLineRunner或ApplicationRunner的不同
这两者的不同其实就是run方法中所接收的参数类型不同,CommandLineRunner是对我们启动jar包时所传参数不进行处理,原样返回String类型给你,ApplicationRunner是封装成了ApplicationArguments参数,通过这个类型可以更方便的判断某些参数是否存在之类的。
二、JDK所提供的@PostConstruct
@PostConstruct是JDK所提供的注解,使用该注解的方法会在服务器加载Servlet的时候运行。也可以在一个类中写多个方法并添加这个注解。
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class PostConstructTest {
@PostConstruct
public void start() {
System.out.println("@PostConstruct方法执行");
}
@PostConstruct
public void start01() {
System.out.println("@PostConstruct1111方法执行");
}
}
三、其他方法(不常用)
1. ApplicationContextAware
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextAwareImpl implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAwareImpl方法执行");
}
}
2. ApplicationListener(会执行多次)
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class ApplicationListenerImpl implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("ApplicationListenerImpl方法执行");
}
}
3. InitializingBean
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class InitializingBeanImpl implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBeanImpl方法执行");
}
}
这些方法也都可以实现在启动项目后立即执行,但是不太常用。
来源:https://juejin.cn/post/7237020208647569469
标签:SpringBoot,启动,执行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java导出csv格式文件的方法
2022-07-24 20:26:36
![](https://img.aspxhome.com/file/2023/5/62665_0s.jpg)
Java多线程Thread类的使用详解
2023-11-11 13:08:11
![](https://img.aspxhome.com/file/2023/3/59013_0s.png)
Java数组的定义、初始化、及二维数组用法分析
2022-06-11 16:59:04
新手入门Jvm--jvm概览
2023-11-09 15:42:31
![](https://img.aspxhome.com/file/2023/8/71878_0s.png)
spring中向一个单例bean中注入非单例bean的方法详解
2022-07-19 13:14:18
![](https://img.aspxhome.com/file/2023/7/62167_0s.png)
Mybatis-plus实现主键自增和自动注入时间的示例代码
2022-11-05 00:22:51
谈谈为JAXB和response设置编码,解决wechat4j中文乱码的问题
2023-07-31 01:34:27
![](https://img.aspxhome.com/file/2023/1/57621_0s.png)
java使用IO流对数组排序实例讲解
2023-09-04 02:24:19
java HashMap和HashTable的区别详解
2023-08-10 18:41:56
Servlet注解之@WebInitParam多个InitParam的使用
2023-08-04 13:43:01
Unity Shader实现描边OutLine效果
2022-01-13 03:11:13
![](https://img.aspxhome.com/file/2023/0/76380_0s.gif)
Java实现SSL双向认证的方法
2023-09-22 10:34:35
![](https://img.aspxhome.com/file/2023/0/58090_0s.jpg)
Java编程复用类代码详解
2021-09-13 10:06:34
Java、JavaScript、Oracle、MySQL中实现的MD5加密算法分享
2022-08-24 02:42:11
浅谈spring中scope作用域
2022-08-18 10:42:53
关于Mybatis-Plus Wrapper是否应该出现在Servcie类中
2023-11-28 22:04:56
Java获得当前时间前指定几个小时具体时间的方法示例
2023-11-24 04:56:49
![](https://img.aspxhome.com/file/2023/7/59727_0s.jpg)
Springboot+AOP实现返回数据提示语国际化的示例代码
2021-08-18 19:49:12
![](https://img.aspxhome.com/file/2023/5/73125_0s.png)
SpringBoot构建RESTful API的实现示例
2022-04-13 14:45:08
解决使用json-lib包实现xml转json时空值被转为空中括号的问题
2022-10-20 02:12:14