前言:

在springboot项目中,一般的配置文件都在resource/config下面,它可以以两种方式存在,一种是yml,一种是properties方式。

当运维和开发分开的时候,比如连接mysql数据库生产上的时候,运维不会告诉你账户和密码,需要将配置文件放到固定的目录下,运维自己去配置。这样就需要配置文件外置。

当配置文件外置的时候,他是在项目启动的时候,自己去加载配置文件。下面请看实现。

1. 需要增加一个文件

spring.factories,这个文件里面配置启动的时候需要初始化的信息

org.springframework.boot.env.EnvironmentPostProcessor=cn.fintecher.pangolin.service.common.config.AutoConfigEnvironmentPostProcessor

2. 在AutoConfigEnvironmentPostProcessor这个类中增加如下代码

package cn.fintecher.pangolin.service.common.config;

import cn.fintecher.pangolin.common.utils.AutoConfigEnvironmentUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.SpringFactoriesLoader; import java.util.List; @Order(1)
public class AutoConfigEnvironmentPostProcessor implements EnvironmentPostProcessor { private final Logger logger = LoggerFactory.getLogger(AutoConfigEnvironmentPostProcessor.class); private final ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); private final List<PropertySourceLoader> propertySourceLoaders; public AutoConfigEnvironmentPostProcessor() {
super();
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader());
} @Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
AutoConfigEnvironmentUtil.postProcessEnvironment(environment, application, propertySourceLoaders, resourcePatternResolver, logger);
} } 公共方法:
public static void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application,
List<PropertySourceLoader> propertySourceLoaders, ResourcePatternResolver resourcePatternResolver,
Logger logger) { String[] activeProfiles = environment.getActiveProfiles();
for (String activeProfile : activeProfiles) {
if (Objects.equals(activeProfile, "swagger"))
continue;
for (PropertySourceLoader loader : propertySourceLoaders) {
for (String fileExtension : loader.getFileExtensions()) {
if (!fileExtension.equals("yml"))
continue; String applicationLocal = ResourceUtils.CLASSPATH_URL_PREFIX + "/config/application.yml";
try {
Resource applicationResource = resourcePatternResolver.getResource(applicationLocal);
List<PropertySource<?>> applactionYML = loader.load(ResourceUtils.CLASSPATH_URL_PREFIX + "/config/application.yml", applicationResource);
applactionYML.stream().forEach(environment.getPropertySources()::addLast); } catch (IOException e) {
e.printStackTrace();
} String path = (String) environment.getPropertySources().get("applicationConfig: [classpath:/config/application.yml]").getProperty("spring.config.location"); String location = path + "/application-" + activeProfile + "." + fileExtension;
try {
Resource[] resources = resourcePatternResolver.getResources(location);
for (Resource resource : resources) {
List<PropertySource<?>> propertySources = loader.load(resource.getFilename(), resource);
if (null != propertySources && !propertySources.isEmpty()) {
propertySources.stream().forEach(environment.getPropertySources()::addLast);
}
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
}
}

3.  在代码中获取了spring.config.location这个配置文件,这个配置文件在application.yml中配置如下

spring:
application:
name: common-service
config:
location: @profile.properties@
@profile.properties@ 取的是pom文件中的

<profile.properties>file:d:/tomcat/profiles/${project.artifactId}/properties</profile.properties>
这样这个配置文件就可以放在任何目录下面。

4.  其他信息比如logger日志也需要放在外面,在application.yml增加如下配置即可。

ps: 不需要改动的配置信息都可以配置在application.yml中。

logging:
config: @profile.properties@/logback-spring.xml

5. 可以试试。

												

springboot配置文件外置处理的更多相关文章

  1. SpringBoot使用外置的Servlet容器

    SpringBoot默认使用嵌入式的Servlet容器,应用打包成可执行的jar包 优点:简单.便携 缺点:默认不支持jsp,优化定制比较复杂(使用定制器serverProperties.自定义Emb ...

  2. 解决spring-boot配置文件使用加密方式保存敏感数据启动报错No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly

    spring-boot配置文件使用加密方式保存敏感数据 application.yml spring: datasource: username: dbuser password: '{cipher} ...

  3. springboot配置文件中使用当前配置的变量

    在开发中,有时我们的application.properties某些值需要重复使用,比如配置redis和数据库或者mongodb连接地址,日志,文件上传地址等,且这些地址如果都是相同或者父路径是相同的 ...

  4. SpringBoot 配置文件存放位置及读取顺序

    SpringBoot配置文件可以使用yml格式和properties格式 分别的默认命名为:application.yml.application.properties 存放目录 SpringBoot ...

  5. [SpringBoot] - 配置文件的多种形式及JSR303数据校验

    Springboot配置文件: application.yml   application.properties(自带) yml的格式写起来稍微舒服一点 在application.properties ...

  6. 将springboot配置文件中的值注入到静态变量

    SpringBoot配置文件分为.properties和.yml两种格式,根据启动环境的不同获取不同环境的的值. spring中不支持直接注入静态变量值,利用spring的set注入方法注入静态变量 ...

  7. SpringBoot配置文件 application.properties详解

    SpringBoot配置文件 application.properties详解   本文转载:https://www.cnblogs.com/louby/p/8565027.html 阅读过程中若发现 ...

  8. Spring-Boot配置文件数据源配置项

    Spring-Boot配置文件数据源配置项(常用配置项为红色) 参数 介绍 spring.datasource.continue-on-error = false 初始化数据库时发生错误时,请勿停止 ...

  9. 【日常错误】spring-boot配置文件读取不到

    最近在用spring-boot做项目时,遇到自定义的配置文件无法读取到的问题,通过在appcation.java类上定义@PropertySource(value = {"classpath ...

随机推荐

  1. windows server疑难杂症

    1.某些网址.服务访问失败,可能的原因:增强的安全配置关闭增强的安全配置,并且重启电脑!!!http://jingyan.baidu.com/article/6181c3e076ac0b152ff15 ...

  2. WPF 集合分组排序

    <Window x:Class="ViewExam.MainWindow"        xmlns="http://schemas.microsoft.com/w ...

  3. VS2012中使用CEGUI项目发布到XP平台的问题(核心方法就一句话。“你项目使用的所有外部依赖库都用/MT编译。”)

    接着上一篇文章,详细说说如何把一个带CEGUI的项目发布到XP平台. 这个问题纠缠了我好几天.这里把详细解决思路记下来.有同样问题的朋友可以少走很多弯路. 核心方法就一句话.“你项目使用的所有外部依赖 ...

  4. UBUTUN 通过蓝牙连接Hoary和诺基亚手机

    通过蓝牙连接Hoary和诺基亚手机 这个how to已经用Hoary.诺基亚6630和一个道尔芯片(Dongle)蓝牙(Usb蓝牙)测试过了.通过这个How to,你可以:-通过蓝牙,从你的电脑发送文 ...

  5. PostgreSQL在win7上安装详细步骤

    原文:PostgreSQL在win7上安装详细步骤 PostgreSQL安装: 一.windows下安装过程 安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程 ...

  6. 流程图浅析MFC架构

    http://blog.csdn.net/qq2399431200/article/details/9035315

  7. Lucene Index Search

    转发自:  https://my.oschina.net/u/3777556/blog/1647031 什么是Lucene?? Lucene 是 apache 软件基金会发布的一个开放源代码的全文检索 ...

  8. Openssl - Static libraries (w32, mingw) 以及对Qt静态编译时的设置

    Openssl static libraries created for Windows 32bit using MinGW compiler   Compiled with:       ./Con ...

  9. Android和IOS启动第三方地图APP

    最近客户新提了需求,地址字段要能通过第三方的地图进行定位,于是对Android和IOS端进行了调整. 以下是调用地图部分的代码. android可按照包名来判断app是否存在: 方法: /* * ch ...

  10. Hadoop集群(第5期)SecureCRT使用

    1.SecureCRT简介   SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,同时支持Telnet和rlogin协议.SecureCRT是一款用于连接运行包括Windows. ...