一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解Spring Boot 項目啟動時執(zhí)行特定方法

詳解Spring Boot 項目啟動時執(zhí)行特定方法

2021-05-09 12:44月未明 Java教程

這篇文章主要介紹了詳解Spring Boot 項目啟動時執(zhí)行特定方法,Springboot給我們提供了兩種“開機(jī)啟動”某些方法的方式:ApplicationRunner和CommandLineRunner。感興趣的小伙伴們可以參考一下

springboot給我們提供了兩種“開機(jī)啟動”某些方法的方式:applicationrunner和commandlinerunner。

這兩種方法提供的目的是為了滿足,在項目啟動的時候立刻執(zhí)行某些方法。我們可以通過實現(xiàn)applicationrunner和commandlinerunner,來實現(xiàn),他們都是在springapplication 執(zhí)行之后開始執(zhí)行的。

commandlinerunner接口可以用來接收字符串?dāng)?shù)組的命令行參數(shù),applicationrunner 是使用applicationarguments 用來接收參數(shù)的,貌似后者更牛逼一些。

先看看commandlinerunner :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.springboot.study;
 
import org.springframework.boot.commandlinerunner;
import org.springframework.stereotype.component;
 
/**
 * created by pangkunkun on 2017/9/3.
 */
@component
public class mycommandlinerunner implements commandlinerunner{
 
  @override
  public void run(string... var1) throws exception{
    system.out.println("this will be execute when the project was started!");
  }
}

applicationrunner :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.springboot.study;
 
import org.springframework.boot.applicationarguments;
import org.springframework.boot.applicationrunner;
import org.springframework.stereotype.component;
 
/**
 * created by pangkunkun on 2017/9/3.
 */
@component
public class myapplicationrunner implements applicationrunner {
 
  @override
  public void run(applicationarguments var1) throws exception{
    system.out.println("myapplicationrunner class will be execute when the project was started!");
  }
 
}

這兩種方式的實現(xiàn)都很簡單,直接實現(xiàn)了相應(yīng)的接口就可以了。記得在類上加@component注解。

如果想要指定啟動方法執(zhí)行的順序,可以通過實現(xiàn)org.springframework.core.ordered接口或者使用org.springframework.core.annotation.order注解來實現(xiàn)。

這里我們以applicationrunner 為例來分別實現(xiàn)。

ordered接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.springboot.study;
 
import org.springframework.boot.applicationarguments;
import org.springframework.boot.applicationrunner;
import org.springframework.core.ordered;
import org.springframework.stereotype.component;
 
/**
 * created by pangkunkun on 2017/9/3.
 */
@component
public class myapplicationrunner implements applicationrunner,ordered{
 
 
  @override
  public int getorder(){
    return 1;//通過設(shè)置這里的數(shù)字來知道指定順序
  }
 
  @override
  public void run(applicationarguments var1) throws exception{
    system.out.println("myapplicationrunner1!");
  }
 
}

order注解實現(xiàn)方式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.springboot.study;
 
import org.springframework.boot.applicationarguments;
import org.springframework.boot.applicationrunner;
import org.springframework.core.ordered;
import org.springframework.core.annotation.order;
import org.springframework.stereotype.component;
 
/**
 * created by pangkunkun on 2017/9/3.
 * 這里通過設(shè)定value的值來指定執(zhí)行順序
 */
@component
@order(value = 1)
public class myapplicationrunner implements applicationrunner{
 
  @override
  public void run(applicationarguments var1) throws exception{
    system.out.println("myapplicationrunner1!");
  }
 
}

這里不列出其他對比方法了,自己執(zhí)行下就好。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/qq_35981283/article/details/77826537

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美男男gaysgays | 亚洲激情自拍偷拍 | 亚洲精品中文字幕在线 | 亚洲男人天堂影院 | 亚洲国产精品综合福利专区 | 阿 好深 快点 老师受不了 | 波多野结衣xxxx性精品 | 91在线免费播放 | 国产日韩一区二区三区 | waswaswas免费| 色伦网 | 91亚洲视频在线观看 | 91国内在线国内在线播放 | 校园高h| 99久久6er热免费精品 | 日本三级在线观看免费 | 日本人泡妞18xxⅹ | 日本艳鉧动漫1~6完整版在 | 亚洲欧美日韩天堂 | 欧美一二 | kkkk4444在线看片 | 男女男精品视频网站 | 波多野结衣两女调教 | 亚洲黄色成人 | 娇妻被朋友征服中文字幕 | 双性太子 | 希望影院高清免费观看视频 | 亚洲同性男男gay1069 | 亚洲小视频 | 国产日产在线观看 | 色综合视频在线观看 | 久久99r66热这里只有精品 | 日韩欧美a | 亚洲国产成人久久99精品 | 免费看国产一级片 | 国产福利片在线 易阳 | 色综合视频在线观看 | 好大好硬好深好爽想要之黄蓉 | 色天使亚洲综合在线观看 | 91在线老师啪国自产 | 日本片免费观看一区二区 |