zoukankan      html  css  js  c++  java
  • 170704、springboot编程之CommandLineRunner

    实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求。 
    为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现。

    1、编写CommandLineRunnerDemo1 类,继承CommandLineRunner

    
    
    package com.rick.common.init;

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    /**
    * Desc : 服务启动完成执行,可以有多个,根据@Order指定的顺序执行,数值越小,优先级越高
    * User : RICK
    * Time : 2017/8/22 15:25
    */

    @Component
    @Order(value = 1)
    public class CommandLineRunnerDemo1 implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
    System.out.println("--------CommandLineRunnerDemo1--------");
    System.out.println(">>>>>>>>>>>>>>>服务器初始化完成<<<<<<<<<<<<");
    }
    }
     

    Spring Boot应用程序在启动后,会遍历CommandLineRunner接口的实例并运行它们的run方法。也可以利用@Order注解(或者实现Order接口)来规定所有CommandLineRunner实例的运行顺序。

    如下我们使用@Order 注解来定义执行顺序。

    2、编写CommandLineRunnerDemo2 类,继承CommandLineRunner

    package com.rick.common.init;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    /**
     * Desc :  服务启动完成执行,可以有多个,根据@Order指定的顺序执行,数值越小,优先级越高
     * User : RICK
     * Time : 2017/8/22 15:25
      */
    
    @Component
    @Order(value = 2)
    public class CommandLineRunnerDemo2 implements CommandLineRunner {
        @Override
        public void run(String... strings) throws Exception {
            System.out.println("--------CommandLineRunnerDemo2--------");
            System.out.println(">>>>>>>>>>>>>>>服务器初始化完成<<<<<<<<<<<<");
        }
    }

    3、启动服务测试

  • 相关阅读:
    asp.net连接mssql server的方式
    mssql如何创建简单的存储过程
    varchar和nvarchar的区别
    C#中关键字partial的作用
    oracle创建用户命令
    在linux上安装音频视频播放器解码器(打造完美播放器)
    oracle创建表空间
    windows console application链接数据库读取数据
    今天学习asp.net mvc的过程中出现了一点问题,是有关浏览器的,一个疑问?
    华为t8100刷机之后出现illegal version 不久开始关机,提示非法版本
  • 原文地址:https://www.cnblogs.com/zrbfree/p/7411342.html
Copyright © 2011-2022 走看看