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

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

 更新時間:2018年06月13日 14:51:54   作者:月未明  
這篇文章主要介紹了詳解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 :

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 :

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接口:

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)方式:

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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring框架十一種常見異常的解決方法匯總

    Spring框架十一種常見異常的解決方法匯總

    今天小編就為大家分享一篇關(guān)于Spring框架十一種常見異常的解決方法匯總,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Java 中的 @SneakyThrows 注解的使用方法(簡化異常處理的利與弊)

    Java 中的 @SneakyThrows 注解的使用方法(簡化異常處理的利與弊)

    @SneakyThrows是Lombok提供的一個注解,用于簡化Java方法中的異常處理,特別是對于檢查型異常,它允許方法拋出異常而不必顯式聲明或捕獲這些異常,本文介紹Java 中的 @SneakyThrows 注解的使用方法,感興趣的朋友一起看看吧
    2025-03-03
  • java獲取系統(tǒng)路徑字體、得到某個目錄下的所有文件名、獲取當(dāng)前路徑

    java獲取系統(tǒng)路徑字體、得到某個目錄下的所有文件名、獲取當(dāng)前路徑

    這篇文章主要介紹了java獲取系統(tǒng)路徑字體、得到某個目錄下的所有文件名、獲取當(dāng)前路徑,需要的朋友可以參考下
    2014-04-04
  • Spring Security登錄接口兼容JSON格式登錄實現(xiàn)示例

    Spring Security登錄接口兼容JSON格式登錄實現(xiàn)示例

    前后端分離中,前端和后端的數(shù)據(jù)交互通常是JSON格式,本文主要介紹了Spring Security登錄接口兼容JSON格式登錄實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • 一文詳解Java如何創(chuàng)建和銷毀對象

    一文詳解Java如何創(chuàng)建和銷毀對象

    Java由Sun Microsystems發(fā)明并在1995年發(fā)布,是世界上使用最廣泛的編程語言之一。本文主要和大家介紹一下Java是如何創(chuàng)建和銷毀對象的,希望對大家有所幫助
    2022-11-11
  • java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)教程

    java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)教程

    最近在java連接SQL數(shù)據(jù)庫時會出現(xiàn)一些問題,所以這篇文章主要給大家介紹了關(guān)于java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Springboot框架實現(xiàn)自動裝配詳解

    Springboot框架實現(xiàn)自動裝配詳解

    在使用springboot時,很多配置我們都沒有做,都是springboot在幫我們完成,這很大一部分歸功于springboot自動裝配。本文將詳細(xì)為大家講解SpringBoot的自動裝配原理,需要的可以參考一下
    2022-08-08
  • 通過AOP環(huán)繞通知如何實現(xiàn)事務(wù)控制

    通過AOP環(huán)繞通知如何實現(xiàn)事務(wù)控制

    這篇文章主要介紹了通過AOP環(huán)繞通知如何實現(xiàn)事務(wù)控制的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 最新評論

    武冈市| 株洲县| 历史| 手游| 且末县| 浦城县| 梓潼县| 清河县| 微山县| 长垣县| 靖西县| 香河县| 城市| 鞍山市| 民和| 太和县| 章丘市| 肥城市| 呼和浩特市| 绥德县| 霍林郭勒市| 光泽县| 拜泉县| 兴海县| 开远市| 濉溪县| 中卫市| 修文县| 黔西| 嵩明县| 封开县| 福安市| 鞍山市| 三原县| 响水县| 留坝县| 乾安县| 嘉鱼县| 裕民县| 德化县| 泸溪县|