若依如何区分不同环境下配置文件?

Posted Roc-xb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了若依如何区分不同环境下配置文件?相关的知识,希望对你有一定的参考价值。

当在多配置文件中,需要切换配置文件时,通常的做法都是修改激活的文件名称,而spring.profiles.active=@profiles.active@是配合maven profile进行选择不同配置文件进行启动,可以避免修改文件,而在maven打包是指定使用哪个配置文件。

目录

1、配置pom.xml,定义不同环境配置属性

 2、修改对应的配置文件

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, "");
    
    

以上是关于若依如何区分不同环境下配置文件?的主要内容,如果未能解决你的问题,请参考以下文章

ruoyi-vue(若依前后端分离版本)环境搭建 用eclipse 安装redis 后端配置 配置node环境 前端配置

若依vue项目更改开发环境端口

若依vue项目更改开发环境端口

搭建reactJS 项目的时候 区分各种环境进行打包处理

若依框架详细使用

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