Spring enables development and test environment switching with profile

Source: Internet
Author: User

Take development testing as an example, introducing the use of profile to implement test environment and development environment switching under the Tomcat deployment application and MAVEN deployment application

One, Tomcat deployment application

1. Data Source Configuration

Dev.properties Path:/src/main/resrouces

Jdbc.database=mysqljdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://mysql:3306/develop?useunicode=true &characterencoding=utf-8jdbc.schema=developjdbc.username=rootjdbc.password=12qw4ds

Test.properties Path:/src/main/resrouces

Jdbc.database=mysqljdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/test?useunicode=true &characterencoding=utf-8jdbc.schema=testjdbc.username=rootjdbc.password=123456

Applicationcontext-detabase.xml Path: 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&nbsP;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

You can define the profile to separate the data source configuration for the development and production environments

<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. The default profile is defined in Web. xml:

The default profile is that the content defined in the default profiles will be used in the case where there is no such activity, and it is often possible to define global servlet context parameters in Web. Spring.profiles.default implementation

<!--Configure Spring's default profile--<context-param> <param-name>spring.profiles.default</param-name&      Gt <param-value>development</param-value> </context-param>


4. Activate profile

Spring provides us with a large number of methods for activating the profile, which can be activated by code, or the spring.profiles.active parameter can be activated through system environment variables, JVM parameters, servlet context parameters, Here we implement by defining JVM parameters. in Tomcat, for example, we added the following JVM parameters to the Tomcat startup script

Java_opts= "-dspring.profiles.active=development-server-xx:permsize=256m-xx:maxpermsize=512m-xms1024m-xmx1024m- xss512k-xx:largepagesizeinbytes=128m-xx:maxtenuringthreshold=15-xx:+aggressiveopts-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 "

If not defined, the default profile that we specify is used

II. MAVEN Deployment Application

1. Configuration files

Dev.properties Path is/src/main/resources/filter

Master.jdbc.driverClass = Com.mysql.jdbc.Drivermaster.jdbc.url = Jdbc:mysql://mysql-dev:3306/devmaster.jdbc.user = Rootmaster.jdbc.password = Aa12345678

Test.properties Path is/src/main/resources/filter

Master.jdbc.driverClass = Com.mysql.jdbc.Drivermaster.jdbc.url = Jdbc:mysql://mysql-test:3306/testmaster.jdbc.user = Rootmaster.jdbc.password = root

Config.properties Path:/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 path is:/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= "/>        <" 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>.......<!--  Define configuration file Paths  --><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>

Where the default activation can be configured as follows

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

Filters: used to define the specified filter property file location, such as the filter element assignment value Filters/filter1.properties, then this file can be defined Name=value, this name= Value pairs can be referenced in the Engineering Pom via ${name}, and the default filter directory is ${basedir}/src/main/filters/
Resources: Describe the location of a resource in a project

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

<!--configuring Spring initial file--><context-param><param-name>contextconfiglocation</param-name>< Param-value> Classpath:spring/spring-bean.xml </param-value></context-param>

5. Packing

maven Clean Install  - Pdev

Spring enables development and test environment switching with profile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.