有時候我們需要在應用啟動完成後執行一些特定的操作,比如:
刪除一些臨時檔案或者redis中的快取看如下例項:將一些字典類的資料載入到快取,這樣就不用每次去資料庫中查了,有些關聯資料從快取中取得賦值就可以了,不再需要聯接查詢
啟動乙個計時器
taskrunner類:
private static final logger logger = logge***ctory.getlogger(taskrunner.class);
@override
public int getorder() {
return 2;
@override
logger.info("task runner");
startuprunner類:
@order(1)public static void main(string args) {public class startuprunner implements commandlinerunner {
private static final logger logger = logge***ctory.getlogger(startuprunner.class);
@autowired
@qualifier("primarydatasource")
private datasource primarydatasource;
@autowired
@qualifier("secondarydatasource")
private datasource seconddatasource;
@override
public void run(string... args) throws exception {
logger.info("startup runner");
system.out.print(primarydatasource.getconnection().getmetadata().geturl());
system.out.print(seconddatasource.getconnection().getmetadata().geturl());
@bean
public startuprunner startuprunner(){
return new startuprunner();
@bean
public taskrunner taskrunner(){
return new taskrunner();
談談spring boot 啟動層面的開發
springboot啟動後執行某些命令
多個類實現介面可以使用 order註解來定義執行順序 專案啟動完成了,因此可以注入spring管理的類 例項 component order 1 autowired private stringredistemplate stringredistemplate override stringredi...
spring boot, 容器啟動後執行某操作
常有在spring容器啟動後執行某些操作的需求,現做了乙個demo的實現,做一下記錄,也希望可以給需要的同學提供參考。1.spring啟動後,以新執行緒執行後續需要的操作,所以執行類實現runnable介面 component public class stepexecutor implements...
spring boot, 容器啟動後執行某操作
常有在spring容器啟動後執行某些操作的需求,現做了乙個demo的實現,做一下記錄,也希望可以給需要的同學提供參考。1.spring啟動後,以新執行緒執行後續需要的操作,所以執行類實現runnable介面 component public class stepexecutor implements...