# SpringBoot中CommandLineRunner的作用
> 平常开发中有可能需要实现在项目启动后执行的功能,SpringBoot提供的一种简单的实现方案就是添加一个model并实现CommandLineRunner接口,实现功能的代码放在实现的run方法中
# 简单例子
``` java 
package org.springboot.sample.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyStartupRunner implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作<<<<<<<<<<<<<");
}

}
```
# 如果有多个类实现CommandLineRunner接口,如何保证顺序
> SpringBoot在项目启动后会遍历所有实现CommandLineRunner的实体类并执行run方法,如果需要按照一定的顺序去执行,那么就需要在实体类上使用一个@Order注解(或者实现Order接口)来表明顺序
```
package org.springboot.sample.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value=2)
public class MyStartupRunner1 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行 2222 <<<<<<<<<<<<<");
}

}
```
```
package org.springboot.sample.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value=1)
public class MyStartupRunner2 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行 111111 <<<<<<<<<<<<<");
}

}
```
> 控制台显示
```
>>>>>>>>>>>>>>>服务启动执行 11111111 <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服务启动执行 22222222## 标题 ## <<<<<<<<<<<<<
```
> 根据控制台结果可判断,@Order 注解的执行优先级是按value值从小到大顺序。

转载:https://www.cnblogs.com/myblogs-miller/p/9046425.html

https://www.cnblogs.com/myblogs-miller/p/9046425.html的更多相关文章

  1. 访问路径:https://i.cnblogs.com/posts?categoryid=925678

    https://i.cnblogs.com/posts?categoryid=925678

  2. URL https://i.cnblogs.com/EditPosts.aspx?opt=1

    URL url = new URL("https://i.cnblogs.com");URL url1 = new URL(url, "EditPosts.aspx?op ...

  3. 随笔二-https://www.cnblogs.com/shang1680/p/9657994.html

    作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...

  4. 211806189杨昊辰 https://www.cnblogs.com/honey1433223/

    211806189杨昊辰 https://www.cnblogs.com/honey1433223/

  5. https://www.cnblogs.com/h2zZhou/p/5440271.html

    https://www.cnblogs.com/h2zZhou/p/5440271.html

  6. https://www.cnblogs.com/soundcode/p/4174410.html

    https://www.cnblogs.com/soundcode/p/4174410.html 1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. ...

  7. https://www.cnblogs.com/yudanqu/p/9467803.html

    https://www.cnblogs.com/yudanqu/p/9467803.html

  8. 转发自:一像素 十大经典排序算法(动图演示)原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删

    原链接:https://www.cnblogs.com/onepixel/articles/7674659.html     个人收藏所用   侵删 0.算法概述 0.1 算法分类 十种常见排序算法可 ...

  9. @无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 通篇引用

    无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 关于Async与Await的FAQ 关于Async与Await的 ...

随机推荐

  1. flask框架(二)——flask4剑客、flask配置文件的4种方式

    之前学习的Django有必备三板斧:render,HttpResponse,redirect,JsonResponse 在flask也有,但是有些不同 一.Flask4剑客 1.直接返回字符串(ret ...

  2. python_二叉树简单实现

    今日头条面试题,先做下: 二叉树代码实现 class Node: def __init__(self,item): self.item = item self.child1 = None self.c ...

  3. robotframework_javaScript定位

    整理笔记才发现,只有在rebotframework才用过js定位,那么如果有小伙伴在使用js遇到问题,给我留言吧 通过Id定位   name定位 通过标签名查找 HTML 元素 本例查找 id=&qu ...

  4. appium 环境准备

      一.环境准备 1.相关依赖环境 当前的环境是Windows 10版本 64位系统(32位的自己想办法吧)          1.jdk1.6.0 (64位)  --最好1.6以上版本      2 ...

  5. uni-app项目导入第三方组件库muse-ui

    你说uni-app是什么 我说,uni-app是一套基于vue.js开发跨平台应用的前端框架,可编译多个平台,比如:Android.IOS.H5.微信小程序.支付宝小程序.头条小程序.百度小程序 懂行 ...

  6. Linux sftp命令

    sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的网络的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 ...

  7. IdentityServer4实现OAuth2.0四种模式之客户端模式

    一,准备内容 IdentityServer4 是Asp.net core的一个中间件,用于添加符合OpenId Connect和OAuth2.0规范的终端到Asp.net Core应用.在这里简单介绍 ...

  8. fastDFS的入门程序

    导入jar包 <dependency> <groupId>cn.bestwu</groupId> <artifactId>fastdfs-client- ...

  9. org.w3c.dom document 和xml 字符串 互转

    转自:https://blog.csdn.net/wmyasw/article/details/8686420 package com.mymhotel.opera; import java.io.F ...

  10. Spring事务传播机制与隔离机制

    详情查看 https://www.jianshu.com/p/249f2cd42692