1. 程式人生 > >spring profiles 多檔案配置

spring profiles 多檔案配置

父級pom檔案中加入此配置
<profiles>
    <!--開發庫環境-->
    <profile>
        <id>dev</id>
        <properties>
            <profiles.activation>dev</profiles.activation>
        </properties>
    </profile>
    <!--測試庫環境-->
    <profile>
        <id>test</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <profiles.activation>test</profiles.activation>
        </properties>
    </profile>
    <!--正式庫環境-->
    <profile>
        <id>release</id>

        <properties>
            <profiles.activation>release</profiles.activation>
        </properties>
    </profile>
</profiles>

 

啟動檔案web.xml加入

<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>dev</param-value>
</context-param>
<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>${profiles.activation}</param-value>
</context-param>

 

初始化xml中加入


       <beans profile="dev">
              <context:property-placeholder location="classpath:dev.properties" file-encoding="utf-8" ignore-unresolvable="true" order="2"/>
              <context:property-placeholder location="classpath:application.properties" file-encoding="utf-8" ignore-unresolvable="true" order="1"/>
       </beans>
       <beans profile="test">
              <context:property-placeholder location="classpath:test.properties" file-encoding="utf-8" ignore-unresolvable="true" order="2"/>
              <context:property-placeholder location="classpath:application.properties" file-encoding="utf-8" ignore-unresolvable="true" order="1"/>
       </beans>
</beans>