Spring读取不同环境的配置文件

2019-08-29 00:00:00 读取 环境 配置文件

目录

  • 配置文件
    • 需求
    • Spring环境
      • 配置类
      • PropertySourceFactory
    • 非Spring环境

配置文件

需求

针对不同环境下,可以配置不同的配置文件,如application-dev.properties、application-test.properties。

Spring环境

采用spring.profiles.active属性来区分不同的应用环境。

配置类

@Data
@Component
@ConfigurationProperties(prefix = "log")
@PropertySource(name = "log", value = "classpath:log-${spring.profiles.active:}.properties", factory = SystemPropertySourceFactory.class)
public class LogConfig implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private String            tableName        = "comm_log";
    //private String            logType          = "LX2";
    private String            host;
    private int               port;
    private int               maxThreads       = 20;
    private int               saveMode         = 0;
    private boolean           saveLog          = true;
    private String            topic            = "TOPIC_COMMON_LOG";
    private String            defaultErrCode;
    private boolean           openInTable      = false;
    private String            inTableName;
    private String            remoteUrl;
    private String            namesrvAddr;
    private String            producerGroupName;
    private String            instanceName;
    private int               sendTimeout;
}

若spring.profiles.active为空,则该值为空字符串。

PropertySourceFactory

/**
 * 针对自定义配置文件的一个多环境配置逻辑
 * 
 */
public class SystemPropertySourceFactory implements PropertySourceFactory {
    
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource)
        throws IOException {
        String fileSuffix = "";
        try {
            String filename = resource.getResource().getFilename();
            fileSuffix = filename.substring(filename.lastIndexOf("."));
            resource.getResource().getFile();
            return new ResourcePropertySource(name, resource);
        } catch (Exception e) {
            InputStream inputStream = SystemPropertySourceFactory.class.getClassLoader()
                .getResourceAsStream(name + fileSuffix);
            //转成resource
            InputStreamResource inResource = new InputStreamResource(inputStream);
            return new ResourcePropertySource(new EncodedResource(inResource));
        }
    }
    
}

若spring.profiles.active=dev有配置,但是没有这个配置文件(例:log-dev.properties),则默认读取log.properties。若spring.profiles.active无配置,则默认读取log.properties。

非Spring环境

思路:将spring.profiles.active 属性设置到System环境变量。则非Spring环境可从System环境变量中获取该值。

@Component
public class ServerInfo implements ApplicationListener<WebServerInitializedEvent> {
    private static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active";
    
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        //设置系统变量
        System.setProperty(SPRING_PROFILES_ACTIVE, this.getProfileActive());
    }
    
    /**
     * 获取spring.profiles.active属性值
     * @return
     */
    public String getProfileActive() {
        return env.getProperty(SPRING_PROFILES_ACTIVE, "");
    }
    
}
    原文作者:chenzuyibao
    原文地址: https://www.cnblogs.com/chenzuyibao/p/11425046.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章