java服务器读取配置文件

来源:互联网 发布:酷优网络 编辑:程序博客网 时间:2024/06/05 23:43

切换配置文件,完成参数切换。


1.编辑POM

    <dependencies>        <!-- property configuration -->        <dependency>            <groupId>commons-configuration</groupId>            <artifactId>commons-configuration</artifactId>            <version>1.10</version>        </dependency>    </dependencies>    <properties>        <!-- Disable Swagger by default -->        <swagger.enabled>false</swagger.enabled>    </properties>        <profiles>        <profile>            <id>localTest</id>            <properties>                <swagger.enabled>true</swagger.enabled>            </properties>        </profile>    </profiles>


这样配置文件为localTest时开启swagger


2.在resources下新建文件TestConfig.properties

编辑TestConfig.properties

#swagger stateswagger.enabled = ${swagger.enabled}

3.调用

import org.apache.commons.configuration.Configuration;import org.apache.commons.configuration.ConfigurationException;import org.apache.commons.configuration.PropertiesConfiguration;//swagger        try {            Configuration config = new PropertiesConfiguration("TestConfig.properties");            if(config.getBoolean("swagger.enabled")){                buildSwagger();                handlerList.addHandler(buildSwaggerUI());            }        } catch (ConfigurationException e) {        }

这样当配置文件为localTest时,swagger开启,否则关闭

原创粉丝点击