在Spring boot项目的实际开发中,我们有时需要项目服务启动时加载一些数据或预先完成某些动作。为了解决这样的问题,Spring boot 为我们提供了一个方法:通过实现接口 CommandLineRunner 来实现这样的需求。

实现方式:只需要一个类即可,无需其他配置。

实现步骤:

1.创建实现接口 CommandLineRunner 的类 MyStartupRunnerTest

  1. package com.energy;
  2. import org.springframework.boot.CommandLineRunner;
  3. import org.springframework.core.annotation.Order;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * Created by CavanLiu on 2017/2/28 0028.
  7. */
  8. @Component
  9. @Order(value=1)
  10. public class MyStartupRunnerTest implements CommandLineRunner
  11. {
  12. @Override
  13. public void run(String... args) throws Exception
  14. {
  15. System.out.println(">>>>This is MyStartupRunnerTest Order=1. Only testing CommandLineRunner...<<<<");
  16. }
  17. }

2.创建实现接口CommandLineRunner 的类 MyStartupRunnerTest2

  1. package com.energy;
  2. import org.springframework.boot.CommandLineRunner;
  3. import org.springframework.core.annotation.Order;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * Created by CavanLiu on 2017/2/28 0028.
  7. */
  8. @Component
  9. @Order(value=2)
  10. public class MyStartupRunnerTest2 implements CommandLineRunner
  11. {
  12. @Override
  13. public void run(String... args) throws Exception
  14. {
  15. System.out.println(">>>>This is MyStartupRunnerTest Order=2. Only testing CommandLineRunner...<<<<");
  16. }
  17. }

3.启动Spring boot后查看控制台输出信息,如下所示:

  1. >>>>This is MyStartupRunnerTest Order=1. Only testing CommandLineRunner...<<<<
  2. >>>>This is MyStartupRunnerTest2 Order=2. Only testing CommandLineRunner...<<<<

4.Application启动类代码略。

说明:CommandLineRunner接口的运行顺序是依据@Order注解的value由小到大执行,即value值越小优先级越高。

springboot 启动类CommandLineRunner(转载)的更多相关文章

  1. springboot 启动类启动跳转到前端网页404问题的两个解决方案

    前段时间研究springboot 发现使用Application类启动的话, 可以进入Controller方法并且返回数据,但是不能跳转到WEB-INF目录下网页, 前置配置 server: port ...

  2. 详谈springboot启动类的@SpringBootApplication注解

    前几天我们学会了如何创建springboot项目今天我们说一下他是怎么运行的为什么不需要我们再去编写繁重的配置文件的 @SpringBootApplication 首先我们看一下这个注解,他是用来标注 ...

  3. Springboot启动类及注解说明

    Spring boot的启动是基于main方法的,其主要注解为: 1. @springBootApplication:项目的启动注解,是一个组合注解,包含@SpringbootConfiguratio ...

  4. Springboot启动源码详解

    我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static voi ...

  5. SpringBoot启动流程分析(四):IoC容器的初始化过程

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  6. 深度好文,springboot启动原理详细分析

    我们开发任何一个Spring Boot项目,都会用到如下的启动类 1 @SpringBootApplication 2 public class Application { 3 public stat ...

  7. SpringBoot学习(二)探究Springboot启动机制

    引言: SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起探究一下SpringBoot的启动原 ...

  8. Springboot 启动分析

    启动类 Spring 启动类主要是 Annotation (@SpringBootApplication) 和 main 方法体中的 SpringApplication.run. 其中注解 @Spri ...

  9. SpringBoot启动流程源码分析

    前言 SpringBoot项目的启动流程是很多面试官面试中高级Java程序员喜欢问的问题.这个问题的答案涉及到了SpringBoot工程中的源码,也许我们之前看过别的大牛写过的有关SpringBoot ...

随机推荐

  1. Hive(五)数据类型与库表操作以及中文乱码

    一.数据类型 1.基本数据类型 Hive 支持关系型数据中大多数基本数据类型 类型 描述 示例 boolean true/false TRUE tinyint 1字节的有符号整数 -128~127 1 ...

  2. ueditor初始化

    一.下载文件复制到项目中 二.复制表情文件 三.复制列表图片 四.修改ueditor.config.js文件 五.接着修改net文件下config.json文件 六.完蛋了,不支持IE8,版本替换为了 ...

  3. 最小圆覆盖 gym-102006 I

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  4. Spark入门1(以WordCount为例讲解flatmap和map之间的区别)

    package com.test import org.apache.spark.{SparkConf, SparkContext} object WordCount { def main(args: ...

  5. IE访问历史记录恢复工具pasco

    IE访问历史记录恢复工具pasco IE浏览器会自动记录用户访问网站的操作,并将这些信息保存在IE缓存中的index.dat文件中.通过分析这些历史记录,可以还原用户行为.为了便于数字取证,Kali ...

  6. 防止sql注入的几种方法

    一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...

  7. DataTable,List,Dictonary互转,筛选及相关写法

    1.创建自定义DataTable  /// 创建自定义DataTable(一) 根据列名字符串数组, /// </summary> /// <param name="sLi ...

  8. 处理QMenu的triggered信号时遇到的一个问题

    最近,在一个Qt程序中使用QMenu类时,遇到了一个小问题,特记录下.首先,我模仿一下问题出现的场景:假设我在做一个高大上的XX管理系统,比如说:学生信息管理系统.在这个系统中,学生的各项信息(比如: ...

  9. C# 微信小程序获取openid sessionkey

    项目介绍 1.微信小程序获取openid和session_key 2.后台使用C#开发 项目流程 准备工作 1 获取appid 1.1 下载微信web开发工具 https://developers.w ...

  10. 【POJ】2796:Feel Good【单调栈】

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18449   Accepted: 5125 Case T ...