SpringBoot读取外部的配置文件的代码实现
作者:墨momo
这篇文章主要介绍了SpringBoot读取外部的配置文件的代码实现,文中通过代码示例给大家讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
一、使用场景
假设有一个买卖商品的系统,客户希望能灵活修改首页推荐商品的个数 num。
如果 num 是写在代码里的固定值,每次修改,开发人员就得重新将系统打包部署上线,费时费力。
但如果 num 是写在 jar 包的外部配置文件中,开发人员只需要修改该外部配置文件,然后重启已经部署上线的系统,就可以达到灵活修改 num 的效果啦。
二、代码实现
(一)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环境中
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读取外部的配置文件的代码实现的文章就介绍到这了,更多相关SpringBoot读取外部配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
