Spring boot 在启动时加载数据

CommandLineRunner 接口

  • 通过 实现 CommandLineRunner 接口去实现 数据的添加, 这种方式可以在所有方法都启动完后 在去启动添加数据,通过 @Order注解的加载顺序 value 值越小加载优先级越高
  • 参数: CommandLineRunner 接口的 run 方法接收一个 String[] 参数,表示命令行参数。
  • 应用场景: 适用于需要根据命令行参数执行特定任务的情况,例如导入数据、初始化配置等。
    注意: 由于 该线程 是 main 线程是主线程 如果数据获取有问题是会导致项目无法启动
@Order(1)
@Component
public class FengxinConfig implements CommandLineRunner {
	 @Override
    public void run(String... args) throws Exception {
			...
    }
}

注意: 由于 该线程 是 main 线程是主线程 如果数据获取有问题是会导致项目无法启动, 可以通过 Thread 方法去新建一个线程 去启动

@Order(1)
@Component
public class FengxinConfig implements CommandLineRunner {
	 @Override
    public void run(String... args) throws Exception {
    	new Thread(){
            @Override
            public void run() {
            	...
            }
    	};
    }
}

ApplicationRunner 接口

  • 与上一个保持一致, 但 ApplicationRunner 接口也用于执行启动后的初始化任务,但它接收一个 ApplicationArguments 对象,这个对象包含了命令行参数以及更多元数据,例如非选项参数和选项参数。
  • 参数: ApplicationRunner 接口的 run 方法接收一个 ApplicationArguments 对象,它包含了命令行参数和其他元数据。
  • 应用场景: 适用于需要访问更多元数据(如非选项参数、选项参数及其值)的情况。
@Component
public class FengxinConfig implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 处理命令行参数
        System.out.println("Non-option arguments: " + args.getNonOptionArgs());
        System.out.println("Option names: " + args.getOptionNames());
        System.out.println("Option values: " + args.getOptionValues("optionName"));
    }
}

ApplicationRunner 接口 与 CommandLineRunner 接口 区别

  • 命令行参数处理:
    • CommandLineRunner 直接接收一个 String[] 参数。
    • ApplicationRunner 接收一个 ApplicationArguments 对象,提供了更多的元数据和方法来处理命令行参数。
  • 灵活性:
    • ApplicationRunner 更加灵活,因为它可以处理非选项参数和选项参数。
    • CommandLineRunner 相对简单,只处理命令行参数本身。
  • 使用场景:
    • 如果你需要更详细的命令行参数信息,建议使用 ApplicationRunner。
    • 如果你的任务比较简单,只需要处理命令行参数,可以使用 CommandLineRunner。

Spring框架提供的 InitializingBean 接口

Spring 框架提供的 InitializingBean 接口是一个用于执行初始化逻辑的回调接口。它允许在 Spring 容器完成所有依赖注入之后执行一些额外的初始化操作。当 Spring 容器实例化了一个实现了 InitializingBean 接口的 Bean 时,它会在所有依赖注入完成后调用该接口的 afterPropertiesSet() 方法。

  • 资源初始化
  •   如果你需要在依赖注入完成后对某些资源进行初始化,可以使用 InitializingBean。
    
  • 验证依赖注入
  •   可以在 afterPropertiesSet() 方法中检查所有必需的属性是否已被正确注入。
    
  • 执行额外的配置
  •   执行一些额外的配置任务,例如打开连接、预加载数据等。
    
import org.springframework.beans.factory.InitializingBean;

public class MyBean implements InitializingBean {

    private String property;

    public void setProperty(String property) {
        this.property = property;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 执行额外的初始化操作
        System.out.println("InitializingBean - afterPropertiesSet() called");
        // 检查属性是否已正确注入
        if (property == null) {
            throw new IllegalStateException("Property must be set!");
        }
        // 其他初始化逻辑
    }
}

假设你有一个 MyBean 类实现了 InitializingBean 接口,你可以在 Spring 配置中注入这个 Bean,并让 Spring 自动调用 afterPropertiesSet() 方法:
(如果 afterPropertiesSet() 抛出异常,Spring 将不会继续初始化该 Bean)

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

@Configuration
public class AppConfig {

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

Java EE5 引进的 PostConstruct

@PostConstruct 是一个由 Java EE 规范定义的注解,用于标记一个非私有的 void 方法,该方法将在依赖注入完成后由容器调用。@PostConstruct 注解通常用于执行一些初始化操作,这些操作对于 Bean 的正常工作至关重要。它是 JSR-250 规范的一部分,并且被 Spring 框架支持。

import javax.annotation.PostConstruct;

public class MyBean {

    private String property;

    public void setProperty(String property) {
        this.property = property;
    }

    @PostConstruct
    public void init() {
        // 执行额外的初始化操作
        System.out.println("@PostConstruct - init() called");
        // 检查属性是否已正确注入
        if (property == null) {
            throw new IllegalStateException("Property must be set!");
        }
        // 其他初始化逻辑
    }
}
  • 使用 @PostConstruct:
    • 使用 @PostConstruct 注解标记一个非私有的 void 方法。
    • 在该方法中执行必要的初始化逻辑。
  • Spring 容器调用:
    • 当 Spring 容器完成依赖注入后,会自动调用 @PostConstruct 方法。
    • 如果 @PostConstruct 方法抛出异常,Spring 将不会继续初始化该 Bean。
  • 与其他初始化方法的关系:
    • @PostConstruct 方法将在 InitializingBean 接口的 afterPropertiesSet() 方法之后调用。
    • 如果你同时使用 InitializingBean 接口和 @PostConstruct 注解,则 afterPropertiesSet() 方法将先于 @PostConstruct 方法执行。
  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值