軟件開發過程一般涉及“開發 -> 測試 -> 部署上線”多個階段,每個階段的環境的配置參數會有不同,如數據源,文件路徑等。為避免每次切換環境時都要進行參數配置等繁瑣的操作,可以通過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(); } }
參考: