zoukankan      html  css  js  c++  java
  • SpringBoot

    前言

    CommandLineRunner接口在容器启动成功后最后一步调用,常用于应用程序启动后初始化操作,其在整个应用生命周期内只会执行一次。


    具体应用

    实现CommandLineRunner接口

    • Runner.java
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Runner implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            /**
             * TODO:
             */
            System.out.println(" Runner run ");
        }
    }
    
    • CommandLineRunner会在Spring Boot容器加载之后执行

    在这里插入图片描述


    CommandLineRunner实现类的执行顺序

    • 使用@Order注解定义CommandLineRunner的执行顺序
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    @Component
    @Order(1)
    public class RunnerOne implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            /**
             * TODO:
             */
            System.out.println(" RunnerOne run ");
        }
    }
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    @Component
    @Order(2)
    public class RunnerTwo implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            /**
             * TODO:
             */
            System.out.println(" RunnerTwo run ");
        }
    }
    

    在这里插入图片描述


    - End -
    梦想是咸鱼
    关注一下吧
    以上为本篇文章的主要内容,希望大家多提意见,如果喜欢记得点个推荐哦
    作者:Maggieq8324
    本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。
  • 相关阅读:
    POJ 1887 Testing the CATCHER
    HDU 3374 String Problem
    HDU 2609 How many
    POJ 1509 Glass Beads
    POJ 1458 Common Subsequence
    POJ 1159 Palindrome
    POJ 1056 IMMEDIATE DECODABILITY
    POJ 3080 Blue Jeans
    POJ 1200 Crazy Search
    软件体系结构的艺术阅读笔记1
  • 原文地址:https://www.cnblogs.com/maggieq8324/p/15188997.html
Copyright © 2011-2022 走看看