SpringBoot讀取外部的配置文件的代碼實現(xiàn)
一、使用場景
假設有一個買賣商品的系統(tǒng),客戶希望能靈活修改首頁推薦商品的個數(shù) num。
如果 num 是寫在代碼里的固定值,每次修改,開發(fā)人員就得重新將系統(tǒng)打包部署上線,費時費力。
但如果 num 是寫在 jar 包的外部配置文件中,開發(fā)人員只需要修改該外部配置文件,然后重啟已經(jīng)部署上線的系統(tǒng),就可以達到靈活修改 num 的效果啦。
二、代碼實現(xiàn)
(一)application.yml 的配置
配置外部文件的路徑,這里是 customer.yml,和 src 文件夾同級,如圖。
customer: path: customer.yml

(二)編輯 customer.yml
這里配置了一個 num,值是 5
num: 5
(三)自定義方法讀取外部配置文件
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import java.io.File;
import java.util.Properties;
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
//自定義配置文件,對應 application.yml 里的前綴
String profiles = environment.getProperty("customer.path");
//加載成PropertySource對象,并添加到Environment環(huán)境中
File file = new File(profiles);
Resource resource = new FileSystemResource(file);
environment.getPropertySources().addLast(loadProfiles(resource));
}
/**
* 加載單個配置文件
* @param resource
* @return
*/
private PropertySource<?> loadProfiles(Resource resource) {
// 判斷資源是否存在
if (!resource.exists()) {
throw new IllegalArgumentException("資源" + resource + "不存在");
}
// 判斷后綴名,兼容 .yml 文件和 .properties 文件
if (resource.getFilename().contains(".yml")) {
return loadYaml(resource);
} else {
return loadProperty(resource);
}
}
/**
* 加載properties格式的配置文件
*
* @param resource
* @return
*/
private PropertySource loadProperty(Resource resource) {
try {
//從輸入流中加載一個Properties對象
Properties properties = new Properties();
properties.load(resource.getInputStream());
return new PropertiesPropertySource(resource.getFilename(), properties);
} catch (Exception ex) {
throw new IllegalStateException("加載配置文件失敗" + resource, ex);
}
}
/**
* 加載yml格式的配置文件
*
* @param resource
* @return
*/
private PropertySource loadYaml(Resource resource) {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource);
//從輸入流中加載一個Properties對象
Properties properties = factory.getObject();
return new PropertiesPropertySource(resource.getFilename(), properties);
} catch (Exception ex) {
throw new IllegalStateException("加載配置文件失敗" + resource, ex);
}
}
}
(四)使用外部配置文件的配置
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private Environment environment;
@GetMapping("/test")
public void test() {
// 讀取 num 配置值,不為空則輸出
String num = environment.getProperty("num");
if (num != null && !num.equals("")) {
System.out.println("num = " + num);
} else {
System.out.println("num is null or ''");
}
}
}
到此這篇關于SpringBoot讀取外部的配置文件的代碼實現(xiàn)的文章就介紹到這了,更多相關SpringBoot讀取外部配置文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot接口實現(xiàn)百萬并發(fā)的代碼示例
隨著互聯(lián)網(wǎng)的發(fā)展,越來越多的應用需要支持高并發(fā),在這種情況下,如何實現(xiàn)高并發(fā)成為了一個重要的問題,Spring Boot是一個非常流行的Java框架,它提供了很多方便的功能來支持高并發(fā),本文將介紹如何使用Spring Boot來實現(xiàn)百萬并發(fā)2023-10-10
Java 實現(xiàn)協(xié)同過濾算法推薦算法的示例代碼
本文介紹了協(xié)同過濾算法的概念,包括基于用戶的協(xié)同過濾和基于物品的協(xié)同過濾,文章詳細解釋了數(shù)據(jù)準備、相似度計算以及如何在Java中實現(xiàn)這些算法,通過一個簡單的用戶-物品評分矩陣示例,展示了如何計算用戶和物品之間的相似度,并推薦未評分的物品,感興趣的朋友一起看看吧2025-02-02

