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