spring通过profile实现开发和测试环境切换-创新互联

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

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享名称:spring通过profile实现开发和测试环境切换-创新互联
本文来源:https://www.cdcxhl.com/article44/diejhe.html

成都网站建设公司_创新互联,为您提供域名注册网站收录电子商务网站内链搜索引擎优化动态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化