国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 12:31:22
字體:
來源:轉載
供稿:網(wǎng)友

實際應用中,我們會有在項目服務啟動的時候就去加載一些數(shù)據(jù)或做一些事情這樣的需求。

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

很簡單,只需要一個類就可以,無需其他配置。

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

package org.springboot.sample.runner;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;/** * 服務啟動執(zhí)行 * * @author  單紅宇(365384722) * @create  2016年1月9日 */@Componentpublic class MyStartupRunner1 implements CommandLineRunner {  @Override  public void run(String... args) throws Exception {    System.out.println(">>>>>>>>>>>>>>>服務啟動執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作<<<<<<<<<<<<<");  }}

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

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

package org.springboot.sample.runner;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/** * 服務啟動執(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(">>>>>>>>>>>>>>>服務啟動執(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;/** * 服務啟動執(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(">>>>>>>>>>>>>>>服務啟動執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 22222222 <<<<<<<<<<<<<");  }}

啟動程序后,控制臺輸出結果為:

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 崇信县| 常山县| 泾源县| 上虞市| 新源县| 综艺| 称多县| 化德县| 长阳| 天峻县| 阿克| 辽阳县| 淮北市| 屯昌县| 泸西县| 揭东县| 洛阳市| 潍坊市| 卢龙县| 祁阳县| 磐安县| 新津县| 海淀区| 蒲江县| 房山区| 临汾市| 略阳县| 瑞金市| 泰顺县| 广丰县| 鄄城县| 安化县| 永康市| 阿拉善左旗| 宜君县| 五莲县| 灵武市| 扎囊县| 玉门市| 德庆县| 康定县|