詳解SpringBoot程序啟動時執(zhí)行初始化代碼
因項目集成了Redis緩存部分數(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 { /* * 在服務啟動后執(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 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)文章
Springboot整合Dubbo+Nacos實現(xiàn)RPC調(diào)用的示例代碼
隨著互聯(lián)網(wǎng)技術(shù)的飛速發(fā)展,越來越多的企業(yè)和開發(fā)者開始關(guān)注微服務架構(gòu),Nacos是阿里巴巴開源的一個動態(tài)服務發(fā)現(xiàn)、配置管理和服務管理平臺,本文講解如何將Spring Boot與Dubbo和Nacos整合,實現(xiàn)RPC調(diào)用,需要的朋友可以參考下2024-02-02java8之LocalDate的使用、LocalDate格式化問題
這篇文章主要介紹了java8之LocalDate的使用、LocalDate格式化問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04Spring Boot MyBatis 連接數(shù)據(jù)庫配置示例
本篇文章主要介紹了Spring Boot MyBatis 連接數(shù)據(jù)庫示例的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02