Spring Boot應(yīng)用啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的五種方式(常見(jiàn)方法)
Spring Boot為開(kāi)發(fā)者提供了多種方式在應(yīng)用啟動(dòng)時(shí)執(zhí)行自定義代碼,這些方式包括注解、接口實(shí)現(xiàn)和事件監(jiān)聽(tīng)器。在本篇博客中,我們將探討一些常見(jiàn)的方法,以及如何利用它們?cè)趹?yīng)用啟動(dòng)時(shí)執(zhí)行初始化邏輯。

1. @PostConstruct注解
`@PostConstruct`注解可以標(biāo)注在方法上,該方法將在類(lèi)被初始化后調(diào)用。在Spring Boot應(yīng)用中,你可以使用這個(gè)注解來(lái)執(zhí)行一些初始化的邏輯。
@PostConstruct
public void doSomething(){
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("do something");
}2. ApplicationListener接口
實(shí)現(xiàn)`ApplicationListener`接口并監(jiān)聽(tīng)`ApplicationStartedEvent`事件,這樣你的邏輯將在應(yīng)用啟動(dòng)后被觸發(fā)。
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
public class MyApplicationListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("ApplicationListener executed");
}
}3. @EventListener注解
使用`@EventListener`注解,可以將方法標(biāo)記為事件監(jiān)聽(tīng)器,并在特定事件發(fā)生時(shí)執(zhí)行。
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
public class MyEventListener {
@EventListener(ApplicationStartedEvent.class)
public void onApplicationEvent() {
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("@EventListener executed");
}
}4. ApplicationRunner接口
實(shí)現(xiàn)`ApplicationRunner`接口,該接口的`run`方法會(huì)在Spring Boot應(yīng)用啟動(dòng)后執(zhí)行。
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("ApplicationRunner executed");
}
}5. CommandLineRunner接口
與`ApplicationRunner`類(lèi)似,`CommandLineRunner`接口的`run`方法也在應(yīng)用啟動(dòng)后執(zhí)行。
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("CommandLineRunner executed");
}
}Demo代碼
完整如下
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class Application implements
ApplicationListener<ApplicationStartedEvent>,
CommandLineRunner,
ApplicationRunner
{
/**
* 本次執(zhí)行先后順序?yàn)椋](méi)有設(shè)置order)
* PostConstruct、ApplicationListener、@EventListener注解、ApplicationRunner、CommandLineRunner
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@PostConstruct
public void doSomething(){
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("do something 11111111111");
System.out.println("PostConstruct注解啟動(dòng)");
System.out.println("===============");
}
@EventListener(ApplicationStartedEvent.class)
public void onApplicationEvent() {
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("do something 22222222222");
System.out.println("@EventListener 注解啟動(dòng) executed");
System.out.println("===============");
}
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("do something 3333333333");
System.out.println("ApplicationListener executed");
System.out.println("===============");
}
@Override
public void run(String... args) throws Exception {
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("do something 44444444");
System.out.println("CommandLineRunner啟動(dòng)");
System.out.println("===============");
}
@Override
public void run(ApplicationArguments args) throws Exception {
// 在應(yīng)用啟動(dòng)后執(zhí)行的代碼
System.out.println("do something 55555555");
System.out.println("ApplicationRunner啟動(dòng)");
System.out.println("===============");
}
}Demo分析
@PostConstruct注解方法 (doSomething方法) 在類(lèi)初始化后被調(diào)用,因此會(huì)首先輸出。
ApplicationListener接口方法 (onApplicationEvent方法) 在應(yīng)用啟動(dòng)后執(zhí)行,會(huì)輸出其相關(guān)的信息。
@EventListener注解方法 (onApplicationEvent方法) 同樣在應(yīng)用啟動(dòng)后執(zhí)行,會(huì)輸出其相關(guān)的信息。
ApplicationRunner接口方法 (run方法) 在ApplicationListener之后執(zhí)行,它用于在Spring Boot應(yīng)用啟動(dòng)后執(zhí)行一些額外的邏輯。
CommandLineRunner接口方法 (run方法) 也在ApplicationListener之后執(zhí)行,用于在Spring Boot應(yīng)用啟動(dòng)后執(zhí)行一些額外的邏輯。
總結(jié)
通過(guò)以上幾種方式,你可以根據(jù)項(xiàng)目的需求選擇合適的初始化方法。無(wú)論是使用注解、接口實(shí)現(xiàn),還是事件監(jiān)聽(tīng)器,Spring Boot提供了靈活的機(jī)制來(lái)管理應(yīng)用啟動(dòng)時(shí)的自定義邏輯,使得開(kāi)發(fā)者能夠更方便地控制應(yīng)用的初始化過(guò)程。在實(shí)際項(xiàng)目中,通常根據(jù)具體場(chǎng)景選擇其中一種或多種方式,以滿足不同的需求。
到此這篇關(guān)于Spring Boot應(yīng)用啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的五種方式(常見(jiàn)方法)的文章就介紹到這了,更多相關(guān)Spring Boot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式
- SpringBoot項(xiàng)目執(zhí)行腳本 自動(dòng)拉取最新代碼并重啟的實(shí)例內(nèi)容
- springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟
- Spring Boot 項(xiàng)目啟動(dòng)自動(dòng)執(zhí)行方法的兩種實(shí)現(xiàn)方式
- springBoot啟動(dòng)時(shí)讓方法自動(dòng)執(zhí)行的幾種實(shí)現(xiàn)方式
相關(guān)文章
Ubuntu 15下安裝Eclipse經(jīng)驗(yàn)分享
這篇文章主要為大家分享了Ubuntu 15下安裝Eclipse經(jīng)驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
java servlet手機(jī)app訪問(wèn)接口(二)短信驗(yàn)證
這篇文章主要介紹了java servlet手機(jī)app訪問(wèn)接口(二),短信驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
java進(jìn)行error捕獲和處理示例(java異常捕獲)
通常來(lái)說(shuō),大家都是對(duì)Java中的Exception進(jìn)行捕獲和進(jìn)行相應(yīng)的處理,有些人說(shuō),error就無(wú)法捕獲了。其實(shí),error也是可以捕獲的。Error和Exception都是Throwable的子類(lèi)。既然可以catch Throwable,那么error也是可以catch的2014-01-01
java通過(guò)控制鼠標(biāo)實(shí)現(xiàn)屏幕廣播的方法
這篇文章主要介紹了java通過(guò)控制鼠標(biāo)實(shí)現(xiàn)屏幕廣播的方法,針對(duì)前面一篇Java屏幕共享功能進(jìn)行了改進(jìn),實(shí)現(xiàn)了鼠標(biāo)控制功能,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
Spring如何使用三級(jí)緩存解決循環(huán)依賴
在Spring框架中,循環(huán)依賴是指兩個(gè)或多個(gè)Bean相互依賴,形成閉環(huán),導(dǎo)致無(wú)法完成初始化,此問(wèn)題僅存在于單例Bean中,而原型Bean會(huì)拋出異常,Spring通過(guò)三級(jí)緩存及提前暴露策略解決循環(huán)依賴:一級(jí)緩存存放完全初始化的Bean2024-11-11
FactoryBean?BeanFactory方法使用示例詳解講解
這篇文章主要為大家介紹了FactoryBean?BeanFactory方法使用示例詳解講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

