詳解SpringBoot程序啟動(dòng)時(shí)執(zhí)行初始化代碼
因項(xiàng)目集成了Redis緩存部分?jǐn)?shù)據(jù),需要在程序啟動(dòng)時(shí)將數(shù)據(jù)加載到Redis中,即初始化數(shù)據(jù)到Redis。
在SpringBoot項(xiàng)目下,即在容器初始化完畢后執(zhí)行我們自己的初始化代碼。
第一步:創(chuàng)建實(shí)現(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、我們自己的初始化代碼寫(xiě)在onApplicationEvent里;
2、ContextRefreshedEvent是Spring的ApplicationContextEvent一個(gè)實(shí)現(xiàn),在容器初始化完成后調(diào)用;
3、以注解的方式注入我們需要的bean,會(huì)報(bào)空指針異常,因此需要以代碼中的方式獲取我們要的bean
第二步:在SpringBootApplication中注冊(cè)我們剛創(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啟動(dòng)時(shí)執(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 //如果有多個(gè)這樣的類時(shí),可以通過(guò)Order指定執(zhí)行順序,數(shù)值越小執(zhí)行優(yōu)先級(jí)越高 @Order(value = 0) public class InitSystemConfig implements CommandLineRunner, EnvironmentAware { /* * 在服務(wù)啟動(dòng)后執(zhí)行,會(huì)在@Bean實(shí)例化之后執(zhí)行,故如果@Bean需要依賴這里的話會(huì)出問(wèn)題 */ @Override public void run(String... args) { //這里可以根據(jù)數(shù)據(jù)庫(kù)返回結(jié)果創(chuàng)建一些對(duì)象、啟動(dòng)一些線程等 } /* * 在SystemConfigDao實(shí)例化之后、@Bean實(shí)例化之前執(zhí)行 * 常用于讀取數(shù)據(jù)庫(kù)配置以供其它bean使用 * environment對(duì)象可以獲取配置文件的配置,也可以把配置設(shè)置到該對(duì)象中 */ @Override public void setEnvironment(Environment environment) { } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot初始化啟動(dòng)報(bào)錯(cuò)Error?creating?bean?with?name?'dataSource'?defined?in?class?path?resource
- SpringBoot實(shí)現(xiàn)第一次啟動(dòng)時(shí)自動(dòng)初始化數(shù)據(jù)庫(kù)的方法
- SpringBoot啟動(dòng)并初始化執(zhí)行sql腳本問(wèn)題
- springboot中項(xiàng)目啟動(dòng)時(shí)實(shí)現(xiàn)初始化方法加載參數(shù)
- springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作
- springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫(kù)的步驟
- SpringBoot項(xiàng)目啟動(dòng)時(shí)如何讀取配置以及初始化資源
- springboot組件初始化后的4種啟動(dòng)方式及常用方法
相關(guān)文章
Java進(jìn)程cpu占用過(guò)高問(wèn)題解決
這篇文章主要介紹了Java進(jìn)程cpu占用過(guò)高問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Springboot整合Dubbo+Nacos實(shí)現(xiàn)RPC調(diào)用的示例代碼
隨著互聯(lián)網(wǎng)技術(shù)的飛速發(fā)展,越來(lái)越多的企業(yè)和開(kāi)發(fā)者開(kāi)始關(guān)注微服務(wù)架構(gòu),Nacos是阿里巴巴開(kāi)源的一個(gè)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺(tái),本文講解如何將Spring Boot與Dubbo和Nacos整合,實(shí)現(xiàn)RPC調(diào)用,需要的朋友可以參考下2024-02-02java8之LocalDate的使用、LocalDate格式化問(wèn)題
這篇文章主要介紹了java8之LocalDate的使用、LocalDate格式化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Spring Boot MyBatis 連接數(shù)據(jù)庫(kù)配置示例
本篇文章主要介紹了Spring Boot MyBatis 連接數(shù)據(jù)庫(kù)示例的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02