Spring概念:启动初始化

在 Spring 框架里,启动初始化的方式有多种,不同方式适用于不同场景,下面为你详细介绍:

1. @PostConstruct 注解

解释

@PostConstruct 注解是 JSR-250 规范里的注解,在 Bean 的依赖注入完成之后,会立即执行被该注解标注的方法。此方法只会执行一次。

示例代码

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    @PostConstruct
    public void init() {
        System.out.println("MyBean 初始化完成");
    }
}

适用场景

  • 资源初始化:当 Bean 需要在创建后立即初始化一些资源时,比如初始化数据库连接池、加载配置文件等。
  • 数据预热:在应用启动时对一些常用数据进行缓存,以提高后续访问性能。

2. InitializingBean 接口

解释

InitializingBean 接口定义了 afterPropertiesSet() 方法,Spring 容器在 Bean 的属性设置完成后,会调用该方法进行初始化操作。

示例代码

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class MyBean implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("MyBean 初始化完成");
    }
}

适用场景

  • 自定义初始化逻辑:当需要在 Bean 的属性设置完成后执行特定的初始化逻辑时,可以实现该接口。
  • 验证属性:在初始化时对 Bean 的属性进行验证,确保属性值符合要求。

3. @Bean 注解的 initMethod 属性

解释

在使用 @Bean 注解定义 Bean 时,可以通过 initMethod 属性指定一个初始化方法,Spring 容器会在 Bean 创建后调用该方法。

示例代码

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean(initMethod = "init")
    public MyBean myBean() {
        return new MyBean();
    }
}

class MyBean {
    public void init() {
        System.out.println("MyBean 初始化完成");
    }
}

适用场景

  • 第三方库 Bean 初始化:当使用第三方库的类作为 Bean 时,无法修改其代码实现 InitializingBean 接口,可通过 initMethod 属性指定初始化方法。
  • 多个初始化方法:当一个 Bean 有多个初始化步骤,需要分别在不同方法中实现时,可以使用该方式指定多个初始化方法。

4. CommandLineRunnerApplicationRunner 接口

解释

这两个接口都可以在 Spring 应用启动完成后执行一些操作。CommandLineRunner 接口的 run 方法接收字符串数组类型的命令行参数,而 ApplicationRunner 接口的 run 方法接收 ApplicationArguments 类型的参数,使用起来更加方便。

示例代码

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("应用启动完成,执行初始化操作");
    }
}

适用场景

  • 启动脚本执行:在应用启动后执行一些初始化脚本,如初始化数据库表、加载数据等。
  • 参数处理:根据命令行参数进行一些初始化配置,比如指定不同的环境配置文件。

5. ApplicationListener 监听 ContextRefreshedEvent 事件

解释

ContextRefreshedEvent 事件会在 Spring 容器刷新完成后发布,通过实现 ApplicationListener<ContextRefreshedEvent> 接口,可以监听该事件并在事件发生时执行初始化操作。

示例代码

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class MyContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("Spring 容器刷新完成,执行初始化操作");
    }
}

适用场景

  • 多容器场景初始化:在有多个 Spring 容器的场景中,确保在所有容器刷新完成后执行初始化操作。
  • 依赖容器完全初始化的操作:某些操作需要依赖 Spring 容器中的所有 Bean 都已经初始化完成,此时可以监听该事件。
posted @ 2025-03-14 18:05  皇问天  阅读(57)  评论(0)    收藏  举报