Spring.profile配合Jenkins發布War包,實現開發、測試和生產環境的按需切換


前兩篇不錯

Spring.profile實現開發、測試和生產環境的配置和切換 - Strugglion - 博客園
https://www.cnblogs.com/strugglion/p/7091021.html

詳解Spring中的Profile - 簡書
https://www.jianshu.com/p/948c303b2253

spring-mvc-hibernate.xml

    <!-- JSR303 Validator定義 -->
    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

    <!-- 引入屬性文件 -->
    <!-- 基於Spring Profile機制的database連接配置 -->
    <!-- 開發環境配置文件 -->
    <beans profile="dev">
        <context:property-placeholder location="classpath:profiles/dbconfig_dev.properties" ignore-unresolvable="true" />
    </beans>
    <!-- 測試環境配置文件 -->
    <beans profile="test">
        <context:property-placeholder location="classpath:profiles/dbconfig_test.properties" ignore-unresolvable="true" />
    </beans>
    <!-- 生產環境配置文件 -->
    <beans profile="prod">
        <context:property-placeholder location="classpath:profiles/dbconfig_prod.properties" ignore-unresolvable="true" />
    </beans>
</beans>

 

web.xml

    <!-- 設置spring.profiles.active后則spring.profiles.default失效,web啟動時會加載對應的環境信息 -->
    <!-- 為了適應jenkins的持續集成發布crm.war方案,在tomcat的bin/setenv.sh中追加如下參數:-Dspring.profiles.active="xxxx"。xxx為dev/test/prod其中之一。 -->
    <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>prod</param-value>
    </context-param>
    -->

 setenv.sh

[root@crm_web apache-tomcat-7.0.91]# cat bin/setenv.sh
CATALINA_OPTS="$CATALINA_OPTS -Dspring.profiles.active=test -Djava.rmi.server.hostname=192.168.66.16 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access"

 setenv.bat

set CATALINA_OPTS=-Dspring.profiles.active=test -Djava.rmi.server.hostname=192.168.66.182 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access

 

 

注意事項:

1.spring.profiles.active

2.profile 特性是在spring3.1中引入的,3.0還不支持。xml配置中的spring-beans-3.0.xsd,在3.0中解析不到3.1中的profile,所以必須替換成spring-beans-3.1.xsd

  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

3.Nested <beans> must appear last in the file.

  https://dzone.com/articles/spring-31-environment-profiles

    Nested <beans> must appear last in the file.

    文件有正確順序的標簽示例。

  https://stackoverflow.com/questions/33869324/spring-profile-and-property-placeholder-exception

    I would try to re-order the application context file, having the two profiles before <context:property-placeholder properties-ref="deployProperties" />.

  http://www.cnblogs.com/davidwang456/p/4204569.html

   // Any nested <beans> elements will cause recursion in this method. In
     // order to propagate and preserve <beans> default-* attributes correctly,
     // keep track of the current (parent) delegate, which may be null. Create
     // the new (child) delegate with a reference to the parent for fallback purposes,
     // then ultimately reset this.delegate back to its original (parent) reference.
     // this behavior emulates a stack of delegates without actually necessitating one.
   還得多讀源碼,如果不把<beans>放在<bean>,會導致循環引用的問題。

官方參考

3. New Features and Enhancements in Spring Framework 3.1
https://docs.spring.io/spring-framework/docs/3.2.0.RELEASE/spring-framework-reference/html/new-in-3.1.html

Spring 3.1 M2: Testing with @Configuration Classes and Profiles
http://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles/

Spring 3.1 M1: Introducing @Profile
http://spring.io/blog/2011/02/14/spring-3-1-m1-introducing-profile/

Spring Framework 3.1 M1 released
http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/

11. Testing
https://docs.spring.io/spring-framework/docs/3.2.0.RELEASE/spring-framework-reference/html/testing.html#testcontext-framework

cbeams/spring-3.1-profiles-java: Sample demonstrating use of Spring 3.1's 'bean definition profiles' feature with @Configuration classes
https://github.com/cbeams/spring-3.1-profiles-java

Spring Framework Reference Documentation
http://files.cnblogs.com/files/kongkaikai/spring-framework-reference.pdf

Introduction · Spring Framework 4.x參考文檔
http://blog.didispace.com/books/spring-framework-4-reference/

 

 

以下的作為參考

Spring實戰——Profile - JackieZheng - 博客園
http://www.cnblogs.com/bigdataZJ/p/SpringInAction4.html

通過 spring 容器內建的 profile 功能實現開發環境、測試環境、生產環境配置自動切換 - 許恕 - CSDN博客
https://blog.csdn.net/xvshu/article/details/51133786

詳解Spring中的Profile - 王雲十三 - 博客園
https://www.cnblogs.com/SummerinShire/p/6392242.html

詳解Spring中的Profile - 劉劍峰的博客 - CSDN博客
https://blog.csdn.net/jeffleo/article/details/71433655

spring @profile注解的使用 - 止水的專欄 - CSDN博客
https://blog.csdn.net/wild46cat/article/details/71189858


免責聲明!

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



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