1、在“src/main/resources/META-INF/spring.factories”增加文件路径:
org.springframework.boot.env.EnvironmentPostProcessor=\
com.code.config.ConfigEnvironmentPostProcessor
2、在指定路径下创建文件:ConfigEnvironmentPostProcessor
public class ConfigEnvironmentPostProcessor implements EnvironmentPostProcessor {private static final Logger log = LoggerFactory.getLogger(ConfigEnvironmentPostProcessor.class);static String DEF_CONFIG_PATH;static String PRJECT_NAME = "PRJECT-NAME";static final String PROJECT_CONFIG_FILE = "META-INF/project.properties";private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();public ConfigEnvironmentPostProcessor() {}public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {String proName = null;if (!FileUtil.exist(DEF_CONFIG_PATH)) {try {log.error("在【{}】中没有发现配置文件!", DEF_CONFIG_PATH);throw new Exception("没有发现配置文件!");} catch (Exception var8) {var8.printStackTrace();}}if (ClassUtils.getDefaultClassLoader().getResource("/") != null) {String proDir = ClassUtils.getDefaultClassLoader().getResource("/").getPath();String proPath = proDir + "META-INF/project.properties";if (FileUtil.exist(proPath)) {Props props = new Props(proPath);Object obj = props.get(PRJECT_NAME);if (Objects.nonNull(obj)) {proName = String.valueOf(obj);}}if (StrUtil.isBlank(proName)) {proName = "";}this.loadProProperties(proName, environment);}}private void loadProProperties(String pro, ConfigurableEnvironment environment) {MutablePropertySources propertySources = environment.getPropertySources();String basePath = DEF_CONFIG_PATH;if (StrUtil.isNotBlank(pro)) {basePath = basePath + File.separator + pro;}Resource path = new FileSystemResource(basePath + File.separator + "application.yml");PropertySource<?> propertySource = this.loadYaml(path);propertySources.addLast(propertySource);Object active = propertySource.getProperty("spring.profiles.active");if (Objects.nonNull(active)) {path = new FileSystemResource(basePath + File.separator + "application-" + active + ".yml");propertySource = this.loadYaml(path);propertySources.addLast(propertySource);}}private PropertySource<?> loadYaml(Resource path) {if (!path.exists()) {throw new IllegalArgumentException("Resource " + path + " does not exist");} else {try {return (PropertySource) this.loader.load(path.getFilename(), path).get(0);} catch (IOException var3) {throw new IllegalStateException("Failed to load yaml configuration from " + path, var3);}}}static {String os = System.getProperty("os.name").toLowerCase();if (os.contains("linux")) {DEF_CONFIG_PATH = "/home/myDoip/project/config";} else {String path = ConfigEnvironmentPostProcessor.class.getResource("/").toString();path = path.replace("file:/", "").replace("/target/classes/", "");DEF_CONFIG_PATH = path;File file = new File(DEF_CONFIG_PATH + File.separator + "application.yml");if (!file.exists()) {path = path + "/src/main/resources/";DEF_CONFIG_PATH = path;}System.out.println("配置文件所在位置:" + DEF_CONFIG_PATH);PRJECT_NAME = "PRJECT-NAME-DEV";}} }