概述
开发中可能会有这样的场景,需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求,两个启动加载接口分别是:CommandLineRunner和ApplicationRunner。Spring 提供了接口 InitializingBean,jdk提供了@PostConstruct.
参考文档:
CommandLineRunner、ApplicationRunner、InitializingBean、@PostConstruct 执行顺序_commandlinerunner 启动顺序-CSDN博客
用于在 Spring Boot 应用程序启动后自动执行一些特定的逻辑---ApplicationRunner 可单独运行某个功能,不想用Controller调用时_spring applicationrunner controller-CSDN博客
@PostConstruct 初使化_枚举类@postconstruct-CSDN博客