1. 程式人生 > >Spring.profile實現開發、測試和生產環境的配置和切換

Spring.profile實現開發、測試和生產環境的配置和切換

軟體開發過程一般涉及“開發 -> 測試 -> 部署上線”多個階段,每個階段的環境的配置引數會有不同,如資料來源,檔案路徑等。為避免每次切換環境時都要進行引數配置等繁瑣的操作,可以通過spring的profile功能來進行配置引數的切換。

以我用到的專案的實際情況為例,首先可以在resources資料夾下分別為每個環境建立單獨的資料夾(也可以額外建立一個common資料夾,用於存放公共的引數配置檔案),每個資料夾下面存放對應的環境所需的配置檔案,就像這樣子:

在resources資料夾下建立applicationContext-profile.xml檔案,用來定義不同的profile:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee.xsd">

    <description>spring profile配置</description>

    <!-- 開發環境配置檔案 -->
    <beans profile="development">
        <context:property-placeholder
            location="classpath*:common/*.properties, classpath*:development/*.properties" />
    </beans>

    <!-- 測試環境配置檔案 -->
    <beans profile="test">
        <context:property-placeholder
            location="classpath*:common/*.properties, classpath*:test/*.properties" />
    </beans>
    
    <!-- 生產環境配置檔案 -->
    <beans profile="production">
        <context:property-placeholder
            location="classpath*:common/*.properties, classpath*:production/*.properties" />
    </beans>

</beans>

這樣就實現了通過profile標記不同的環境,接下來就可以通過設定spring.profiles.default和spring.profiles.active這兩個屬性來啟用和使用對應的配置檔案。default為預設,如果沒有通過active來指定,那麼就預設使用default定義的環境。

這兩個屬性可以通過多種方法來設定:

  • 在web.xml中作為web應用的上下文引數context-param;
  • 在web.xml中作為DispatcherServlet的初始化引數;
  • 作為JNDI條目;
  • 作為環境變數;
  • 作為JVM的系統屬性;
  • 在整合測試類上,使用@ActiveProfiles註解配置。

前兩者都可以在web.xml檔案中設定:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/applicationContext*.xml
        </param-value>
    </context-param>

    <!-- 在上下文context-param中設定profile.default的預設值 -->
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>development</param-value>
    </context-param>

    <!-- 在上下文context-param中設定profile.active的預設值 -->
    <!-- 設定active後default失效,web啟動時會載入對應的環境資訊 -->
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>development</param-value>
    </context-param>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 在DispatcherServlet引數中設定profile的預設值,active同理 -->
        <init-param>
            <param-name>spring.profiles.default</param-name>
            <param-value>development</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

啟用指定的環境,也可以通過JVM引數來設定,可以在tomcat的啟動指令碼中加入以下JVM引數來啟用:

-Dspring.profiles.active="production"

在程式中,也可以通過 @Profile("...") 對某些資源進行註解,這樣只有當選擇對應的環境時,才會產生對應的bean,如:

@Bean
@Profile("production")    
    public DataSource jndiDataSource(){
        JndiObjectFactoryBean jofb=new JndiObjectFactoryBean();
        jofb.setJndiName("jndi/iDS");
        jofb.setResourceRef(true);
        jofb.setProxyInterface(xxx.class);
        return (DataSource) jofb.getObject();
    }
}