您好,登錄后才能下訂單哦!
以開發測試為例,介紹tomcat部署應用和maven部署應用下利用profile實現測試環境和開發環境切換
一、tomcat部署應用
1、數據源配置
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 來將開發和生產環境的數據源配置分開
<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中定義默認的profile:
默認 profile 是指在沒有任何 profile 被激活的情況下,默認 profile 內定義的內容將被使用,通常可以在 web.xml 中定義全局 servlet 上下文參數 spring.profiles.default 實現
<!-- 配置spring的默認profile --> <context-param> <param-name>spring.profiles.default</param-name> <param-value>development</param-value> </context-param>
4、激活profile
spring 為我們提供了大量的激活 profile 的方法,可以通過代碼來激活,也可以通過系統環境變量、JVM參數、servlet上下文參數來定spring.profiles.active 參數激活 profile,這里我們通過定義 JVM 參數實現。以 tomcat 為例,我們在 tomcat 的啟動腳本中加入以下 JVM 參數
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"
如果不定義,則會使用我們指定的默認 profile
二、maven部署應用
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>
其中默認激活可以做如下配置
<activation> <activeByDefault>true</activeByDefault> </activation>
filters:用于定義指定filter屬性文件位置,例如filter元素賦值filters/filter1.properties,那么這個文件里面就可以定義name=value對,這個name=value對的值就可以在工程pom中通過${name}引用,默認的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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。