SpringBoot2.X 项目使用外置绝对路径的配置文件
spring-boot-absolute-config
前言
该工程是为解决应用部署应用时指定配置文件存放位置的问题.
SpringBoot项目默认加载以下位置的配置文件:
|
1
2
3
4
|
classpath:file:./classpath:config/file:./config/: |
想要指定外部的配置文件, 一种方法就是通过启动脚本来控制:
|
1
2
|
在启动脚本中添加:-Dspring.config.location=文件绝对路径 |
但有时候有些项目需要兼容之前的老项目,就会遇到使用外部绝对路径的来指定配置文件了,每次都在启动脚本中添加,显然不是很合适.因此诞生了该工程.
实现方式
通过实现 EnvironmentPostProcessor 接口, 自定义实现方法:
|
1
|
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) |
来实现加载自定义配置文件.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package com.github.springboot.absolute.config;import org.springframework.boot.SpringApplication;import org.springframework.boot.context.config.ConfigFileApplicationListener;import org.springframework.boot.env.EnvironmentPostProcessor;import org.springframework.boot.env.PropertiesPropertySourceLoader;import org.springframework.boot.env.PropertySourceLoader;import org.springframework.boot.env.YamlPropertySourceLoader;import org.springframework.core.Ordered;import org.springframework.core.env.ConfigurableEnvironment;import org.springframework.core.env.PropertySource;import org.springframework.core.io.PathResource;import org.springframework.core.io.Resource;import org.springframework.util.StringUtils;import java.io.FileNotFoundException;import java.io.IOException;import java.util.List;/** * <p> 自定义环境加载策略 * <p>需要在classpath下application.yml或application.properties中指定文件位置</p> * <p>key=config.file.absolute.path</p> * * @author lijinghao * @version : MyEnvironmentPostProcessor.java, v 0.1 2018年09月13日 下午4:55:55 lijinghao Exp $ */public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { /**, * 指定加载外部文件类型: [.properties], [.yml], [.yaml] */ private static final String SUFFIX_TYPE_YML = ".yml"; private static final String SUFFIX_TYPE_YAML = ".yaml"; private static final String SUFFIX_TYPE_PROPERTIES = ".properties"; /** * 指定外部配置文件路径的KEY */ private static final String CONFIG_FILE_ABSOLUTE_PATH = "config.file.absolute.path"; private static final int DEFAULT_ORDER = ConfigFileApplicationListener.DEFAULT_ORDER + 1; /** * Post-process the given {@code env}. * * @param environment the env to post-process * @param application the application to which the env belongs */ @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { try { // Get file absolute path String path = environment.getProperty(CONFIG_FILE_ABSOLUTE_PATH); if (StringUtils.isEmpty(path)) { System.out.println("WARNING: External file path to be loaded is not configured.[config.file.absolute.path]"); return; } System.out.println("INFO: Loading external file: \"" + path + "\""); // Loading external file Resource resource = new PathResource(path); PropertySourceLoader loader; if (resource.exists() && resource.isFile()) { String filename = resource.getFilename(); String fileSuffix = filename.substring(filename.indexOf(".")); if (SUFFIX_TYPE_PROPERTIES.equalsIgnoreCase(fileSuffix)) { loader = new PropertiesPropertySourceLoader(); } else if (SUFFIX_TYPE_YML.equalsIgnoreCase(fileSuffix) || SUFFIX_TYPE_YAML.equalsIgnoreCase(fileSuffix)) { loader = new YamlPropertySourceLoader(); } else { throw new RuntimeException("Unsupported file types: " + fileSuffix); } }else { throw new FileNotFoundException("Cannot find the file : \"" + path +"\""); } List<PropertySource<?>> sources = loader.load("externalFiles", resource); for (PropertySource<?> source : sources) { environment.getPropertySources().addLast(source); } } catch (IOException e) { e.printStackTrace(); } } /** * Get the order value of this object. * <p>Higher values are interpreted as lower priority. As a consequence, * the object with the lowest value has the highest priority (somewhat * analogous to Servlet {@code load-on-startup} values). * <p>Same order values will result in arbitrary sort positions for the * affected objects. * * @return the order value * @see #HIGHEST_PRECEDENCE * @see #LOWEST_PRECEDENCE */ @Override public int getOrder() { return DEFAULT_ORDER; }} |
在classpath:META-INF 下创建:spring.factories
添加如下内容:
|
1
|
org.springframework.boot.env.EnvironmentPostProcessor=com.github.springboot.absolute.config.MyEnvironmentPostProcessor |
使用说明
引入pom文件
12345<dependency><groupId>com.github.springboot</groupId><artifactId>absolute-config</artifactId><version>1.0.0-RELEASE</version></dependency>在classpath下的配置文件中增加参数
12如,在application.yml中添加config.file.absolute.path: /opt/app/config/**/**/application.yml重启项目
重启项目时,会自动加载指定位置的配置文件;
注意事项
支持配置文件的格式
121) classpath下SpringBoot默认加载application.properties、application.yml或application.yaml;2) 外置配置文件可以是以.properties、.yml或.yaml结尾(注意配置内容的格式);外部加载的配置文件,不能使用原始配置文件的key
12如: server.port:8090此参数只在classpath下的配置文件中生效,在外部加载的配置文件中不生效.此类key主要是在 ConfigFileApplicationListener 中进行加载.
引入了配置文件,但没配置config.file.absolute.path
此时不会报错,只会在启动时打印提醒的语句.
配置了错误的config.file.absolute.path
此时在项目启动时会打印出错误的异常栈,但不影响程序的正常启动.
但是,如果你的项目中依赖了外置配置文件中的内容,可能会报错.
具体源代码详见:https://github.com/lthaoshao/spring-boot-absolute-config
原文地址:https://www.cnblogs.com/lthaoshao/p/9676045.html
SpringBoot2.X 项目使用外置绝对路径的配置文件的更多相关文章
- Android获取内置sdcard跟外置sdcard路径
Android获取内置sdcard跟外置sdcard路径.(测试过两个手机,亲测可用) 1.先得到外置sdcard路径,这个接口是系统提供的标准接口. 2.得到上一级文件夹目录 3.得到该目录的所有文 ...
- J2EE web项目中解决所有路径问题
Java中使用的路径,分为两种:绝对路径和相对路径.归根结底,Java本质上只能使用绝对路径来寻找资源.所有的相对路径寻找资源的方法,都不过是一些便利方法.不过是API在底层帮助我们构建了绝对路径,从 ...
- 【转】Java Web 项目获取运行时路径 classpath
Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...
- java web项目中 获取resource路径下的文件路径
public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...
- (转)关于java和web项目中的相对路径问题
原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...
- Android-获取外置SDcard路径
Android手机支持SDcard.目前很多手机厂商把SDcard集成到手机中,当然有的手机同时也支持可插拔的SDcard.这就有了内置SDcard和位置SDcard之分.当手机同时支持内置和外置SD ...
- JavaWeb 项目中的绝对路径和相对路径以及问题的解决方式
近期在做JavaWeb项目,总是出现各种的路径错误,并且发现不同情况下 / 所代表的含义不同,导致在调试路径上浪费了大量时间. 在JavaWeb项目中尽量使用绝对路径 由于使用绝对路径是绝对不会出 ...
- springboot2 webflux 响应式编程学习路径
springboot2 已经发布,其中最亮眼的非webflux响应式编程莫属了!响应式的weblfux可以支持高吞吐量,意味着使用相同的资源可以处理更加多的请求,毫无疑问将会成为未来技术的趋势,是必学 ...
- Java中动态获取项目根目录的绝对路径
https://www.cnblogs.com/zhouqing/archive/2012/11/10/2757774.html 序言 在开发过程中经常会用到读写文件,其中就必然涉及路径问题.使用固定 ...
随机推荐
- Xdebug步骤
谷歌浏览器安装xdebug cd /etc/php/7.2/fpm/conf.d 重启sudo service php7.1-fpm restart (注意 php版本) 重启编辑器
- Sublime Text 2 快捷键用法大全
Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑) Ctrl+G 跳转到相应的行 Ctrl+J 合并行(已选择需要合并的多行时) Ctrl+L 选择整行(按住-继续 ...
- 【JZOJ4811】【NOIP2016提高A组五校联考1】排队
题目描述 输入 输出 样例输入 5 4 1 2 1 3 3 4 3 5 1 4 2 4 1 2 2 5 样例输出 3 1 1 2 数据范围 样例解释 解法 可推知原树可以转换为一个序列,即优先序列: ...
- 【JZOJ4770】【NOIP2016提高A组模拟9.9】闭门造车
题目描述 自从htn体验了一把飙车的快感,他就下定决心要闭门造车!但是他两手空空怎么造得出车来呢?无奈的他只好来到了汽车零部件商店. 一走进商店,玲琅满目的各式零件看得htn眼花缭乱.但是他很快便反应 ...
- Effective Modern C++:05右值引用、移动语义和完美转发
移动语义使得编译器得以使用成本较低的移动操作,来代替成本较高的复制操作:完美转发使得人们可以撰写接收任意实参的函数模板,并将其转发到目标函数,目标函数会接收到与转发函数所接收到的完全相同的实参.右值引 ...
- 好玩又实用,阿里巴巴开源混沌工程工具 ChaosBlade
减少故障的最好方法就是让问题经常性的发生.在可控范围或环境下,通过不断重复失败过程,持续提升系统的容错和弹性能力. 那么,实施一次高效的混沌工程实验,需要几步呢? 答案:2 步. ① 登陆 Chaos ...
- 洛谷P2607 骑士
题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里, ...
- oracle函数 INITCAP(c1)
[功能]返回字符串并将字符串的第一个字母变为大写,其它字母小写; [参数]c1字符型表达式 [返回]字符型 [示例] SQL> select initcap('smith abc aBC') u ...
- android学习——Android Layout标签之-viewStub,requestFocus,merge,include
定义Android Layout(XML)时,有四个比较特别的标签是非常重要的,其中有三个是与资源复用有关,分别是<viewStub/>, <requestFocus />, ...
- saltStack 配置管理(也就是替换文件)
目录 /srv/salt/base下面新建一个文件dns.sls /opt/resolv.conf_bak: #这个是文件替换的位置,也就说替换到远程文件的/opt/resolv.conf_ ...