SpringBoot使用spring.factories加載默認(rèn)配置的實(shí)現(xiàn)代碼
在日常開(kāi)發(fā)過(guò)程中,發(fā)布一些產(chǎn)品或者框架時(shí),會(huì)遇到某些功能需要一些配置才能正常運(yùn)行,這時(shí)我們需要的提供默認(rèn)配置項(xiàng),同時(shí)用戶也能覆蓋進(jìn)行個(gè)性化
創(chuàng)建Initializer
public class FrameContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("FrameContextInitializer--Start");
System.out.println("FrameContextInitializer--End");
}
}
配置Initializer
resources/META-INF文件夾下創(chuàng)建spring.factories文件,指定實(shí)現(xiàn)類(lèi)
org.springframework.context.ApplicationContextInitializer=com.haopan.frame.common.initializer.FrameContextInitializer
實(shí)現(xiàn)Initializer
讀取默認(rèn)yml文件
String frameYAMLFilePath = ClassPathFileUtil.getFilePathActual("systemfile/config/frame.yml");
public static String getFilePathActual(String classFilePath) {
String filePath = "";
try {
String templateFilePath = "tempfiles/classpathfile/";
File tempDir = new File(templateFilePath);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
String[] filePathList = classFilePath.split("/");
String checkFilePath = "tempfiles/classpathfile";
for (String item : filePathList) {
checkFilePath += "/" + item;
}
File tempFile = new File(checkFilePath);
if (tempFile.exists()) {
tempFile.delete();
}
//解析
ClassPathResource classPathResource = new ClassPathResource(classFilePath);
InputStream inputStream = classPathResource.getInputStream();
checkFilePath = "tempfiles/classpathfile";
for (int i = 0; i < filePathList.length; i++) {
checkFilePath += "/" + filePathList[i];
if (i == filePathList.length - 1) {
//文件
File file = new File(checkFilePath);
if(!file.exists()){
FileUtils.copyInputStreamToFile(inputStream, file);
}
} else {
//目錄
tempDir = new File(checkFilePath);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
}
}
inputStream.close();
filePath = checkFilePath;
} catch (Exception e) {
e.printStackTrace();
}
return filePath;
}
將yml內(nèi)容加到環(huán)境上下文
這邊的addLast是執(zhí)行順序?yàn)樽詈笞x取,如果項(xiàng)目的yml文件沒(méi)有讀取到,則默認(rèn)配置是一個(gè)保底
System.out.println("FrameContextInitializer--Start");
PropertySource<?> propertySource = loader.load("frameConfiguration", new InputStreamResource(new FileInputStream(frameYAMLFilePath))).get(0);
applicationContext.getEnvironment().getPropertySources().addLast(propertySource);
System.out.println("FrameContextInitializer--End");
到此這篇關(guān)于SpringBoot使用spring.factories加載默認(rèn)配置的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)SpringBoot spring.factories加載配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot處理請(qǐng)求參數(shù)中包含特殊符號(hào)
今天寫(xiě)代碼遇到了一個(gè)問(wèn)題,請(qǐng)求參數(shù)是個(gè)路徑“D:/ExcelFile”,本文就詳細(xì)的介紹一下該錯(cuò)誤的解決方法,感興趣的可以了解一下2021-06-06
java中pdf轉(zhuǎn)圖片的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇java中pdf轉(zhuǎn)圖片的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
Java Spring JdbcTemplate基本使用詳解
JDBC已經(jīng)能夠滿足大部分用戶最基本的需求,但是在使用JDBC時(shí),必須自己來(lái)管理數(shù)據(jù)庫(kù)資源如:獲取PreparedStatement,設(shè)置SQL語(yǔ)句參數(shù),關(guān)閉連接等步驟2021-10-10
淺談Java中隨機(jī)數(shù)的幾種實(shí)現(xiàn)方式
這篇文章主要介紹了Java中隨機(jī)數(shù)的幾種實(shí)現(xiàn)方式,從最簡(jiǎn)單的Math.random到多線程的并發(fā)實(shí)現(xiàn)都在本文所列之中,需要的朋友可以參考下2015-07-07
深入解析Java編程中的boolean對(duì)象的運(yùn)用
這篇文章主要介紹了Java編程中的boolean對(duì)象的運(yùn)用,是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10
將字符串?dāng)?shù)字格式化為樣式1,000,000,000的方法
這篇文章主要介紹了將字符串?dāng)?shù)字格式化為樣式1,000,000,000的方法,有需要的朋友可以參考一下2014-01-01
微服務(wù)Spring?Boot?整合Redis?阻塞隊(duì)列實(shí)現(xiàn)異步秒殺下單思路詳解
這篇文章主要介紹了微服務(wù)Spring?Boot?整合Redis?阻塞隊(duì)列實(shí)現(xiàn)異步秒殺下單,使用阻塞隊(duì)列實(shí)現(xiàn)秒殺的優(yōu)化,采用異步秒殺完成下單的優(yōu)化,本文給大家分享詳細(xì)步驟及實(shí)現(xiàn)思路,需要的朋友可以參考下2022-10-10

