當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringBoot中实现CommandLineRunner接口在项目启动后立即执行某方法
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot中实现CommandLineRunner接口在项目启动后立即执行某方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
場(chǎng)景
在啟動(dòng)SpringBoot項(xiàng)目的啟動(dòng)類之后需要其立即執(zhí)行某方法。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。
實(shí)現(xiàn)
在項(xiàng)目下新建類,使這個(gè)類實(shí)現(xiàn)CommandLineRunner接口,此接口是springboot自帶的,接口定義如下
package org.springframework.boot;@FunctionalInterface public interface CommandLineRunner {void run(String... args) throws Exception; }實(shí)現(xiàn)接口后需要重新run方法,在run方法中執(zhí)行需要接著執(zhí)行的邏輯
package com.ruoyi.web.imserver.config;import com.ruoyi.web.imserver.ServerLauncherImpl; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;/*** <p> 啟動(dòng)MobileIMSDK服務(wù)端 </p>** @author :* @description : run方法在SpringBoot服務(wù)啟動(dòng)之后會(huì)自動(dòng)被調(diào)用* @date :*/@Component @Order(value = 1) public class ChatServerRunner implements CommandLineRunner {@Overridepublic void run(String... strings) throws Exception {System.out.println("公眾號(hào):霸道的程序猿");}}啟動(dòng)SpringBoot的啟動(dòng)類,查看效果
?
這里的Order注解的value代表啟動(dòng)的順序,value值越小,順訊越在前。
總結(jié)
以上是生活随笔為你收集整理的SpringBoot中实现CommandLineRunner接口在项目启动后立即执行某方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 若依(基于SpringBoot的权限管理
- 下一篇: Android中ListView的使用以