SpringBoot扩展点 项目启动后立即执行
2023-06-13 09:13:27 时间
在平时开发时可能要实现在项目启动后执行的一些功能,此时可以使用SpringBoot提供的这个接口。
SpringBoot中有两个接口可以实现CommandLineRunner 或 ApplicationRunner
CommandLineRunner
该接口只有一个run(String... args)方法。
触发时机为整个项目启动完毕后,自动执行。如果有多个CommandLineRunner,可以使用@Order来进行排序。
扩展例子(打印web与swagger地址):
@Slf4j
@Component
public class ProjectInfoPrint implements CommandLineRunner {
private final ApplicationContext applicationContext;
public ProjectInfoPrint(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void run(String... args) throws Exception {
Environment env = applicationContext.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = env.getProperty("server.port");
String appName = env.getProperty("spring.application.name");
log.info("\n----------------------------------------------------------\n" +
"Application " + appName + " is running! Access URLs:\n\t" +
"Local: \t\thttp://localhost:" + port + "/\n\t" +
"External: \thttp://" + ip + ":" + port + "/\n\t" +
"swagger-ui: http://" + ip + ":" + port + "/swagger-ui.html\n\t" +
"swagger-docs: http://" + ip + ":" + port + "/doc.html\n\t" +
"\n----------------------------------------------------------");
}
}
输出效果:
ApplicationRunner
该接口也只有一个run(ApplicationArguments args)方法,可以获得更多参数相关信息。
使用上与CommandLineRunner基本没什么区别,但这两个接口有优先顺序。
SpringApplication.callRunners
相关文章
- springboot mysql事物_SpringBoot事务详细简介[通俗易懂]
- docker快速安装fastdfs服务springboot访问
- 在SpringBoot项目中集成TDengine,并通过SQL对数据进行增删改查
- springboot validation 国际化
- 7大部分13章节561页笔记,彻底讲透SpringBoot生态体系及扩展
- springboot+vue练手级项目,真实的在线博客系统
- Springboot集成swagger_springboot nginx
- springboot多环境配置文件_Springboot教程
- springboot jdbc template_Springboot教程
- SpringBoot + Vue 学生选课管理系统源码(包含数据库文件)
- 【已解决】Springboot 启动时报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebSe
- springboot 之集成Redis
- SpringBoot版的低代码开发平台,关联无 SQL,性能高10倍!
- SpringBoot集成Redis及使用总结详解编程语言