最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 啟動(dòng)加載數(shù)據(jù) CommandLineRunner的使用

 更新時(shí)間:2017年04月21日 11:26:51   作者:catoop  
本篇文章主要介紹了Spring Boot 啟動(dòng)加載數(shù)據(jù) CommandLineRunner的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

實(shí)際應(yīng)用中,我們會(huì)有在項(xiàng)目服務(wù)啟動(dòng)的時(shí)候就去加載一些數(shù)據(jù)或做一些事情這樣的需求。

為了解決這樣的問題,spring Boot 為我們提供了一個(gè)方法,通過實(shí)現(xiàn)接口 CommandLineRunner 來實(shí)現(xiàn)。

很簡(jiǎn)單,只需要一個(gè)類就可以,無需其他配置。

創(chuàng)建實(shí)現(xiàn)接口 CommandLineRunner 的類

package org.springboot.sample.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 服務(wù)啟動(dòng)執(zhí)行
 *
 * @author  單紅宇(365384722)
 * @create  2016年1月9日
 */
@Component
public class MyStartupRunner1 implements CommandLineRunner {

  @Override
  public void run(String... args) throws Exception {
    System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作<<<<<<<<<<<<<");
  }

}

Spring Boot應(yīng)用程序在啟動(dòng)后,會(huì)遍歷CommandLineRunner接口的實(shí)例并運(yùn)行它們的run方法。也可以利用@Order注解(或者實(shí)現(xiàn)Order接口)來規(guī)定所有CommandLineRunner實(shí)例的運(yùn)行順序。

如下我們使用@Order 注解來定義執(zhí)行順序。

package org.springboot.sample.runner;

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

/**
 * 服務(wù)啟動(dòng)執(zhí)行
 *
 * @author  單紅宇(365384722)
 * @create  2016年1月9日
 */
@Component
@Order(value=2)
public class MyStartupRunner1 implements CommandLineRunner {

  @Override
  public void run(String... args) throws Exception {
    System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 11111111 <<<<<<<<<<<<<");
  }

}

package org.springboot.sample.runner;

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

/**
 * 服務(wù)啟動(dòng)執(zhí)行
 *
 * @author  單紅宇(365384722)
 * @create  2016年1月9日
 */
@Component
@Order(value=1)
public class MyStartupRunner2 implements CommandLineRunner {

  @Override
  public void run(String... args) throws Exception {
    System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 22222222 <<<<<<<<<<<<<");
  }

}

啟動(dòng)程序后,控制臺(tái)輸出結(jié)果為:

>>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 22222222 <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 11111111 <<<<<<<<<<<<<

根據(jù)控制臺(tái)結(jié)果可判斷,@Order 注解的執(zhí)行優(yōu)先級(jí)是按value值從小到大順序。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

开平市| 平远县| 桂林市| 绥芬河市| 黄平县| 弥勒县| 阿勒泰市| 渭源县| 庄浪县| 石渠县| 宜州市| 阳新县| 绩溪县| 敖汉旗| 景泰县| 绵阳市| 中山市| 清丰县| 普陀区| 尉犁县| 新宁县| 吉林省| 靖远县| 中牟县| 固安县| 资源县| 左云县| 平利县| 田东县| 北辰区| 宜丰县| 富阳市| 凤冈县| 巴林左旗| 花垣县| 丰镇市| 信丰县| 永兴县| 禄丰县| 富蕴县| 佳木斯市|