SpringBoot项目启动时自动执行指定方法
2018-09-17 19:16
741 查看
SpringBoot项目启动时自动执行指定方法
在SpringBoot中,有两种接口方式实现启动执行,分别是ApplicationRunner和CommandLineRunner,除了可接受参数不同,其他的大同小异
ApplicationRunner:
package cn.king.glasses.modular.system.schedule; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * 继承 ApplicationRunner 接口后项目启动时会按照执行顺序执行 run 方法 * @version: * @Description: * @author fxg * @date: 2018年9月17日 下午5:21:06 * */ @Component @Order(value = 1) public class StartService implements ApplicationRunner { private static Logger logger = LoggerFactory.getLogger(StartService.class); @Override public void run(ApplicationArguments args) throws Exception { logger.info("=========== 项目启动后,初始化 Redis ============="); } }[/code]
CommandLineRunner:
import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import java.util.Date; @Component public class MyCommandLineRunner implements CommandLineRunner,Ordered{ @Override public int getOrder(){ return 1; //返回执行顺序 } @Override public void run(String... var1) throws Exception{ System.out.println(new Date()); } }[/code]
我们可以直接在run()方法中编写我们需要执行的方法即可。
相关文章推荐
- springboot项目启动执行特定方法
- Spring Boot--项目启动后默认执行特定的方法
- springboot项目启动后第一次执行controller方法时进入很慢
- springboot+springcloud微服务模块启动完成后自动执行指定的一段代码去完成一些事情
- springboot 学习之路 9 (项目启动后就执行特定方法)
- spring boot在启动项目之后执行的实现方法
- Spring Boot学习--项目启动时执行特定方法(对于quartz 任务调度一样也是可行)
- 项目启动 spring初始化bean之后执行指定方法
- spring项目启动完成后,自动执行一次某个特定方法。
- Spring Boot学习--项目启动时执行特定方法
- springboot项目执行controller方法时进入慢的问题
- springboot 启动后执行特定的方法
- 在springboot中,如何做到当项目启动的时候去执行这个逻辑
- Spring项目启动时执行初始化方法
- java如何实现项目启动时执行指定方法
- SpringBoot项目不占用端口启动的方法
- Mac idea 启动SpringBoot 项目慢解决方法
- SpringBoot web demo项目启动后立马自动关闭问题解决办法
- Intellij IDEA实现SpringBoot项目多端口启动的两种方法
- Spring Boot解决项目启动时初始化资源的方法