使用spring profile實現多環境切換


 

第一步:

applicationContext.xml

    <!--環境配置開始-->
    <beans profile="production">
        <context:property-placeholder
                ignore-resource-not-found="true" location="classpath:config.prod.properties"/>
    </beans>

    <beans profile="development">
        <context:property-placeholder
                ignore-resource-not-found="true" location="classpath:config.dev.properties"/>
    </beans>

 

第二步:

配置web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>

    <!--測試環境-->
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>development</param-value>
    </context-param>

    <!--生產環境-->
    <!--<context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>prod</param-value>
    </context-param>-->

 

 

第三步:

其重點難點:

Windows系統中,修改tomcat  start.bat文件,在第一行加入:

SET JAVA_OPTS="-Dspring.profiles.active=prod"

 

Linux 系統中,修改tomcat  catlina.sh文件,在第一行加入:

CATALINA_OPTS="$CATALINA_OPTS -Dspring.profiles.active=\"prod\""

或者

JAVA_OPTS="-Dspring.profiles.active=\"production\""

 

Window 系統中查看是否設置成功:

打開%jdk_home%\bin 中的 jvisualvm.exe  界面即可:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM