spring-boot-absolute-config
前言
該工程是為解決應用部署應用時指定配置文件存放位置的問題.
SpringBoot項目默認加載以下位置的配置文件:
classpath: file:./ classpath:config/ file:./config/:
想要指定外部的配置文件, 一種方法就是通過啟動腳本來控制:
在啟動腳本中添加: -Dspring.config.location=文件絕對路徑
但有時候有些項目需要兼容之前的老項目,就會遇到使用外部絕對路徑的來指定配置文件了,每次都在啟動腳本中添加,顯然不是很合適.因此誕生了該工程.
實現方式
通過實現 EnvironmentPostProcessor 接口, 自定義實現方法:
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application)
來實現加載自定義配置文件.
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
添加如下內容:
org.springframework.boot.env.EnvironmentPostProcessor=com.github.springboot.absolute.config.MyEnvironmentPostProcessor
使用說明
-
引入pom文件
<dependency> <groupId>com.github.springboot</groupId> <artifactId>absolute-config</artifactId> <version>1.0.0-RELEASE</version> </dependency> -
在classpath下的配置文件中增加參數
如,在application.yml中添加 config.file.absolute.path: /opt/app/config/**/**/application.yml
-
重啟項目
重啟項目時,會自動加載指定位置的配置文件;
注意事項
-
支持配置文件的格式
1) classpath下SpringBoot默認加載application.properties、application.yml或application.yaml; 2) 外置配置文件可以是以.properties、.yml或.yaml結尾(注意配置內容的格式);
-
外部加載的配置文件,不能使用原始配置文件的key
如: server.port: 8090 此參數只在classpath下的配置文件中生效,在外部加載的配置文件中不生效.
此類key主要是在 ConfigFileApplicationListener 中進行加載.
-
引入了配置文件,但沒配置config.file.absolute.path
此時不會報錯,只會在啟動時打印提醒的語句.
-
配置了錯誤的config.file.absolute.path
此時在項目啟動時會打印出錯誤的異常棧,但不影響程序的正常啟動.
但是,如果你的項目中依賴了外置配置文件中的內容,可能會報錯.
具體源代碼詳見:https://github.com/lthaoshao/spring-boot-absolute-config
