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

服務(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 項(xiàng)目啟動時執(zhí)行特定方法

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

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

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

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

這兩種方法提供的目的是為了滿足,在項(xiàng)目啟動的時候立刻執(zhí)行某些方法。我們可以通過實(shí)現(xiàn)applicationrunner和commandlinerunner,來實(shí)現(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!");
  }
 
}

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

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

這里我們以applicationrunner 為例來分別實(shí)現(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注解實(shí)現(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品日韩欧美一区二区 | 王晶三级作品 | 久久91精品国产91久久户 | 国产自在线观看 | 18国产精品白浆在线观看免费 | 亚洲黄色色图 | 热99re久久精品国产首页 | 香蕉国产精品偷在线播放 | 日本高清全集免费观看 | 好大好深受不了了快进来 | www.4虎影院| 日本免费在线观看 | 亚欧洲乱码视频一二三区 | 超级碰碰青草免费视频92 | 亚洲 欧美 在线观看 | 欧美国产日产精品免费视频 | 摔跤成人黄版 | 亚洲欧美另类第一页 | 日本b站一卡二不卡三卡四卡 | 精品免费tv久久久久久久 | 免费在线观看中文字幕 | 99视频在线观看免费视频 | 女同学用白丝脚玩我的故事 | 隔壁老王国产在线精品 | 91热国内精品永久免费观看 | 99草精品视频 | 亚洲国产99999在线精品一区 | 天天操天天做 | 亚洲精品国产精麻豆久久99 | 91午夜视频 | 把老师操了 | 99精品视频在线观看 | 18hdxxxx中国 | 午夜勾魂曲 | 欧美一区二区视频 | 精品国产成a人在线观看 | 欧美日韩亚洲区久久综合 | 性欧美xxxxx护士另类 | 五月丁香啪啪. | 四虎1515hhh co m | 欧美高清免费一级在线 |