首先在resources目录下新增META-INF文件夹,然后新建spring.factories文件,文件内容为:
org.springframework.boot.env.EnvironmentPostProcessor=com.ex.gays.framework.config.LocalSettingsEnvironmentPostProcessor
以上为我的配置
key值为:org.springframework.boot.env.EnvironmentPostProcessor
value值为你项目中读取外部文件配置类的package path
下面就是我的读取外部文件配置类的详细代码,学习的公司大佬(*^__^*)
(读取配置有个开关判断,是一个常量值,就不做代码说明了)
package com.ex.gays.framework.config; import com.ex.gays.common.utils.ConstParams; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; 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; @Slf4j public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor { // 本地测试路径 private static final String LOCATION_CONF_PATH = "D:\\JavaOther\\apache-tomcat-8.0.52\\myconf\\"; // 外部配置文件 private static final String CONF_NAME = "application.yml"; // tomcat启动参数key private static final String SYSTEM_CONF_PATH_KEY = "spring.application.conf.path"; @Override public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) { // 判断开关 if (ConstParams.PROPERTY_SWITCH.equals("on")) { String systemPropPath = System.getProperty(SYSTEM_CONF_PATH_KEY); log.info("LocalSettingsEnvironmentPostProcessor -> postProcessEnvironment -> systemPropPath : {}" ,systemPropPath); if (StringUtils.isEmpty(systemPropPath)) { log.error("SystemPropPath is null or empty, Application run failed!"); throw new RuntimeException("Failed to load system property from spring.application.conf.path ! "); } String fullPath = systemPropPath.concat(File.separator).concat(CONF_NAME); log.info("LocalSettingsEnvironmentPostProcessor -> postProcessEnvironment -> fullPath : {}" ,fullPath); try { File file = new File(fullPath); if (file.exists()) { MutablePropertySources propertySources = configurableEnvironment.getPropertySources(); Properties properties = loadProperties(file); log.info("LocalSettingsEnvironmentPostProcessor -> postProcessEnvironment -> properties toString : {}", properties.toString()); propertySources.addFirst(new PropertiesPropertySource("Config", properties)); } } catch (Exception e) { log.error("LocalSettingsEnvironmentPostProcessor -> postProcessEnvironment , Load system property exception , Please check configuration", e); } } } private Properties loadProperties(File file) { FileSystemResource resource = new FileSystemResource(file); try { return PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { throw new IllegalStateException("Failed to load file resource from " + file.getAbsolutePath(), e); } } }