美文网首页
SpringBoot开机启动的两种方式

SpringBoot开机启动的两种方式

作者: 凯凯frank | 来源:发表于2020-02-18 23:13 被阅读0次

    1. 实现ApplicationRunner接口

    // runner/BootApplicationRunner.java
    @Component
    //@Order(2)
    public class BootApplicationRunner implements ApplicationRunner {
        private static final Logger logger = LoggerFactory.getLogger(BootApplicationRunner.class);
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            logger.info("This is ApplicationRunner....");
        }
    }
    

    2.实现CommandLineRunner接口

    // runner/BootCommandLineRunner.java
    @Component
    //@Order(1)
    public class BootCommandLineRunner implements CommandLineRunner {
        private static final Logger logger = LoggerFactory.getLogger(BootApplicationRunner.class);
    
        @Override
        public void run(String... args) throws Exception {
            logger.info("this is CommandLineRunner...");
        }
    }
    
    

    区别:传入的参数不同,ApplicationRunner的优先级高于CommandLineRunner。
    可以使用@Order(1)注解改变优先级,数字越小,优先级越高

    相关文章

      网友评论

          本文标题:SpringBoot开机启动的两种方式

          本文链接:https://www.haomeiwen.com/subject/kmtofhtx.html