方法一:@PostConstruct
此方法可能是最常用的
可以使用Spring Boot的@PostConstruct注解來實(shí)現(xiàn)在啟動時執(zhí)行一次的功能。@PostConstruct注解標(biāo)記的方法會在Bean初始化完成后自動調(diào)用,可以在該方法中執(zhí)行只需要在啟動時執(zhí)行一次的操作。
如果想在生成對象時完成某些初始化操作,而偏偏這些初始化操作又依賴于依賴注入,那么就無法在構(gòu)造函數(shù)中實(shí)現(xiàn)。為此,可以使用@PostConstruct注解一個方法來完成初始化,@PostConstruct注解的方法將會在依賴注入完成后被自動調(diào)用。
Constructor >> @Autowired >> @PostConstruct
例如,在一個Spring Boot應(yīng)用程序的啟動類中添加一個@PostConstruct注解標(biāo)記的方法:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class MyApplication {
@PostConstruct
public void init() {
// 在這里執(zhí)行僅需在啟動時執(zhí)行一次的操作
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
在以上示例代碼中,init()方法被標(biāo)記為@PostConstruct注解,表示它會在MyApplication Bean初始化完成后自動調(diào)用。在init()方法中可以執(zhí)行只需要在啟動時執(zhí)行一次的操作,例如初始化一些數(shù)據(jù)、建立數(shù)據(jù)庫連接等。
方法二:使用Spring Boot提供的CommandLineRunner接口或ApplicationRunner接口
此方法已經(jīng)在項(xiàng)目中實(shí)踐使用ok。
除了@PostConstruct注解,還可以使用Spring Boot提供的CommandLineRunner接口或ApplicationRunner接口來實(shí)現(xiàn)在啟動時執(zhí)行一次的功能。
這兩個接口都有一個run()方法,在應(yīng)用程序啟動后會被自動調(diào)用。需要在該方法中實(shí)現(xiàn)需要在啟動時執(zhí)行的操作,例如初始化數(shù)據(jù)、開啟定時任務(wù)等。
如果需要多個操作在啟動時執(zhí)行,可以定義多個實(shí)現(xiàn)了CommandLineRunner或ApplicationRunner接口的Bean,并通過@Order注解指定它們的執(zhí)行順序。
示例代碼如下:
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1) // 可以通過@Order注解指定執(zhí)行順序,數(shù)字越小越先執(zhí)行
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 在這里執(zhí)行啟動時需要執(zhí)行的操作
}
}
@Component
@Order(2)
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// 在這里執(zhí)行啟動時需要執(zhí)行的操作
}
}
以上示例代碼定義了兩個Bean,分別是實(shí)現(xiàn)CommandLineRunner接口的MyCommandLineRunner和實(shí)現(xiàn)ApplicationRunner接口的MyApplicationRunner。它們的run()方法會在應(yīng)用程序啟動后自動調(diào)用,可以在這里實(shí)現(xiàn)需要在啟動時執(zhí)行的操作。其中,@Order注解用于指定它們的執(zhí)行順序,數(shù)字越小越先執(zhí)行。
方法三:使用Spring Boot提供的ApplicationListener接口
此方法暫未實(shí)踐
還可以使用Spring Boot提供的ApplicationListener接口來實(shí)現(xiàn)在應(yīng)用程序啟動時執(zhí)行一次的功能。這個接口定義了監(jiān)聽Spring Boot應(yīng)用程序事件的方法,當(dāng)應(yīng)用程序觸發(fā)相應(yīng)的事件時,監(jiān)聽器會自動調(diào)用相應(yīng)的方法進(jìn)行處理。
具體實(shí)現(xiàn)步驟如下:
創(chuàng)建一個實(shí)現(xiàn)ApplicationListener接口的類,例如MyApplicationListener。
實(shí)現(xiàn)onApplicationEvent()方法,在該方法中編寫需要在啟動時執(zhí)行的操作,例如初始化數(shù)據(jù)、建立數(shù)據(jù)庫連接等。
通過@Component注解或@Bean注解將MyApplicationListener注冊成Spring Bean。
示例代碼如下:
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// 在這里執(zhí)行啟動時需要執(zhí)行的操作
}
}
以上示例代碼創(chuàng)建了一個名為MyApplicationListener的Bean,并實(shí)現(xiàn)了ApplicationListener接口,用于監(jiān)聽ApplicationReadyEvent事件。在onApplicationEvent()方法中編寫需要在啟動時執(zhí)行的操作。最后通過@Component注解將MyApplicationListener注冊成Spring Bean。文章來源:http://www.zghlxwxcb.cn/news/detail-460754.html
當(dāng)應(yīng)用程序啟動完成后,MyApplicationListener會自動監(jiān)聽到ApplicationReadyEvent事件并執(zhí)行其中的代碼??梢栽谶@里實(shí)現(xiàn)需要在啟動時執(zhí)行一次的操作,確保其只在應(yīng)用程序啟動時執(zhí)行一次。文章來源地址http://www.zghlxwxcb.cn/news/detail-460754.html
到了這里,關(guān)于Spring Boot實(shí)現(xiàn)在啟動時執(zhí)行一次的功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!