一、利用场景
假设有一个生意业务商品的体系,客户盼望能机动修改首页保举商品的个数 num。
假如 num 是写在代码里的固定值,每次修改,开发职员就得重新将体系打包摆设上线,费时费力。
但假如 num 是写在 jar 包的外部设置文件中,开发职员只须要修改该外部设置文件,然后重启已经摆设上线的体系,就可以到达机动修改 num 的结果啦。
二、代码实现
(一)application.yml 的设置
设置外部文件的路径,这里是 customer.yml,和 src 文件夹同级,如图。
- customer:
- path: customer.yml
复制代码
(二)编辑 customer.yml
这里设置了一个 num,值是 5
(三)自界说方法读取外部设置文件
- package com.xxx.env;
- 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);
- }
- }
- }
复制代码 (四)将自界说方法注册到 Spring Boot
在 src/main/resources/META-INF/spring.factories 中注册自界说方法 MyEnvironmentPostProcessor,如下
- # 将自定义方法的路径填进去
- org.springframework.boot.env.EnvironmentPostProcessor=com.xxx.env.MyEnvironmentPostProcessor
复制代码 (五)利用外部设置文件的设置
- 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 ''");
- }
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金 |