当前位置: 移动技术网 > IT编程>开发语言>Java > Spring读取不同环境的配置文件

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

2019年08月28日  | 移动技术网IT编程  | 我要评论

配置文件

需求

针对不同环境下,可以配置不同的配置文件,如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, "");
    }
    
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网