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

标签:Springboot,启动,执行
0
投稿

猜你喜欢

  • Java客户端调用.NET的WebService实例

    2023-11-03 17:22:00
  • Java 在PDF中添加骑缝章示例解析

    2023-11-24 22:41:35
  • Java实现NIO聊天室的示例代码(群聊+私聊)

    2023-11-29 00:57:45
  • SpringBoot全局配置long转String丢失精度的问题解决

    2023-02-19 22:58:49
  • Android Studio 多层级 Module 对 aar 引用问题解决方法

    2023-08-06 19:41:27
  • java处理图片背景颜色的方法

    2023-11-27 04:38:20
  • Java裁剪压缩PNG图片,透明背景色变黑的解决方案

    2023-11-25 13:21:27
  • IDEA部署JavaWeb项目到Tomcat服务器的方法

    2023-11-02 23:21:22
  • jsp+servlet实现简单登录页面功能(附demo)

    2023-09-24 11:32:28
  • 浅谈springboot之JoinPoint的getSignature方法

    2022-12-25 11:23:20
  • 基于Java语言MD5加密Base64转换方法

    2023-11-24 00:22:41
  • 解决idea每次新建项目都需要重新指定maven目录

    2022-12-25 17:39:52
  • springboot项目配置多个kafka的示例代码

    2023-11-23 23:15:29
  • Kotlin中的惰性操作容器Sequence序列使用原理详解

    2023-10-01 14:21:55
  • 一文带你了解C#中抽象方法与虚方法的区别

    2023-07-23 00:14:32
  • 分析JVM源码之Thread.interrupt系统级别线程打断

    2023-07-31 17:15:23
  • eclipse maven 插件的安装和配置详解

    2023-08-24 16:57:01
  • Hibernate实现批量添加数据的方法

    2023-11-29 08:53:56
  • Java详细讲解堆排序与时间复杂度的概念

    2023-10-20 02:00:11
  • 在IDEA里gradle配置和使用的方法步骤

    2023-11-23 16:07:58
  • asp之家 软件编程 m.aspxhome.com