前言:
在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. 可以試試。