Spring Boot启动时执行初始化操作

本文介绍了SpringBoot中实现启动时初始化操作的多种方法,包括使用@PostConstruct注解、实现CommandLineRunner或ApplicationRunner接口,并解释了它们的执行时机及顺序控制。
通常,有些操作需要在工程启动时执行,例如某些资源的加载。SpringBoot提供了几种方式来实现该功能:

@PostConstruct
对于注入到Spring容器中的类,在其成员函数前添加@PostConstruct注解,则在执行Spring beans初始化时,就会执行该函数。
但由于该函数执行时,其他Spring beans可能并未初始化完成,因此在该函数中执行的初始化操作应当不依赖于其他Spring beans。

@Component
public class Construct {
    @PostConstruct
    public void doConstruct() throws Exception {
        System.out.println("初始化:PostConstruct");
    }
}

CommandLineRunner
CommandLineRunner是Spring提供的接口,定义了一个run()方法,用于执行初始化操作。

@Component
public class InitCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("初始化:InitCommandLineRunner");
    }
}

CommandLineRunner的时机为Spring beans初始化之后,因此CommandLineRunner的执行一定是晚于@PostConstruct的。
若有多组初始化操作,则每一组操作都要定义一个CommandLineRunner派生类并实现run()方法。这些操作的执行顺序使用@Order(n)来设置,n为int型数据。

@Component
@Order(99)
public class CommandLineRunnerA implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("初始化:CommandLineRunnerA");
    }
}

@Component
@Order(1)
public class CommandLineRunnerB implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("初始化:CommandLineRunnerB");
    }
}

如上,会先执行CommandLineRunnerB的run(),再执行CommandLineRunnerA的run()。
@Order(n)中的n较小的会先执行,较大的后执行。n只要是int值即可,无需顺序递增。

ApplicationRunner
ApplicationRunner接口与CommandLineRunner接口类似,都需要实现run()方法。二者的区别在于run()方法的参数不同:

@Component
public class InitApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("初始化:InitApplicationRunner");
    }
}

ApplicationRunner接口的run()参数为ApplicationArguments对象,因此可以获取更多项目相关的内容。
ApplicationRunner接口与CommandLineRunner接口的调用时机也是相同的,都是Spring beans初始化之后。因此ApplicationRunner接口也使用@Order(n)来设置执行顺序

 

Spring Boot中实现应用启动时初始化操作有多种方法,以下是几种常见的方式: 1. **使用`CommandLineRunner`或`ApplicationRunner`接口**: 这两个接口都提供了在Spring Boot应用启动后执行代码的机制。`CommandLineRunner`的`run`方法接收一个`String`数组作为参数,而`ApplicationRunner`的`run`方法接收一个`ApplicationArguments`对象。 ```java import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // 初始化操作 System.out.println("CommandLineRunner: 初始化操作"); } } ``` ```java import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationArguments; import org.springframework.stereotype.Component; @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // 初始化操作 System.out.println("ApplicationRunner: 初始化操作"); } } ``` 2. **使用`@PostConstruct`注解**: `@PostConstruct`注解用于在依赖注入完成后执行初始化方法。这个方法会在`CommandLineRunner`和`ApplicationRunner`之前执行。 ```java import javax.annotation.PostConstruct; import org.springframework.stereotype.Component; @Component public class MyPostConstructBean { @PostConstruct public void init() { // 初始化操作 System.out.println("@PostConstruct: 初始化操作"); } } ``` 3. **实现`InitializingBean`接口**: `InitializingBean`接口提供了一个`afterPropertiesSet`方法,该方法在所有属性设置完成后被调用。 ```java import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; @Component public class MyInitializingBean implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { // 初始化操作 System.out.println("InitializingBean: 初始化操作"); } } ``` 4. **使用`ApplicationListener`**: 通过实现`ApplicationListener`接口,可以监听特定的应用程序事件,例如`ContextRefreshedEvent`,在事件触发时执行初始化操作。 ```java import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; @Component public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 初始化操作 System.out.println("ApplicationListener: 初始化操作"); } } ``` 这些方法各有优缺点,选择哪种方式取决于具体的应用场景和需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值