不同环境(测试,预发等)配置文件的读取

当然classpath是基础,已懂。

项目中在classpath下建立不同环境的properties文件,右侧选择的哪个profile则在classpath下生成的所有配置文件用到的环境配置中的值都是右侧指定的profile的环境配置文件的。如果右侧没有选择任何profile,则使用的是pom.xml中配置为true的 profile来生成classpath下的配置文件,如果都为false的话则使用外层<properties> 下的<env>dev</env>指定的来生成。优先级:右侧,里properties,外properties

 

<properties>
    <env>pre</env>
    <ihhotel-version>1.0-SNAPSHOT</ihhotel-version>
    <spring-version>4.2.7.RELEASE</spring-version>
    <mybatis-version>3.4.1</mybatis-version>
    <mybatis-spring-version>1.3.0</mybatis-spring-version>
    <druid-version>1.0.11</druid-version>
    <mysql-connection-version>5.1.24</mysql-connection-version>
    <pagehelper-version>4.0.1</pagehelper-version>
</properties>
<build>
    <filters>
        <filter>src/main/filters/${env}.properties</filter>:路径必须有不然找不到配置文件
    </filters>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF8</encoding>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

在pom.xml中配置false和true来选择是否默认激活:

 

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <env>dev</env>
        </properties>
    </profile>
    <profile>
        <id>jd-test</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <env>jd-test</env>
        </properties>
    </profile>
    <profile>
        <id>pre</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <env>pre</env>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <env>prod</env>
        </properties>
    </profile>
</profiles>
<properties>
        <env>jd-test</env>
        <ihhotel-version>1.0-SNAPSHOT</ihhotel-version>
        <spring-version>4.2.7.RELEASE</spring-version>
        <mybatis-version>3.4.1</mybatis-version>
        <mybatis-spring-version>1.3.0</mybatis-spring-version>
        <druid-version>1.0.11</druid-version>
        <mysql-connection-version>5.1.24</mysql-connection-version>
        <pagehelper-version>4.0.1</pagehelper-version>
</properties>


<dependencies>
<build>
    <filters>
        <filter>src/main/filters/${env}.properties</filter>
    </filters>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF8</encoding>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

 

IDEA里右侧选择的话按它来,否则项目会根据pom中的<properties> <env>pre</env>来选择

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值