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;
    }
}
相关文章
|
6月前
|
安全 Shell PHP
pear文件利用 (远程文件下载、生成配置文件、写配置文件) 从一道题看——CTFshow私教 web40
pear文件利用 (远程文件下载、生成配置文件、写配置文件) 从一道题看——CTFshow私教 web40
412 0
|
8月前
|
XML 开发框架 .NET
【已解决】请在位于当前 Web 应用程序根目录下的“web.config”配置文件中创建一个 <customErrors> 标记
【已解决】请在位于当前 Web 应用程序根目录下的“web.config”配置文件中创建一个 <customErrors> 标记
|
9月前
|
XML 开发框架 .NET
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
98 1
|
Java 测试技术 Spring
Spring入门&控制反转(或依赖注入)&AOP的关键概念& 多配置文件&与web集成(二)
Spring入门&控制反转(或依赖注入)&AOP的关键概念& 多配置文件&与web集成
|
SQL Java 关系型数据库
Spring入门&控制反转(或依赖注入)&AOP的关键概念& 多配置文件&与web集成(一)
Spring入门&控制反转(或依赖注入)&AOP的关键概念& 多配置文件&与web集成
192 0
|
Java Spring
java202304java学习笔记第六十一天-ssm-spring配置文件-spring集成web环境
java202304java学习笔记第六十一天-ssm-spring配置文件-spring集成web环境
93 0
java202304java学习笔记第六十一天-ssm-spring配置文件-spring集成web环境
|
开发者
SSM整合相关配置文件(pom.xml,web.xml,spring.xml,springMVC.xml)
SSM整合相关配置文件(pom.xml,web.xml,spring.xml,springMVC.xml)
146 0
|
NoSQL Java 关系型数据库
Go语学习笔记 - 配置文件使用、日志配置 | Web框架Gin(二)
Go语学习笔记 - 配置文件使用、日志配置 | Web框架Gin(二)
Go语学习笔记 - 配置文件使用、日志配置 | Web框架Gin(二)
|
Java 容器
【SpringBoot 2】(六)配置文件 web开发相关(二)
【SpringBoot 2】(六)配置文件 web开发相关(二)
179 0
【SpringBoot 2】(六)配置文件 web开发相关(二)
|
XML 存储 前端开发
【SpringBoot 2】(六)配置文件 web开发相关(一)
【SpringBoot 2】(六)配置文件 web开发相关(一)
133 0
【SpringBoot 2】(六)配置文件 web开发相关(一)

热门文章

最新文章

  • 1
    Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
    23
  • 2
    AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
    19
  • 3
    【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
    54
  • 4
    部署使用 CHAT-NEXT-WEB 基于 Deepseek
    303
  • 5
    【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
    26
  • 6
    java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
    38
  • 7
    零基础构建开源项目OpenIM桌面应用和pc web- Electron篇
    28
  • 8
    【01】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-硬件设备实时监控系统运营版发布-本产品基于企业级开源项目Zabbix深度二开-分步骤实现预计10篇合集-自营版
    20
  • 9
    FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
    53
  • 10
    springSecurity学习之springSecurity过滤web请求
    59