Web 项目如何读取外部配置文件

简介: Web 项目如何读取外部配置文件

1. 用于启动时自动获取yml文件

import java.io.File;
import lombok.extern.slf4j.Slf4j;
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.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
    private static final String mainYml = "application.yml";
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String parent = System.getProperty("application.path");
        File parentFile = new File(parent);
        if(!parentFile.exists()){
            throw new NullPointerException("外部配置文件为空");
        }else {
            log.info("开始加载外部配置文件");
            File main = loadYml(parent);
            File active = loadMainYml(parent);
            YamlPropertiesFactoryBean mainYml = new YamlPropertiesFactoryBean();
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            mainYml.setResources(new FileSystemResource(main));
            yaml.setResources(new FileSystemResource(active));
            MutablePropertySources propertySources = environment.getPropertySources();
            propertySources.addFirst(new PropertiesPropertySource(main.getName(), mainYml.getObject()));
            propertySources.addFirst(new PropertiesPropertySource(active.getName(), yaml.getObject()));
        }
    }
    public File loadYml(String parent){
        String main = parent + File.separator + mainYml;
        return new File(main);
    }
    public File loadMainYml(String parent){
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        String main = parent + File.separator + mainYml;
        File mainFile = new File(main);
        if(mainFile.exists()){
            yaml.setResources(new FileSystemResource(mainFile));
            String active = yaml.getObject().getProperty("spring.profiles.active");
            String envFile = parent + File.separator + "application-" + active + ".yml";
            return new File(envFile);
        }
        return null;
    }
}
相关文章
|
2天前
|
机器学习/深度学习 人工智能 前端开发
机器学习PAI常见问题之web ui 项目启动后页面打不开如何解决
PAI(平台为智能,Platform for Artificial Intelligence)是阿里云提供的一个全面的人工智能开发平台,旨在为开发者提供机器学习、深度学习等人工智能技术的模型训练、优化和部署服务。以下是PAI平台使用中的一些常见问题及其答案汇总,帮助用户解决在使用过程中遇到的问题。
|
2天前
|
存储 开发框架 NoSQL
ASP.NET WEB——项目中Cookie与Session的用法
ASP.NET WEB——项目中Cookie与Session的用法
41 0
|
2天前
|
开发框架 前端开发 .NET
ASP.NET WEB——项目创建与文件上传操作
ASP.NET WEB——项目创建与文件上传操作
48 0
|
2天前
|
Java Maven
IDEA2022如何创建web项目
IDEA2022如何创建web项目
116 0
|
2天前
|
IDE API 开发工具
 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之Web组件
 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之Web组件
48 2
|
2天前
|
Go
golang学习3,golang 项目中配置gin的web框架
golang学习3,golang 项目中配置gin的web框架
|
2天前
|
前端开发 API Docker
web前端开发项目走proxy代理后端接口,构建发布到生产等环境后,如何修改api接口
web前端开发项目走proxy代理后端接口,构建发布到生产等环境后,如何修改api接口
45 0
|
2天前
|
XML 开发框架 .NET
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
13 1
|
2天前
|
Java Maven Spring
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)2
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
32 2
|
2天前
|
Java Maven 开发工具
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)1
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
27 2