springboot加载外部配置文件-war包直接读取外部配置文件

1. springboot支持动态的读取文件,扩展接口:org.springframework.boot.env.EnvironmentPostProcessor

我的项目使用场景起因是: 在同一台机器上起了两个tomcat实例, 每个项目的日志文件打印路径要配成不同, 如果每次打包手动修改打印日志的路径太费时费力, 所以考虑把配置文件每个tomcat放一份, 启动时自动读取当前tomcat文件下的配置就好.

2. 我这里的自定义配置文件存放路径: tomcat的conf文件夹下面

3. application.properties配置文件内容(我这里不同tomcat配置参数值不同)

spring.profiles.active=test
4. 定义MyEnvironmentPostProcessor实现EnvironmentPostProcessor接口

package com.hlz.web.common.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 javax.servlet.ServletContextEvent;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
 
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
 
 
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) {
        //tomcat路径
        String property = System.getProperty("catalina.home");
        System.out.println("catalinahome:"+property);
 
        String path =property+File.separator+"conf"+File.separator+"myspringboot.properties";
        File file = new File(path);
        System.out.println("Loading local settings from : "+path);
 
        if (file.exists()) {
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            Properties properties = loadProperties(file);
            System.out.println(properties.toString());
            propertySources.addFirst(new PropertiesPropertySource("Config", properties));
        }
    }
 
    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);
        }
    }
}
 
5. 在classpath定义一个META-INF文件夹然后在其下面先建spring.factories文件,在其中指定:

org.springframework.boot.env.EnvironmentPostProcessor=com.hlz.web.common.config.MyEnvironmentPostProcessor

 

 

启动工程

springboot加载外部配置文件-war包直接读取外部配置文件

springboot加载外部配置文件-war包直接读取外部配置文件


6. 如果同一个参数, application.yml中有定义, 外部配置文件也有定义, 以哪一个为准呢,

//以配置文件为准 
propertySources.addFirst(new PropertiesPropertySource("Config", properties));
 
//以application.yml中的文件为准
 propertySources.addLast(new PropertiesPropertySource("Config", properties));
7.一些官网的关于EnvironmentPostProcessor的说明

Allows for customization of the application's Environment prior to the application context being refreshed.
 
EnvironmentPostProcessor implementations have to be registered in META-INF/spring.factories, using the fully qualified name of this class as the key.
 
EnvironmentPostProcessor processors are encouraged to detect whether Spring's Ordered interface has been implemented or if the @Order annotation is present and to sort instances accordingly if so prior to invocation.