1.修改pom.xml,过滤src/main/resources目录下.properties文件
![e938d794607a403576446e1b0905026a.png](https://img-blog.csdnimg.cn/img_convert/e938d794607a403576446e1b0905026a.png)
pom.xml
pom.xml代码:
XXXXorg.springframework.boot spring-boot-maven-plugin src/main/resources**/*.properties
2.在resources目录下创建META_INF/spring.factories文件(注意META_INF文件夹也是要创建的)
![99f450a433949a0268117f96ff497a4e.png](https://img-blog.csdnimg.cn/img_convert/99f450a433949a0268117f96ff497a4e.png)
spring.factories
spring.factories内容:
org.springframework.boot.env.EnvironmentPostProcessor=com.config.LocalSettingsEnvironmentPostProcessor
3.新建LocalSettingsEnvironmentPostProcessor.java加载外部属性文件application.properties
![6c9aa00a53b035fd9e5ad358633230a2.png](https://img-blog.csdnimg.cn/img_convert/6c9aa00a53b035fd9e5ad358633230a2.png)
LocalSettingsEnvironmentPostProcessor
LocalSettingsEnvironmentPostProcessor.java代码如下:
package com.config;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;/** * @author 光州程序猿 * @version 1.0 */public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor { private static final String LOCATION = "C:甥敳獲Desktopapplication.properties"; @Override public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) { File file = new File(LOCATION); try { MutablePropertySources propertySources = configurableEnvironment.getPropertySources(); Properties properties = loadProperties(file); propertySources.addFirst(new PropertiesPropertySource("Config", properties)); } catch (Exception e) { e.printStackTrace(); } } 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); } }}
启动SpringBoot项目,指定外部application.properties启动成功。
![b264729781826f2f5322d0595227667a.png](https://img-blog.csdnimg.cn/img_convert/b264729781826f2f5322d0595227667a.png)
启动结果