Springboot启动后执行方法小结
作者:鹤冲天Pro 时间:2022-09-26 22:12:02
一、注解@PostConstruct
使用注解@PostConstruct是最常见的一种方式,存在的问题是如果执行的方法耗时过长,会导致项目在方法执行期间无法提供服务。
@Component
public class StartInit {
//
// @Autowired 可以注入bean
// ISysUserService userService;
@PostConstruct
public void init() throws InterruptedException {
Thread.sleep(10*1000);//这里如果方法执行过长会导致项目一直无法提供服务
System.out.println(123456);
}
}
二、CommandLineRunner接口
实现CommandLineRunner接口 然后在run方法里面调用需要调用的方法即可,好处是方法执行时,项目已经初始化完毕,是可以正常提供服务的。
同时该方法也可以接受参数,可以根据项目启动时: java -jar demo.jar arg1 arg2 arg3 传入的参数进行一些处理。
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(Arrays.toString(args));
}
}
三、实现ApplicationRunner接口
实现ApplicationRunner接口和实现CommandLineRunner接口基本是一样的。
唯一的不同是启动时传参的格式,CommandLineRunner对于参数格式没有任何限制,ApplicationRunner接口参数格式必须是:–key=value
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
Set<String> optionNames = args.getOptionNames();
for (String optionName : optionNames) {
List<String> values = args.getOptionValues(optionName);
System.out.println(values.toString());
}
}
}
四、实现ApplicationListener
实现接口ApplicationListener方式和实现ApplicationRunner,CommandLineRunner接口都不影响服务,都可以正常提供服务,注意监听的事件,通常是ApplicationStartedEvent 或者ApplicationReadyEvent,其他的事件可能无法注入bean。
@Component
public class ApplicationListenerImpl implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("listener");
}
}
五、四种方式的执行顺序
注解方式@PostConstruct 始终最先执行
如果监听的是ApplicationStartedEvent 事件,则一定会在CommandLineRunner和ApplicationRunner 之前执行。
如果监听的是ApplicationReadyEvent 事件,则一定会在CommandLineRunner和ApplicationRunner 之后执行。
CommandLineRunner和ApplicationRunner 默认是ApplicationRunner先执行,如果双方指定了@Order 则按照@Order的大小顺序执行,大的先执行。
来源:https://blog.csdn.net/weixin_44816664/article/details/130045838
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java客户端调用.NET的WebService实例
![](https://img.aspxhome.com/file/2023/8/58988_0s.png)
Java 在PDF中添加骑缝章示例解析
![](https://img.aspxhome.com/file/2023/5/59985_0s.png)
Java实现NIO聊天室的示例代码(群聊+私聊)
![](https://img.aspxhome.com/file/2023/8/59698_0s.jpg)
SpringBoot全局配置long转String丢失精度的问题解决
Android Studio 多层级 Module 对 aar 引用问题解决方法
![](https://img.aspxhome.com/file/2023/2/86762_0s.png)
java处理图片背景颜色的方法
Java裁剪压缩PNG图片,透明背景色变黑的解决方案
IDEA部署JavaWeb项目到Tomcat服务器的方法
jsp+servlet实现简单登录页面功能(附demo)
![](https://img.aspxhome.com/file/2023/5/58465_0s.png)
浅谈springboot之JoinPoint的getSignature方法
基于Java语言MD5加密Base64转换方法
解决idea每次新建项目都需要重新指定maven目录
![](https://img.aspxhome.com/file/2023/3/61273_0s.png)
springboot项目配置多个kafka的示例代码
Kotlin中的惰性操作容器Sequence序列使用原理详解
![](https://img.aspxhome.com/file/2023/7/84717_0s.jpg)
一文带你了解C#中抽象方法与虚方法的区别
分析JVM源码之Thread.interrupt系统级别线程打断
eclipse maven 插件的安装和配置详解
![](https://img.aspxhome.com/file/2023/8/58298_0s.jpg)
Hibernate实现批量添加数据的方法
![](https://img.aspxhome.com/file/2023/5/60815_0s.jpg)
Java详细讲解堆排序与时间复杂度的概念
![](https://img.aspxhome.com/file/2023/7/58597_0s.png)
在IDEA里gradle配置和使用的方法步骤
![](https://img.aspxhome.com/file/2023/1/59851_0s.png)