@PropertySource(value={"file:/config/config.properties"})
public class Application {
}
注意必须要加 file: 开头 配置文件 必须 properties 格式 yml
文件不支持
LocalSettingsEnvironmentPostProcessor
package cc.ewell.dripping.product.baseinfo.common.config;
import lombok.extern.log4j.Log4j;
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.core.io.support.PropertiesLoaderUtils;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
@Log4j
public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor{
private static final String LOCATION = "\\dripping\\baseinfo.properties";
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
File file = new File(System.getProperty("EWELL_ENV"), LOCATION);
//本地环境变量 EWELL_ENV 设置父目录
if (file.exists()) {
log.info(" File "+System.getProperty("EWELL_ENV")+LOCATION);
MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
Properties properties = loadProperties(file);
log.info(" Properties "+properties.toString());
propertySources.addFirst(new PropertiesPropertySource("Config", properties));
}else {
log.warn(" File "+System.getProperty("EWELL_ENV")+LOCATION+" IS NOT EXIST");
}
}
private Properties loadProperties(File f) {
FileSystemResource resource = new FileSystemResource(f);
try {
return PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
}
}
}
这里的 addFirst 方法 是先加着外部配置文件
addLast 方法 是后加着外部配置文件 配置文件加载的顺序 是后面加载的配置文件存在同样的配置 会覆盖先加载的。
propertySources.addLast(new PropertiesPropertySource("Config", properties));
![](https://i-blog.csdnimg.cn/blog_migrate/945bf4b675e36d98f15121be8576ec95.png)
image.png
resources文件夹下创建一个文件夹名为META-INF,在里面创建一个spring.factories的文件,文件内容如下:
org.springframework.boot.env.EnvironmentPostProcessor=cc.ewell.dripping.product.baseinfo.common.config.LocalSettingsEnvironmentPostProcessor