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 @   土豆泥呀  阅读(343)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示