当前位置:
首页
文章
前端
详情

spring通过profile实现开发和测试环境切换

转载: http://blog.51cto.com/1385903/2060233

以开发测试为例,介绍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 参数

spring通过profile实现开发和测试环境切换

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

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。