springboot 启动类CommandLineRunner(转载)
在Spring boot项目的实际开发中,我们有时需要项目服务启动时加载一些数据或预先完成某些动作。为了解决这样的问题,Spring boot 为我们提供了一个方法:通过实现接口 CommandLineRunner 来实现这样的需求。
实现方式:只需要一个类即可,无需其他配置。
实现步骤:
1.创建实现接口 CommandLineRunner 的类 MyStartupRunnerTest
- package com.energy;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.core.annotation.Order;
- import org.springframework.stereotype.Component;
- /**
- * Created by CavanLiu on 2017/2/28 0028.
- */
- @Component
- @Order(value=1)
- public class MyStartupRunnerTest implements CommandLineRunner
- {
- @Override
- public void run(String... args) throws Exception
- {
- System.out.println(">>>>This is MyStartupRunnerTest Order=1. Only testing CommandLineRunner...<<<<");
- }
- }
2.创建实现接口CommandLineRunner 的类 MyStartupRunnerTest2
- package com.energy;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.core.annotation.Order;
- import org.springframework.stereotype.Component;
- /**
- * Created by CavanLiu on 2017/2/28 0028.
- */
- @Component
- @Order(value=2)
- public class MyStartupRunnerTest2 implements CommandLineRunner
- {
- @Override
- public void run(String... args) throws Exception
- {
- System.out.println(">>>>This is MyStartupRunnerTest Order=2. Only testing CommandLineRunner...<<<<");
- }
- }
3.启动Spring boot后查看控制台输出信息,如下所示:
- >>>>This is MyStartupRunnerTest Order=1. Only testing CommandLineRunner...<<<<
- >>>>This is MyStartupRunnerTest2 Order=2. Only testing CommandLineRunner...<<<<
4.Application启动类代码略。
说明:CommandLineRunner接口的运行顺序是依据@Order注解的value由小到大执行,即value值越小优先级越高。
springboot 启动类CommandLineRunner(转载)的更多相关文章
- springboot 启动类启动跳转到前端网页404问题的两个解决方案
前段时间研究springboot 发现使用Application类启动的话, 可以进入Controller方法并且返回数据,但是不能跳转到WEB-INF目录下网页, 前置配置 server: port ...
- 详谈springboot启动类的@SpringBootApplication注解
前几天我们学会了如何创建springboot项目今天我们说一下他是怎么运行的为什么不需要我们再去编写繁重的配置文件的 @SpringBootApplication 首先我们看一下这个注解,他是用来标注 ...
- Springboot启动类及注解说明
Spring boot的启动是基于main方法的,其主要注解为: 1. @springBootApplication:项目的启动注解,是一个组合注解,包含@SpringbootConfiguratio ...
- Springboot启动源码详解
我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static voi ...
- SpringBoot启动流程分析(四):IoC容器的初始化过程
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- 深度好文,springboot启动原理详细分析
我们开发任何一个Spring Boot项目,都会用到如下的启动类 1 @SpringBootApplication 2 public class Application { 3 public stat ...
- SpringBoot学习(二)探究Springboot启动机制
引言: SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起探究一下SpringBoot的启动原 ...
- Springboot 启动分析
启动类 Spring 启动类主要是 Annotation (@SpringBootApplication) 和 main 方法体中的 SpringApplication.run. 其中注解 @Spri ...
- SpringBoot启动流程源码分析
前言 SpringBoot项目的启动流程是很多面试官面试中高级Java程序员喜欢问的问题.这个问题的答案涉及到了SpringBoot工程中的源码,也许我们之前看过别的大牛写过的有关SpringBoot ...
随机推荐
- 2015 icpc北京赛区 D 最小割
题目大意:给你一棵技能树,如果要学习一个技能,那么它之前的技能要全部学完,第 i 个点需要ai 能学习 每条边有一个消耗c 如果支付c那么就能去掉这条边, 你还可以kejin 花费di 就能直接学习 ...
- Django实战(4):scaffold生成物分析
在上一节用一个插件生成了类似rails的scaffold,其实无非就是URLconf+MTV.让我们看看具体都生成了哪些东西. 首先是“入口”的定义即URLconf,打开urls.py: from d ...
- IP、TCP和DNS与HTTP的密切关系
看了上一篇博文的发表时间,是7月22日,现在是10月22日,已经有三个月没写博客了.这三个月里各种忙各种瞎折腾,发生了很多事情,也思考了很多问题.现在这段时间开始闲下来了,同时该思考的事情也思考清楚了 ...
- Qt中两种定时器用法
在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度. 1.QObject类的定时器 QObj ...
- linux 下安装jdk环境安装
一.创建jdk安装目录mkdir /usr/local/java 二.将jdk解压到安装目录中,直接到java目录中,如果不是处理下不要有子目录 tar -zxvf jdk-8u91-linux-x6 ...
- CSUOJ 1270 Swap Digits
Description ) in the first line, which has the same meaning as above. And the number is in the next ...
- iOS 9应用开发教程之定制应用程序图标以及真机测试
iOS 9应用开发教程之定制应用程序图标以及真机测试 定制ios9应用程序图标 在图1.12中可以看到应用程序的图标是网状白色图像,它是iOS模拟器上的应用程序默认的图标.这个图标是可以进行改变的.以 ...
- python opencv3 圆检测
git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 import numpy as np img_ori ...
- WinForm 使用 NPOI 2.2.1从datatable导出Excel
最新的NOPI应该是2.3了,但在官网上还是2.2.1. 也是第一次使用NPOI来导出Excel文件. 在写的时候搜不到2.2.1的教程,搜了一个2.2.0的教程. 不过也没什么问题,NPOI是真的方 ...
- 递归与分治策略之棋盘覆盖Java实现
递归与分治策略之棋盘覆盖 一.问题描述 二.过程详解 1.棋盘如下图,其中有一特殊方格:16*16 . 2.第一个分割结果:8*8 3.第二次分割结果:4*4 4.第三次分割结果:2*2 5.第四次分 ...