SpringBoot启动时初始化参数

1、通过@PostConstruct注解进行初始化

@Component
public class InitParam {
    @PostConstruct
    public void init(){
        System.out.println("==========@PostConstruct初始化");
    }
}

2、通过实现InitializingBean接口,重写afterPropertiesSet方法进行初始化

@Component
public class InitParam implements InitializingBean{
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("==========实现InitializingBean接口初始化");
    }
}

3、通过监听器方式,实现ApplicationListener,重写onApplicationEvent方法

@Component
public class InitParam implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("==========实现ApplicationListener监听器初始化");
    }
}

4、通过实现CommandLineRunner接口,Spring Boot项目启动时会执行run方法

@Component
public class InitParam implements CommandLineRunner {
    @Override
	public void run(String... args) throws Exception {
        System.out.println("==========实现CommandLineRunner接口初始化");
    }
}
posted @ 2023-05-24 17:15  土豆泥呀  阅读(364)  评论(0)    收藏  举报