SpringBoot启动加载数据CommandLineRunner


前言

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

order越小越先执行

1、order为1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.hlj.commandlinerunner.comhljcommandlinerunner.orlder;

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

/**
* @Description
* @Author HealerJean
* @Date 2018/3/29 下午6:18.
*/
@Component
@Order(value=1)
public class MyStartupRunner1 implements CommandLineRunner {

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

}


2、order为2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.hlj.commandlinerunner.comhljcommandlinerunner.orlder;

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

/**
* @Description
* @Author HealerJean
* @Date 2018/3/29 下午6:18.
*/
@Component
@Order(value=2)
public class MyStartupRunner2 implements CommandLineRunner {

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

}

3、启动SpringBoot

WX20180329-182220





如果满意,请打赏博主任意金额,感兴趣的请下方留言吧。可与博主自由讨论哦

支付包 微信 微信公众号
支付宝 微信 微信公众号
 
   

Author: jony
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source jony !
 Previous
Java虚拟机内存分配和溢出 Java虚拟机内存分配和溢出
前言由于虚拟机自动内存管理机制的帮助下,不需要为每一个new出来的对象,去写delete/free代码。不容易出现内存泄漏和内存溢出的问题, 但是如果一旦出现问题,那将是非常可怕的 ,如果不了解虚拟机是怎么使用内存的,那么排查错误将会成
2018-04-08
Next 
Spring Boot Web 应用加速 Spring Boot Web 应用加速
Spring Boot Web 应用加速默认情况下,Spring Boot Web 应用会装配一些功能组件 Bean。 在大多数 Web 应用场景下,可以选择性地关闭一下自动装配的Spring 组件 Bean,以达到提升性能的目的。 配置项
2018-04-08
  TOC