詳解SpringBoot程序啟動時執(zhí)行初始化代碼
因項目集成了Redis緩存部分?jǐn)?shù)據(jù),需要在程序啟動時將數(shù)據(jù)加載到Redis中,即初始化數(shù)據(jù)到Redis。
在SpringBoot項目下,即在容器初始化完畢后執(zhí)行我們自己的初始化代碼。
第一步:創(chuàng)建實現(xiàn)ApplicationListener接口的類
package com.stone;
import com.stone.service.IPermissionService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
/**
* @author Stone Yuan
* @create 2017-12-02 21:54
* @description
*/
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
IPermissionService service = contextRefreshedEvent.getApplicationContext().getBean(IPermissionService.class);
service.loadUserPermissionIntoRedis();
}
}
注意:
1、我們自己的初始化代碼寫在onApplicationEvent里;
2、ContextRefreshedEvent是Spring的ApplicationContextEvent一個實現(xiàn),在容器初始化完成后調(diào)用;
3、以注解的方式注入我們需要的bean,會報空指針異常,因此需要以代碼中的方式獲取我們要的bean
第二步:在SpringBootApplication中注冊我們剛創(chuàng)建的類
package com.stone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class YwythApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(YwythApplication.class);
springApplication.addListeners(new ApplicationStartup());
springApplication.run(args);
}
}
利用CommandLineRunner、EnvironmentAware在Spring boot啟動時執(zhí)行初始化代碼
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
//如果有多個這樣的類時,可以通過Order指定執(zhí)行順序,數(shù)值越小執(zhí)行優(yōu)先級越高
@Order(value = 0)
public class InitSystemConfig implements CommandLineRunner, EnvironmentAware {
/*
* 在服務(wù)啟動后執(zhí)行,會在@Bean實例化之后執(zhí)行,故如果@Bean需要依賴這里的話會出問題
*/
@Override
public void run(String... args) {
//這里可以根據(jù)數(shù)據(jù)庫返回結(jié)果創(chuàng)建一些對象、啟動一些線程等
}
/*
* 在SystemConfigDao實例化之后、@Bean實例化之前執(zhí)行
* 常用于讀取數(shù)據(jù)庫配置以供其它bean使用
* environment對象可以獲取配置文件的配置,也可以把配置設(shè)置到該對象中
*/
@Override
public void setEnvironment(Environment environment) {
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot初始化啟動報錯Error?creating?bean?with?name?'dataSource'?defined?in?class?path?resource
- SpringBoot實現(xiàn)第一次啟動時自動初始化數(shù)據(jù)庫的方法
- SpringBoot啟動并初始化執(zhí)行sql腳本問題
- springboot中項目啟動時實現(xiàn)初始化方法加載參數(shù)
- springboot使用CommandLineRunner解決項目啟動時初始化資源的操作
- springboot 啟動時初始化數(shù)據(jù)庫的步驟
- SpringBoot項目啟動時如何讀取配置以及初始化資源
- springboot組件初始化后的4種啟動方式及常用方法
相關(guān)文章
Spring?Boot集成LiteFlow規(guī)則引擎的詳細(xì)過程
本文詳細(xì)介紹了如何在Spring?Boot應(yīng)用程序中集成LiteFlow規(guī)則引擎,并探討如何使用LiteFlow庫來實現(xiàn)業(yè)務(wù)流程的規(guī)則處理,將通過具體的示例來展示如何在Spring?Boot應(yīng)用程序中配置和使用LiteFlow規(guī)則引擎,以提高系統(tǒng)的靈活性和可維護(hù)性,感興趣的朋友跟隨小編一起看看吧2024-07-07
Java創(chuàng)建,編輯與刪除Excel迷你圖表的實現(xiàn)方法
迷你圖是Excel工作表單元格中表示數(shù)據(jù)的微型圖表。本文將通過Java代碼示例介紹如何在Excel中創(chuàng)建迷你圖表,以及編輯和刪除表格中的迷你圖表,需要的可以參考一下2022-05-05
SpringBoot 監(jiān)聽Redis鍵過期事件(過期監(jiān)聽)
Redis鍵過期事件是SpringBoot中常用的緩存失效通知方式,通過配置可以監(jiān)聽特定鍵的過期事件,具有一定的參考價值,感興趣的可以了解一下2024-12-12
RestTemplate發(fā)送HTTP?GET請求使用方法詳解
這篇文章主要為大家介紹了關(guān)于RestTemplate發(fā)送HTTP?GET請求的使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家<BR>33+多多進(jìn)步2022-03-03
SpringBoot源碼分析之bootstrap.properties文件加載的原理
本文通過訪問看到bootstrap.properties中的信息獲取到了,同時age也被application.properties中的屬性覆蓋掉了。加載順序到底是什么?為什么會覆蓋呢?我們接下來分析下吧2021-12-12
Java基于動態(tài)規(guī)劃法實現(xiàn)求最長公共子序列及最長公共子字符串示例
這篇文章主要介紹了Java基于動態(tài)規(guī)劃法實現(xiàn)求最長公共子序列及最長公共子字符串,簡單描述了動態(tài)規(guī)劃法的概念、原理,并結(jié)合實例形式分析了Java使用動態(tài)規(guī)劃法求最長公共子序列以及最長公共子字符串相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-08-08
Java使用條件語句和循環(huán)結(jié)構(gòu)確定控制流(實例)
下面小編就為大家?guī)硪黄狫ava使用條件語句和循環(huán)結(jié)構(gòu)確定控制流(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

