Springboot初始化加载数据的方法

1、使用@PostConstruct注解

@PostConstruct
public void postConstructDemo(){
	// 执行相关业务
	System.out.println("PostConstruct初始化数据");
}

2、实现InitializingBean接口

@Component
public class InitializingBeanDemo implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        // 执行相关业务
        System.out.println("InitializingBeanDemo初始化数据");
    }
}

3、使用@Bean注解的initMethod执行初始化方法

@Bean(initMethod = "init")
public InitMethodDemo initMethodTest(){
	return new InitMethodDemo();
}

public class InitMethodDemo {

    public void init(){
        // 执行相关业务
        System.out.println("InitMethodDemo初始化数据");
    }

}

4、实现CommandLineRunner 接口

@Component
public class CommandLineRunnerDemo implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // 执行相关业务
        System.out.println("CommandLineRunnerDemo初始化数据");
    }
}

5、实现ApplicationRunner 接口

@Component
public class ApplicationRunnerDemo implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 执行相关业务
        System.out.println("ApplicationRunnerDemo初始化数据");
    }
}

6、实现ApplicationListener接口

@Component
public class ApplicationListenerDemo implements ApplicationListener, Ordered {

    @Override
    public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
        // 执行相关业务
        System.out.println("ApplicationListenerDemo初始化数据");
    }

    @Override
    public int getOrder() {
        return LOWEST_PRECEDENCE;
    }
}

持续更新中...

你可能感兴趣的:(redis缓存预热,springboot初始化数据,springboot初始化配置,springboot,java)