詳解Spring Boot 項目啟動時執(zhí)行特定方法
Springboot給我們提供了兩種“開機啟動”某些方法的方式:ApplicationRunner和CommandLineRunner。
這兩種方法提供的目的是為了滿足,在項目啟動的時候立刻執(zhí)行某些方法。我們可以通過實現(xiàn)ApplicationRunner和CommandLineRunner,來實現(xiàn),他們都是在SpringApplication 執(zhí)行之后開始執(zhí)行的。
CommandLineRunner接口可以用來接收字符串數(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring boot 連接多數(shù)據(jù)源過程詳解
這篇文章主要介紹了Spring boot 連接多數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08SpringBoot中如何對actuator進行關(guān)閉
這篇文章主要介紹了SpringBoot中如何對actuator進行關(guān)閉問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用
這篇文章主要介紹了如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下。2021-01-01實戰(zhàn)干貨之基于SpringBoot的RabbitMQ多種模式隊列
RabbitMQ 是一個由Erlang語言開發(fā)的AMQP的開源實現(xiàn),支持多種客戶端。用于在分布式系統(tǒng)中存儲轉(zhuǎn)發(fā)消息,在易用性、擴展性、高可用性等方面表現(xiàn)不俗,下文將帶你深入了解 RabbitMQ 多種模式隊列2021-09-09@Transactional注解異常報錯之多數(shù)據(jù)源詳解
這篇文章主要介紹了@Transactional注解異常報錯之多數(shù)據(jù)源詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01