Spring Boot应用启动时自动执行代码的五种方式(常见方法)

 更新时间:2024年04月10日 12:05:21   作者:不掉头发的阿水  
Spring Boot为开发者提供了多种方式在应用启动时执行自定义代码,这些方式包括注解、接口实现和事件监听器,本文我们将探讨一些常见的方法,以及如何利用它们在应用启动时执行初始化逻辑,感兴趣的朋友一起看看吧

Spring Boot为开发者提供了多种方式在应用启动时执行自定义代码,这些方式包括注解、接口实现和事件监听器。在本篇博客中,我们将探讨一些常见的方法,以及如何利用它们在应用启动时执行初始化逻辑。

1.  @PostConstruct注解

`@PostConstruct`注解可以标注在方法上,该方法将在类被初始化后调用。在Spring Boot应用中,你可以使用这个注解来执行一些初始化的逻辑。

@PostConstruct
public void doSomething(){
    // 在应用启动后执行的代码
    System.out.println("do something");
}

2.  ApplicationListener接口

实现`ApplicationListener`接口并监听`ApplicationStartedEvent`事件,这样你的逻辑将在应用启动后被触发。

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
public class MyApplicationListener implements ApplicationListener<ApplicationStartedEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        // 在应用启动后执行的代码
        System.out.println("ApplicationListener executed");
    }
}

 3.  @EventListener注解

使用`@EventListener`注解,可以将方法标记为事件监听器,并在特定事件发生时执行。

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
public class MyEventListener {
    @EventListener(ApplicationStartedEvent.class)
    public void onApplicationEvent() {
        // 在应用启动后执行的代码
        System.out.println("@EventListener executed");
    }
}

4.  ApplicationRunner接口

实现`ApplicationRunner`接口,该接口的`run`方法会在Spring Boot应用启动后执行。

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 在应用启动后执行的代码
        System.out.println("ApplicationRunner executed");
    }
}

5.  CommandLineRunner接口

与`ApplicationRunner`类似,`CommandLineRunner`接口的`run`方法也在应用启动后执行。

public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        // 在应用启动后执行的代码
        System.out.println("CommandLineRunner executed");
    }
}

Demo代码

 完整如下

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class Application implements
        ApplicationListener<ApplicationStartedEvent>,
        CommandLineRunner,
        ApplicationRunner
{
    /**
     * 本次执行先后顺序为(没有设置order)
     * PostConstruct、ApplicationListener、@EventListener注解、ApplicationRunner、CommandLineRunner
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @PostConstruct
    public void doSomething(){
        // 在应用启动后执行的代码
        System.out.println("do something 11111111111");
        System.out.println("PostConstruct注解启动");
        System.out.println("===============");
    }
    @EventListener(ApplicationStartedEvent.class)
    public void onApplicationEvent() {
        // 在应用启动后执行的代码
        System.out.println("do something 22222222222");
        System.out.println("@EventListener 注解启动 executed");
        System.out.println("===============");
    }
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        // 在应用启动后执行的代码
        System.out.println("do something 3333333333");
        System.out.println("ApplicationListener executed");
        System.out.println("===============");
    }
    @Override
    public void run(String... args) throws Exception {
        // 在应用启动后执行的代码
        System.out.println("do something 44444444");
        System.out.println("CommandLineRunner启动");
        System.out.println("===============");
    }
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 在应用启动后执行的代码
        System.out.println("do something 55555555");
        System.out.println("ApplicationRunner启动");
        System.out.println("===============");
    }
}

Demo分析

@PostConstruct注解方法 (doSomething方法) 在类初始化后被调用,因此会首先输出。

ApplicationListener接口方法 (onApplicationEvent方法) 在应用启动后执行,会输出其相关的信息。

@EventListener注解方法 (onApplicationEvent方法) 同样在应用启动后执行,会输出其相关的信息。

ApplicationRunner接口方法 (run方法) 在ApplicationListener之后执行,它用于在Spring Boot应用启动后执行一些额外的逻辑。

CommandLineRunner接口方法 (run方法) 也在ApplicationListener之后执行,用于在Spring Boot应用启动后执行一些额外的逻辑。

总结

通过以上几种方式,你可以根据项目的需求选择合适的初始化方法。无论是使用注解、接口实现,还是事件监听器,Spring Boot提供了灵活的机制来管理应用启动时的自定义逻辑,使得开发者能够更方便地控制应用的初始化过程。在实际项目中,通常根据具体场景选择其中一种或多种方式,以满足不同的需求。

到此这篇关于Spring Boot应用启动时自动执行代码的五种方式(常见方法)的文章就介绍到这了,更多相关Spring Boot启动时自动执行代码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring事务失效问题分析及解决方案

    Spring事务失效问题分析及解决方案

    这篇文章主要介绍了Spring事务失效问题分析及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • RocketMQ消息存储文件的加载与恢复机制源码分析

    RocketMQ消息存储文件的加载与恢复机制源码分析

    这篇文章主要介绍了RocketMQ源码分析之消息存储文件的加载与恢复机制详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • RocketMQTemplate 注入失败的解决

    RocketMQTemplate 注入失败的解决

    这篇文章主要介绍了RocketMQTemplate 注入失败的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Java 实战项目锤炼之网上商城系统的实现流程

    Java 实战项目锤炼之网上商城系统的实现流程

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用Java+jsp+servlet+mysql+ajax实现一个网上商城系统,大家可以在过程中查缺补漏,提升水平
    2021-11-11
  • 一文带你彻底剖析Java中Synchronized原理

    一文带你彻底剖析Java中Synchronized原理

    Synchronized是Java中的隐式锁,它的获取锁和释放锁都是隐式的,完全交由JVM帮助我们操作,在了解Synchronized关键字之前,首先要学习的知识点就是Java的对象结构,本文介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • Java源码难点突破Lambda表达式执行原理

    Java源码难点突破Lambda表达式执行原理

    这篇文章主要为大家介绍了Java难点突破Lambda表达式执行原理分析及示例的实现源码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03
  • servlet之session简介_动力节点Java学院整理

    servlet之session简介_动力节点Java学院整理

    这篇文章主要介绍了servlet之session简介,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • Spring MVC整合Shiro权限控制的方法

    Spring MVC整合Shiro权限控制的方法

    这篇文章主要介绍了Spring MVC整合Shiro权限控制,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • FactoryBean BeanFactory方法使用示例详解讲解

    FactoryBean BeanFactory方法使用示例详解讲解

    这篇文章主要为大家介绍了FactoryBean BeanFactory方法使用示例详解讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • java自带排序使用

    java自带排序使用

    这篇文章主要给大家分享了java自带排序使用,该方法是升序排序,方法的内部采用了快排实现,但该方法是 稳定的。下面一起来看看文章的详细介绍吧
    2021-12-12

最新评论