SpringBoot啟動時執行特定的任務

2021-09-24 12:55:36 字數 646 閱讀 2362

springboot啟動時,執行任務commandlinerunner

在開發過程中,可能需要實現專案啟動之後執行功能,springboot提供的一種方案 就是用乙個bean或者model實現commandlinerunner介面,將實現功能的**放在run()方法中.

@componentscan("cn.sdut.backend")

@autowired

private serviceserver server;

public static void main(string args)

public void run(string args)

}// 或者

@component

public class mystartuprunner implements commandlinerunner }

如果有多個實現類實現commandlinerunner介面,則需要通過@order註解來進行排序。

@component

@order(value=2) // value值越小,執行的優先順序越高

public class mystartuprunner1 implements commandlinerunner

}

springBoot在啟動時執行任務如何實現?

在專案開發中會經常用到,在專案啟動時候執行某段特定的 這時我們該如何實現呢?在springboot中提供了中簡單方法 實現commandlinerunner介面,並重寫run 方法,並加上 component 將該類加入ioc容器中 如果同時有多個類實現了commandlinerunner介面可以通...

專案啟動時執行特定方法

commandlinerunner import org.springframework.boot.commandlinerunner import org.springframework.stereotype.component component public class mycommandli...

mysql啟動時執行過程 mysql啟動過程

明白 etc init.d mysql從 來,和mysql.server什麼關係?etc init.d mysql和mysqld safe之間的關係 mysqld safe怎麼把mysql啟動起來的 mysql就是mysql.server拷貝到這裡來的。mysql啟動,呼叫mysqld safe m...