Spring Boot多种方式在应用启动时执行自定义代码@PostConstruct

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提供了灵活的机制来管理应用启动时的自定义逻辑,使得开发者能够更方便地控制应用的初始化过程。在实际项目中,通常根据具体场景选择其中一种或多种方式,以满足不同的需求

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值