SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行特定代碼的完整指南
一、應(yīng)用生命周期回調(diào)方式
1. CommandLineRunner 接口
@Component @Order(1) // 可選,定義執(zhí)行順序 public class DatabaseInitializer implements CommandLineRunner { private final UserRepository userRepository; public DatabaseInitializer(UserRepository userRepository) { this.userRepository = userRepository; } @Override public void run(String... args) throws Exception { // 初始化數(shù)據(jù)庫數(shù)據(jù) userRepository.save(new User("admin", "admin@example.com")); System.out.println("數(shù)據(jù)庫初始化完成"); } }
特點(diǎn):
- 在所有ApplicationReadyEvent之前執(zhí)行
- 可以訪問命令行參數(shù)
- 支持多實(shí)例,通過@Order控制順序
2. ApplicationRunner 接口
@Component @Order(2) public class CacheWarmup implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // 更豐富的參數(shù)訪問方式 System.out.println("源參數(shù): " + args.getSourceArgs()); System.out.println("選項(xiàng)參數(shù): " + args.getOptionNames()); // 預(yù)熱緩存邏輯 System.out.println("緩存預(yù)熱完成"); } }
與CommandLineRunner區(qū)別:
- 提供更結(jié)構(gòu)化的參數(shù)訪問(ApplicationArguments)
- 同樣支持@Order排序
二、Spring事件監(jiān)聽方式
1. 監(jiān)聽特定生命周期事件
@Component public class StartupEventListener { // 在環(huán)境準(zhǔn)備完成后執(zhí)行 @EventListener(ApplicationEnvironmentPreparedEvent.class) public void handleEnvPrepared(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment env = event.getEnvironment(); System.out.println("當(dāng)前環(huán)境: " + env.getActiveProfiles()); } // 在應(yīng)用上下文準(zhǔn)備好后執(zhí)行 @EventListener(ApplicationContextInitializedEvent.class) public void handleContextInit(ApplicationContextInitializedEvent event) { System.out.println("應(yīng)用上下文初始化完成"); } // 在所有Bean加載完成后執(zhí)行 @EventListener(ContextRefreshedEvent.class) public void handleContextRefresh(ContextRefreshedEvent event) { System.out.println("所有Bean已加載"); } // 在應(yīng)用完全啟動(dòng)后執(zhí)行(推薦) @EventListener(ApplicationReadyEvent.class) public void handleAppReady(ApplicationReadyEvent event) { System.out.println("應(yīng)用已完全啟動(dòng),可以開始處理請(qǐng)求"); } }
2. 事件執(zhí)行順序
三、Bean生命周期回調(diào)
1. @PostConstruct 注解
@Service public class SystemValidator { @Autowired private HealthCheckService healthCheckService; @PostConstruct public void validateSystem() { if (!healthCheckService.isDatabaseConnected()) { throw new IllegalStateException("數(shù)據(jù)庫連接失敗"); } System.out.println("系統(tǒng)驗(yàn)證通過"); } }
特點(diǎn):
- 在Bean依賴注入完成后立即執(zhí)行
- 適用于單個(gè)Bean的初始化
- 拋出異常會(huì)阻止應(yīng)用啟動(dòng)
2. InitializingBean 接口
@Component public class NetworkChecker implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("網(wǎng)絡(luò)連接檢查完成"); // 執(zhí)行網(wǎng)絡(luò)檢查邏輯 } }
與@PostConstruct比較:
- 功能類似,但屬于Spring接口而非JSR-250標(biāo)準(zhǔn)
- 執(zhí)行時(shí)機(jī)稍晚于@PostConstruct
四、Spring Boot特性擴(kuò)展
1. ApplicationContextInitializer
public class CustomInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override public void initialize(ConfigurableApplicationContext applicationContext) { // 在上下文刷新前執(zhí)行 System.out.println("應(yīng)用上下文初始化器執(zhí)行"); // 可以修改環(huán)境配置 applicationContext.getEnvironment().setActiveProfiles("dev"); } }
注冊(cè)方式:
1.在META-INF/spring.factories中添加:
org.springframework.context.ApplicationContextInitializer=com.example.CustomInitializer
2.或通過SpringApplication添加:
@SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication app = new SpringApplication(MyApp.class); app.addInitializers(new CustomInitializer()); app.run(args); } }
2. SpringApplicationRunListener
public class StartupMonitor implements SpringApplicationRunListener { public StartupMonitor(SpringApplication app, String[] args) {} @Override public void starting(ConfigurableBootstrapContext bootstrapContext) { System.out.println("應(yīng)用開始啟動(dòng)"); } @Override public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) { System.out.println("環(huán)境準(zhǔn)備完成"); } // 其他生命周期方法... }
注冊(cè)方式:
在META-INF/spring.factories中:
org.springframework.boot.SpringApplicationRunListener=com.example.StartupMonitor
五、條件化初始化
1. 基于Profile的初始化
@Profile("dev") @Component public class DevDataLoader implements CommandLineRunner { @Override public void run(String... args) { System.out.println("加載開發(fā)環(huán)境測(cè)試數(shù)據(jù)"); } }
2. 基于條件的Bean創(chuàng)建
@Configuration public class ConditionalInitConfig { @Bean @ConditionalOnProperty(name = "app.init-sample-data", havingValue = "true") public CommandLineRunner sampleDataLoader() { return args -> System.out.println("加載示例數(shù)據(jù)"); } }
六、初始化方法對(duì)比
方法 | 執(zhí)行時(shí)機(jī) | 適用場(chǎng)景 | 順序控制 | 訪問Spring上下文 |
---|---|---|---|---|
ApplicationContextInitializer | 最早階段 | 環(huán)境準(zhǔn)備 | 無 | 有限訪問 |
@PostConstruct | Bean初始化 | 單個(gè)Bean初始化 | 無 | 完全訪問 |
ApplicationRunner | 啟動(dòng)中期 | 通用初始化 | 支持 | 完全訪問 |
CommandLineRunner | 啟動(dòng)中期 | 命令行相關(guān)初始化 | 支持 | 完全訪問 |
ApplicationReadyEvent | 最后階段 | 安全的后啟動(dòng)操作 | 無 | 完全訪問 |
七、最佳實(shí)踐建議
- 簡(jiǎn)單初始化:使用@PostConstruct或InitializingBean
- 復(fù)雜初始化:使用CommandLineRunner/ApplicationRunner
- 環(huán)境準(zhǔn)備階段:使用ApplicationContextInitializer
- 完全啟動(dòng)后操作:監(jiān)聽ApplicationReadyEvent
避免事項(xiàng):
- 不要在啟動(dòng)時(shí)執(zhí)行長(zhǎng)時(shí)間阻塞操作
- 謹(jǐn)慎處理ContextRefreshedEvent(可能被觸發(fā)多次)
- 確保初始化代碼是冪等的
八、高級(jí)應(yīng)用示例
1. 異步初始化
@Component public class AsyncInitializer { @EventListener(ApplicationReadyEvent.class) @Async public void asyncInit() { System.out.println("異步初始化開始"); // 執(zhí)行耗時(shí)初始化任務(wù) System.out.println("異步初始化完成"); } } ???????@Configuration @EnableAsync public class AsyncConfig { @Bean public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(5); executor.setQueueCapacity(100); executor.initialize(); return executor; } }
2. 初始化失敗處理
@Component public class StartupFailureHandler implements ApplicationListener<ApplicationFailedEvent> { @Override public void onApplicationEvent(ApplicationFailedEvent event) { Throwable exception = event.getException(); System.err.println("應(yīng)用啟動(dòng)失敗: " + exception.getMessage()); // 發(fā)送警報(bào)或記錄日志 } }
3. 多模塊初始化協(xié)調(diào)
public interface StartupTask { void execute() throws Exception; int getOrder(); } @Component public class StartupCoordinator implements ApplicationRunner { @Autowired private List<StartupTask> startupTasks; @Override public void run(ApplicationArguments args) throws Exception { startupTasks.stream() .sorted(Comparator.comparingInt(StartupTask::getOrder)) .forEach(task -> { try { task.execute(); } catch (Exception e) { throw new StartupException("啟動(dòng)任務(wù)執(zhí)行失敗: " + task.getClass().getName(), e); } }); } }
通過以上多種方式,Spring Boot 提供了非常靈活的啟動(dòng)時(shí)初始化機(jī)制,開發(fā)者可以根據(jù)具體需求選擇最適合的方法來實(shí)現(xiàn)啟動(dòng)時(shí)邏輯執(zhí)行。
以上就是SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行特定代碼的完整指南的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot執(zhí)行特定代碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
劍指Offer之Java算法習(xí)題精講數(shù)組與列表的查找及字符串轉(zhuǎn)換
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03RabbitMQ消息隊(duì)列實(shí)現(xiàn)延遲任務(wù)示例
這篇文章主要為大家介紹了RabbitMQ消息隊(duì)列實(shí)現(xiàn)延遲任務(wù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04IntelliJ IDEA設(shè)置JVM運(yùn)行參數(shù)的操作方法
這篇文章主要介紹了IntelliJ IDEA設(shè)置JVM運(yùn)行參數(shù)的操作方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03spring Retryable注解實(shí)現(xiàn)重試詳解
這篇文章主要介紹了spring Retryable注解實(shí)現(xiàn)重試詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java的web開發(fā)中SSH框架的協(xié)作處理應(yīng)用筆記
這篇文章主要介紹了Java的web開發(fā)中SSH框架的協(xié)作處理應(yīng)用筆記,SSH是指Struts和Spring以及Hibernate的框架搭配,需要的朋友可以參考下2015-12-12java獲取文件的inode標(biāo)識(shí)符的方法
這篇文章主要介紹了java獲取文件的inode標(biāo)識(shí)符,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04Spring通過c3p0配置bean連接數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了Spring通過c3p0配置bean連接數(shù)據(jù)庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08java使用PDFRenderer實(shí)現(xiàn)預(yù)覽PDF功能
這篇文章主要為大家詳細(xì)介紹了java使用PDFRenderer實(shí)現(xiàn)預(yù)覽PDF功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12