spring通過profile實(shí)現(xiàn)開發(fā)和測(cè)試環(huán)境切換

以開發(fā)測(cè)試為例,介紹tomcat部署應(yīng)用和maven部署應(yīng)用下利用profile實(shí)現(xiàn)測(cè)試環(huán)境和開發(fā)環(huán)境切換

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的高平網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

一、tomcat部署應(yīng)用

1、數(shù)據(jù)源配置

dev.properties 路徑:/src/main/resrouces

jdbc.database=MySQL
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://mysql:3306/develop?useUnicode=true&characterEncoding=utf-8
jdbc.schema=develop
jdbc.username=root
jdbc.password=12qw4ds

test.properties 路徑:/src/main/resrouces

jdbc.database=MYSQL
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.schema=test
jdbc.username=root
jdbc.password=123456

applicationContext-detabase.xml 路徑:src/main/resources/spring

<beans profile="development">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
      <property name="driverClass" value="${jdbc.driver}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
    </bean>
  </beans>

  <beans profile="test">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
      <property name="driverClass" value="${jdbc.driver}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
    </bean>
  </beans>

2、springmvc.xml   webapp/WEB-INF

可以通過定義 profile 來將開發(fā)和生產(chǎn)環(huán)境的數(shù)據(jù)源配置分開

<beans profile="development">
    <context:property-placeholder ignore-unresolvable="true" ignore-resource-not-found="true" file-encoding="UTF-8"
      location="classpath:dev.properties" />
  </beans>

  <beans profile="test">
    <context:property-placeholder ignore-unresolvable="true" ignore-resource-not-found="true" file-encoding="UTF-8"
      location="test.properties" />
  </beans>

2、web.xml中定義默認(rèn)的profile:

默認(rèn) profile 是指在沒有任何 profile 被激活的情況下,默認(rèn) profile 內(nèi)定義的內(nèi)容將被使用,通??梢栽?web.xml 中定義全局 servlet 上下文參數(shù) spring.profiles.default 實(shí)現(xiàn)

<!-- 配置spring的默認(rèn)profile -->  
<context-param>  
    <param-name>spring.profiles.default</param-name>  
    <param-value>development</param-value>  
</context-param>

4、激活profile

spring 為我們提供了大量的激活 profile 的方法,可以通過代碼來激活,也可以通過系統(tǒng)環(huán)境變量、JVM參數(shù)、servlet上下文參數(shù)來定spring.profiles.active 參數(shù)激活 profile,這里我們通過定義 JVM 參數(shù)實(shí)現(xiàn)。以 tomcat 為例,我們?cè)?tomcat 的啟動(dòng)腳本中加入以下 JVM 參數(shù)

spring通過profile實(shí)現(xiàn)開發(fā)和測(cè)試環(huán)境切換

JAVA_OPTS="-Dspring.profiles.active=development -server -XX:PermSize=256M -XX:MaxPermSize=512M -Xms1024M -Xmx1024M -Xss512k -XX:LargePageSizeInBytes=128m -XX:MaxTenuringThreshold=15 -XX:+Aggr
essiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -
XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$CATALINA_BASE/heap.dump.bin -Djava.awt.headless=true"

如果不定義,則會(huì)使用我們指定的默認(rèn) profile 

二、maven部署應(yīng)用

1、配置文件

dev.properties 路徑為 /src/main/resources/filter

master.jdbc.driverClass = com.mysql.jdbc.Driver
master.jdbc.url = jdbc:mysql://mysql-dev:3306/dev
master.jdbc.user = root
master.jdbc.password = Aa12345678

test.properties 路徑為 /src/main/resources/filter

master.jdbc.driverClass = com.mysql.jdbc.Driver
master.jdbc.url = jdbc:mysql://mysql-test:3306/test
master.jdbc.user = root
master.jdbc.password = root

config.properties 路徑:/src/main/resource/META-INF

master.jdbc.driverClass = ${master.jdbc.driverClass}
master.jdbc.url = ${master.jdbc.url}
master.jdbc.user = ${master.jdbc.user}
master.jdbc.password = ${master.jdbc.password}

spring-datasource.xml 路徑為:/src/main/resources/spring   

<bean id="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${master.jdbc.driverClass}"/>
        <property name="url" value="${master.jdbc.url}"/>
        <property name="username" value="${master.jdbc.user}"/>
        <property name="password" value="${master.jdbc.password}"/>
        <property name="filters" value="stat"/>
        <property name="maxActive" value="50"/>
        <property name="initialSize" value="0"/>
        <property name="maxWait" value="60000"/>
        <property name="minIdle" value="0"/>
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <property name="minEvictableIdleTimeMillis" value="300000"/>
        <property name="validationQuery" value="SELECT 'x'"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
    </bean>

2、pom.xml

<profiles>
		<profile>
			<id>dev</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<properties>
				<profile.file.name>/profile/dev.properties</profile.file.name>
				<package.target>dev</package.target>
			</properties>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<profile.file.name>/profile/test.properties</profile.file.name>
				<package.target>test</package.target>
			</properties>
		</profile>
		<profile>
			<id>pro</id>
			<properties>
				<profile.file.name>/profile/pro.properties</profile.file.name>
				<package.target>pro</package.target>
			</properties>
		</profile>
	</profiles>

	.......

	<!-- 定義配置文件路徑 -->
<build>
        <filters> 
                 <filter>src/main/resources/filter/${env}.properties</filter>
    </filters>
    <resources>
        	<resource>
        		<directory>src/main/resources</directory>
        		<excludes>
        			<exclude>template**/**</exclude>
        		</excludes>
        		<filtering>false</filtering>
        	</resource>
    </resources>
</build>

其中默認(rèn)激活可以做如下配置

<activation>
	<activeByDefault>true</activeByDefault>
</activation>

filters:用于定義指定filter屬性文件位置,例如filter元素賦值filters/filter1.properties,那么這個(gè)文件里面就可以定義name=value對(duì),這個(gè)name=value對(duì)的值就可以在工程pom中通過${name}引用,默認(rèn)的filter目錄是${basedir}/src/main/filters/
resources:描述工程中資源的位置

3、spring-bean.xml

<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:/META-INF/config.properties</value>
			</list>
		</property>
</bean>
<import resource="spring-datasource.xml"/>

4、web.xml

<!-- 配置Spring初始文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
            classpath:spring/spring-bean.xml
        </param-value>
	</context-param>

5、打包

maven clean install -Pdev

名稱欄目:spring通過profile實(shí)現(xiàn)開發(fā)和測(cè)試環(huán)境切換
URL標(biāo)題:http://bm7419.com/article22/geidcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、移動(dòng)網(wǎng)站建設(shè)、定制開發(fā)、網(wǎng)站收錄、搜索引擎優(yōu)化外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)