springboot细节挖掘(数据初始化)

本文介绍了Spring Boot加载启动初始化数据的方法。可通过实现CommandLineRunner和ApplicationRunner接口,编写初始化逻辑并注册成Bean。还提到Spring Bean初始化的InitializingBean、init - method和PostConstruct,指出spring初始化bean在ApplicationRunner和CommandLineRunner接口调用之前。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何加载一些启动就需要的初始化数据呢?

CommandLineRunner

spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现

  • 定义初始化类 MyCommandLineRunner
  • 实现 CommandLineRunner 接口,并实现它的 run() 方法,在该方法中编写初始化逻辑
  • 注册成Bean,添加 @Component注解即可
  • 示例代码如下:


package com.shxseer.watch;

import com.shxseer.watch.netty.server.NettyServer;
import com.shxseer.watch.thread.ExecutorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author yangsonglin
 * @create 2018-07-09 15:34
 **/
@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    NettyServer nettyServer;

    @Autowired
    ExecutorService executorService;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        executorService.start();
        nettyServer.start();
    }
}

 

ApplicationRunner

  • 定义初始化类 MyApplicationRunner
  • 实现 ApplicationRunner 接口,并实现它的 run() 方法,在该方法中编写初始化逻辑
  • 注册成Bean,添加 @Component注解即可
  • 示例代码如下:
@Component
public class MyApplicationRunner implements ApplicationRunner {
 
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("...init resources by implements ApplicationRunner");
    }
 
}

@Order注解规定了CommandLineRunner实例的运行顺序。@order(value=2) value 的值从小到大依次执行。

@Component
@Order(1)
public class MyCommandLineRunner implements CommandLineRunner {
 
    @Override
    public void run(String... args) throws Exception {
        System.out.println("...init resources by implements CommandLineRunner");
    }
}
@Component
@Order(2)
public class MyApplicationRunner implements ApplicationRunner {
 
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("...init resources by implements ApplicationRunner");
    }
 
}

 

spring的初始化数据:

Spring Bean初始化的InitializingBean,init-method和PostConstruct

InitializingBean接口

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet()方法。

在spring初始化bean的时候,如果bean实现了InitializingBean接口,在对象的所有属性被初始化后之后才会调用afterPropertiesSet()方法
 

@Component
public class InitialingzingBeanTest implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean..");
    }
}

我们可以看出spring初始化bean肯定会在 ApplicationRunner和CommandLineRunner接口调用之前。

init-method和@PostConstruct
前面就说过官方文档上不建议使用InitializingBean接口,但是我们可以在<bean>元素的init-method属性指定bean初始化之后的操作方法,或者在指定方法上加上@PostConstruct注解来制定该方法在初始化之后调用
 

@SpringBootApplication
public class Application {


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @PostConstruct
    public void init() {
        System.out.println("init...");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值