當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot 启动载入数据 CommandLineRunner
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot 启动载入数据 CommandLineRunner
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Spring Boot 啟動載入數(shù)據(jù) CommandLineRunner
實際應(yīng)用中,我們會有在項目服務(wù)啟動的時候就去載入一些數(shù)據(jù)或做一些事情這種需求。
為了解決這種問題。Spring Boot 為我們提供了一個方法。通過實現(xiàn)接口 CommandLineRunner 來實現(xiàn)。
非常easy。僅僅須要一個類就能夠,無需其它配置。
創(chuàng)建實現(xiàn)接口 CommandLineRunner 的類
Spring Boot應(yīng)用程序在啟動后,會遍歷CommandLineRunner接口的實例并運行它們的run方法。也能夠利用@Order注解(或者實現(xiàn)Order接口)來規(guī)定全部CommandLineRunner實例的運行順序。
例如以下我們使用@Order 注解來定義運行順序。
package org.springboot.sample.runner;import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;/*** 服務(wù)啟動運行** @author 單紅宇(365384722)* @myblog http://blog.csdn.net/catoop/* @create 2016年1月9日*/ @Component @Order(value=2) public class MyStartupRunner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動運行。運行載入數(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ù)啟動運行** @author 單紅宇(365384722)* @myblog http://blog.csdn.net/catoop/* @create 2016年1月9日*/ @Component @Order(value=1) public class MyStartupRunner2 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動運行,運行載入數(shù)據(jù)等操作 22222222 <<<<<<<<<<<<<");}}啟動程序后。控制臺輸出結(jié)果為:
>>>>>>>>>>>>>>>服務(wù)啟動運行,運行載入數(shù)據(jù)等操作 22222222 <<<<<<<<<<<<< >>>>>>>>>>>>>>>服務(wù)啟動運行。運行載入數(shù)據(jù)等操作 11111111 <<<<<<<<<<<<<依據(jù)控制臺結(jié)果可推斷。@Order 注解的運行優(yōu)先級是按value值從小到大順序。
posted on 2017-08-14 14:19 mthoutai 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/mthoutai/p/7357581.html
總結(jié)
以上是生活随笔為你收集整理的Spring Boot 启动载入数据 CommandLineRunner的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML框架标签的使用-lt;frame
- 下一篇: ASP.NET MVC easyUI-d