专栏名称: 码农小胖哥
技术公众号:码农小胖哥
目录
相关文章推荐
51好读  ›  专栏  ›  码农小胖哥

Spring Boot 2 实战:自定义启动运行逻辑

码农小胖哥  · 掘金  ·  · 2019-11-03 03:08

正文

阅读 41

Spring Boot 2 实战:自定义启动运行逻辑

1. 前言

不知道你有没有接到这种需求,项目启动后立马执行一些逻辑。比如缓存预热,或者上线后的广播之类等等。可能现在没有但是将来会有的。想想你可能的操作, 写个接口上线我调一次行吗?NO!NO!NO!这种初级菜鸟才干的事。今天告诉你个骚操作使得你的代码更加优雅,逼格更高。

2. CommandLineRunner 接口

 package org.springframework.boot;
 
 import org.springframework.core.Ordered;
 import org.springframework.core.annotation.Order;
 
 /**
  * Interface used to indicate that a bean should <em>run</em> when it is contained within
  * a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined
  * within the same application context and can be ordered using the {@link Ordered}
  * interface or {@link Order @Order} annotation.
  * <p>
  * If you need access to {@link ApplicationArguments} instead of the raw String array
  * consider using {@link ApplicationRunner}.
  *
  * @author Dave Syer
  * @see ApplicationRunner
  */
 @FunctionalInterface
 public interface CommandLineRunner {
 
     /**
      * Callback used to run the bean.
      * @param args incoming main method arguments
      * @throws Exception on error
      */
     void run(String... args) throws Exception;
 
 }复制代码

CommandLineRunner 作用是当springApplication 启动后,在同一应用上下文中定义的多个 CommandLineRunner 类型的 Spring Bean 按照标记顺序执行。如果你想替代以数组方式接收 args 参数 可以用 另一个接口代替 org.springframework.boot.ApplicationRunner

talk is cheap show your code 下面我就来操作一波演示一下。

2.1 优先级比较高的 CommandLineRunner 实现

 package cn.felord.begin;
 
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.core.Ordered;
 import org.springframework.stereotype.Component;
 
 /**
  * 优先级比较高 通过实现接口{@link Ordered}的方式 来指定优先级
  *  命令行测试参数     --foo=bar --dev.name=码农小胖哥  java,springboot
  * @author Felordcn
  * @since 2019/6/17 23:06
  */
 @Slf4j
 @Component
 public class HighOrderCommandLineRunner implements CommandLineRunner , Ordered {
     @Override
     public void run(String... args) throws Exception {

       log.info("i am  highOrderRunner");
     }
 
     @Override
     public int getOrder() {
         return Ordered.HIGHEST_PRECEDENCE;
     }
 } 复制代码

2.2 优先级比较低的 CommandLineRunner 实现:

 package cn.felord.begin;
 
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.core.Ordered;
 import org.springframework.core.annotation.Order;
 import org.springframework.stereotype.Component;
 
 /**
  * 优先级比较低 通过注解{@link Order}方式来指定优先级
  * 比最优大64 说明会在 {@link HighOrderCommandLineRunner} 之后执行
  *
  * @author Felord
  * @since 2019/6/17 23:07
  */
 @Slf4j
 @Order(Ordered.HIGHEST_PRECEDENCE + 64)
 @Component
 public class LowOrderCommandLineRunner implements CommandLineRunner {
     @Override
     public void run(String... args) throws Exception {
         log.info("i am  lowOrderRunner");
     }
 }复制代码

2.3 用 ApplicationRunner 实现最低优先级:

 package cn.felord.begin;
 
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.ApplicationArguments;
 import org.springframework.boot.ApplicationRunner;
 import org.springframework.core.Ordered;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 import java.util.List;
 
 /**
  * 优先级最低的实现
  * @author Felordcn
  * @since 2019/6/18 22:13
  */
 @Slf4j
 @Component
 public class DefaultApplicationRunner implements ApplicationRunner, Ordered {
     @Override
     public void run(ApplicationArguments args) throws Exception {
 
         log.info("i am applicationRunner");
         
     }
 
     @Override
     public int getOrder() {
         return Ordered.HIGHEST_PRECEDENCE+65;
     }
 }复制代码

启动springboot 后控制台打印出了执行结果:

 2019-11-02 21:18:14.603  INFO 10244 --- [           main] c.f.begin.HighOrderCommandLineRunner   : i am  highOrderRunner
 2019-11-02 21:18:14.604  INFO 10244 --- [           main] c.f.begin.LowOrderCommandLineRunner    : i am  lowOrderRunner
 2019-11-02 21:18:14.604  INFO 10244 --- [           main] c.f.begin.DefaultApplicationRunner     : i am applicationRunner复制代码






请到「今天看啥」查看全文