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

1. @PostConstruct注解
`@PostConstruct`注解可以標(biāo)注在方法上,該方法將在類被初始化后調(diào)用。在Spring Boot應(yīng)用中,你可以使用這個注解來執(zhí)行一些初始化的邏輯。
@PostConstruct
public void doSomething(){
// 在應(yīng)用啟動后執(zhí)行的代碼
System.out.println("do something");
}2. ApplicationListener接口
實現(xiàn)`ApplicationListener`接口并監(jiān)聽`ApplicationStartedEvent`事件,這樣你的邏輯將在應(yī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)用啟動后執(zhí)行的代碼
System.out.println("ApplicationListener executed");
}
}3. @EventListener注解
使用`@EventListener`注解,可以將方法標(biāo)記為事件監(jiān)聽器,并在特定事件發(fā)生時執(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)用啟動后執(zhí)行的代碼
System.out.println("@EventListener executed");
}
}4. ApplicationRunner接口
實現(xiàn)`ApplicationRunner`接口,該接口的`run`方法會在Spring Boot應(yī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)用啟動后執(zhí)行的代碼
System.out.println("ApplicationRunner executed");
}
}5. CommandLineRunner接口
與`ApplicationRunner`類似,`CommandLineRunner`接口的`run`方法也在應(yīng)用啟動后執(zhí)行。
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 在應(yī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í)行先后順序為(沒有設(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)用啟動后執(zhí)行的代碼
System.out.println("do something 11111111111");
System.out.println("PostConstruct注解啟動");
System.out.println("===============");
}
@EventListener(ApplicationStartedEvent.class)
public void onApplicationEvent() {
// 在應(yīng)用啟動后執(zhí)行的代碼
System.out.println("do something 22222222222");
System.out.println("@EventListener 注解啟動 executed");
System.out.println("===============");
}
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
// 在應(yī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)用啟動后執(zhí)行的代碼
System.out.println("do something 44444444");
System.out.println("CommandLineRunner啟動");
System.out.println("===============");
}
@Override
public void run(ApplicationArguments args) throws Exception {
// 在應(yīng)用啟動后執(zhí)行的代碼
System.out.println("do something 55555555");
System.out.println("ApplicationRunner啟動");
System.out.println("===============");
}
}Demo分析
@PostConstruct注解方法 (doSomething方法) 在類初始化后被調(diào)用,因此會首先輸出。
ApplicationListener接口方法 (onApplicationEvent方法) 在應(yīng)用啟動后執(zhí)行,會輸出其相關(guān)的信息。
@EventListener注解方法 (onApplicationEvent方法) 同樣在應(yīng)用啟動后執(zhí)行,會輸出其相關(guān)的信息。
ApplicationRunner接口方法 (run方法) 在ApplicationListener之后執(zhí)行,它用于在Spring Boot應(yīng)用啟動后執(zhí)行一些額外的邏輯。
CommandLineRunner接口方法 (run方法) 也在ApplicationListener之后執(zhí)行,用于在Spring Boot應(yīng)用啟動后執(zhí)行一些額外的邏輯。
總結(jié)
通過以上幾種方式,你可以根據(jù)項目的需求選擇合適的初始化方法。無論是使用注解、接口實現(xiàn),還是事件監(jiān)聽器,Spring Boot提供了靈活的機(jī)制來管理應(yīng)用啟動時的自定義邏輯,使得開發(fā)者能夠更方便地控制應(yīng)用的初始化過程。在實際項目中,通常根據(jù)具體場景選擇其中一種或多種方式,以滿足不同的需求。
到此這篇關(guān)于Spring Boot應(yīng)用啟動時自動執(zhí)行代碼的五種方式(常見方法)的文章就介紹到這了,更多相關(guān)Spring Boot啟動時自動執(zhí)行代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java servlet手機(jī)app訪問接口(二)短信驗證
這篇文章主要介紹了java servlet手機(jī)app訪問接口(二),短信驗證,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
java進(jìn)行error捕獲和處理示例(java異常捕獲)
通常來說,大家都是對Java中的Exception進(jìn)行捕獲和進(jìn)行相應(yīng)的處理,有些人說,error就無法捕獲了。其實,error也是可以捕獲的。Error和Exception都是Throwable的子類。既然可以catch Throwable,那么error也是可以catch的2014-01-01
java通過控制鼠標(biāo)實現(xiàn)屏幕廣播的方法
這篇文章主要介紹了java通過控制鼠標(biāo)實現(xiàn)屏幕廣播的方法,針對前面一篇Java屏幕共享功能進(jìn)行了改進(jìn),實現(xiàn)了鼠標(biāo)控制功能,具有一定的實用價值,需要的朋友可以參考下2014-12-12
FactoryBean?BeanFactory方法使用示例詳解講解
這篇文章主要為大家介紹了FactoryBean?BeanFactory方法使用示例詳解講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

