在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. HBase(二)CentOS7.5搭建HBase1.2.6HA集群

    一.安装前提 1.HBase 依赖于 HDFS 做底层的数据存储 2.HBase 依赖于 MapReduce 做数据计算 3.HBase 依赖于 ZooKeeper 做服务协调 4.HBase源码是j ...

  2. BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14982  Solved: 6081[Submit ...

  3. ffmpeg 添加filter步骤

    a). 自己写一个XXX.c文件,比如vf_transform.c,放在libavfilter目录下.代码可以参考其他filter: b) 在libavfilter/allfilters.c添加一行: ...

  4. Yii2之控制台命令篇(console)

    控制台命令 Yii 中有一个拥有丰富功能的控制台,它们主要用于创建网站后台处理的任务.在项目根目录下执行相关操作,有意思的事,可以通过 yii 自带的功能,列出当前已有的命令. 1.查看当前项目控制台 ...

  5. PHP版本--HTTP session cookie原理及应用

    PHP 的COOKIE cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制.     PHP在http 协议的头信息里发送cookie,因此  setcookie()函数必须在其它 ...

  6. 采用Post请求的方式提交参数并导出excel

    一般情况下,我们都是采用get请求的方式导出excel.例如采用如下方式: var exportUrl = '/xxx;'; window.open(exportUrl); 导出excel所需的逻辑参 ...

  7. JAVAEE——BOS物流项目14:Linux部署(安装jdk、tomcat、mySQL)和扩展资料

    1 学习计划 1.Linux部署 n 安装jdk n 安装tomcat n 安装MySQL n 将项目发布到tomcat 2.扩展资料 2 Linux部署 2.1 安装jdk 第一步:获取Linux系 ...

  8. Linux-UDP-Socket编程

    接收CAN总线上的数据并将其发送出去 创建客户端: /******************************************************************** * co ...

  9. [ 转载 ] Java Jvm内存介绍

    一.基础理论知识 1.java虚拟机的生命周期: Java虚拟机的生命周期 一个运行中的Java虚拟机有着一个清晰的任务:执行Java程序.程序开始执行时他才运行,程序结束时他就停止.你在同一台机器上 ...

  10. Python 面向对象编程——类定义与对象

    <类定义与对象声明> 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对 ...