1. 程式人生 > >Maven profile整合Spring profile

Maven profile整合Spring profile

在Maven的pom.xml和spring框架中,都有profile這個概念。profile是用於區分各種環境的,例如開發環境、測試環境、正式環境等。 Maven的profile經常用於在打包時根據指定環境打入不同的配置檔案配置,如資料庫配置。 Spring的Profile可以用於在不同的環境下載入不同的bean,例如@Profile註解。 下面介紹二者整合的一些步驟。 一、Spring啟用一個profile spring為beans標籤提供了profile功能,以便專案的開發和生成環境分離。 在beans中定義環境程式碼 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
profile="dev,pro,test"> 二、Spring啟用某個profile Spring啟用某個profile有多種方式,為了便於整合Maven,這裡通過web.xml方式啟用: <context-param> <param-name>spring.profiles.active</param-name> <param-value>dev</param-value> </context-param> 在預設情況下,會啟用Spring的dev profile,即開發環境。 PS 1、一和二還不清楚是不是都要做,還是二選一,我暫時是都做了;
2、二的配置下面會改為動態獲取; 三、Maven profile配置 在pom.xml中,可以配置test和pro、dev三個profile,分別對應測試環境和正式環境和開發環境。這裡也可以根據具體情況自定義。 <profiles> <profile> <!-- 本地開發環境 --> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> <activation> <activeByDefault>true</activeByDefault>
</activation> </profile> <profile> <!-- 測試環境 --> <id>test</id> <properties> <profiles.active>test</profiles.active> </properties> </profile> <profile> <!-- 生產環境 --> <id>pro</id> <properties> <profiles.active>pro</profiles.active> </properties> </profile> </profiles> 此時,執行mvn clean package -Ptest就會使用id為test的profile內的配置打包,mvn clean package -Ppro就是用來打正式環境包的命令。 profiles.active屬性很關鍵,下面會用到。 四、web.xml內容替換 在web.xml中<param-value>dev</param-value>是指定Spring的profile,接下里要實現這個內容的替換,讓mvn clean package -Ptest打包出來的web.xml變成<param-value>test</param-value>。 <context-param>         <param-name>spring.profiles.active</param-name>         <param-value>dev</param-value>  </context-param> 改為: <context-param> <param-name>spring.profiles.active</param-name> <param-value>${profiles.active}</param-value> </context-param> 五、更改pom.xml 現在一般專案都使用maven來管理,maven也有profile,可以將它們結合起來。 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.0.2</version> <configuration> <warName>app</warName> <webResources> <!-- 啟用spring profile --> <resource> <filtering>true</filtering> <directory>src/main/webapp</directory> <includes> <include>**/web.xml</include> </includes> </resource> </webResources> <warSourceDirectory>src/main/webapp</warSourceDirectory> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> mvn install -Ppro 就是釋出生產版本。 注意一個引數<filtering>true</filtering>,一定要設定成true這樣才會用對應environment目錄下的配置檔案覆蓋原來的。 六、然後我們需要在專案裡面src resource裡面的某個配置檔案新增如: profile.active=${profile.active} 如上,打包部署到不同環境時,定義不同的run configurations即可,run的時候選擇對應選項就好。

相關推薦

Maven profile整合Spring profile

在Maven的pom.xml和spring框架中,都有profile這個概念。profile是用於區分各種環境的,例如開發環境、測試環境、正式環境等。 Maven的profile經常用於在打包時根據指定環境打入不同的配置檔案配置,如資料庫配置。 Spring的Profile

Maven 整合 spring profile實現多環境自動切換

profile主要用在專案多環境執行的情況下,比如開發環境、測試環境、線上生產環境。我負責的專案某個資料管理後臺涉及到包含測試環境在內的12個不同的執行環境,所以每次釋出都很蛋疼很糾結,配置改過來改過去,到最後有些環境都忘了怎麼配的。下面以這個專案為例介紹。準備條件:spri

Maven ProfileSpring Profile 管理多環境打包

一般開發團隊會有多個部署環境,如 dev 環境用於開發自測,QA 環境讓測試團隊測試, Production 環境作為線上環境。通常不同環境的配置不同,我們希望打包時的人力消耗最少。 #Spring Boot Profile Spring Boot Profile 有許多的功能,這裡只說管理配置的內容。Spr

通過idea建立Maven專案整合Spring+spring mvc+mybatis

寫這篇文章是為了整理一下idea下整合maven專案的步驟,也為了以後讓室友們參考 建立專案 File→new→project        

使用netbeans 搭建maven工程 整合spring springmvc框架

使用netbeans7.4 自帶的tomcat7.0 所以jdk選擇7.xx 等待生成空的工程   新建web.xml 在pom.xml中新增spring的引用 1 <dependency> 2 <groupId&g

SSM框架整合Maven工程整合Spring+Springmvc+Mybatis(詳細教程,附程式碼)

一、基本概念 1、Spring Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中闡述的部分

非web專案,maven工程整合spring+mabits,並打包為可執行jar包

廢話不多說,直接開幹吧。 spring和mybatis如何整合這裡就不多說了,主要說如何在非web專案中用到這兩種,其中主要用到了 ApplicationContext ctx = new ClassPathXmlApplicationContext(new String

IDEA下Maven專案整合Spring和MyBatis出現jdbc.properties is invalid;前言中不允許有內容

在Idea下用Maven管理Spring和MyBatis整合的專案,在Junit測試service層程式碼時不會出錯,但把整個專案釋出到Tomcat時丟擲各種各樣的異常,花了最多時間的異常為: o

Spring Boot ProfileMaven Profile 整合實踐

在現代的專案開發中多人協作、多環境部署已經是必不可少的軟體開發方式,筆者目前正在開發的一個基於Spring Boot 專案環境就有四套之多,包括(本地、測試、演練、生產)。尤其是現代的大型專案開發,構建複雜、參與人數眾多等因素,使得高效的構建工具必不可少。而Ma

maven profilespring boot profile的區別

maven profile 配置 maven profile 和 spring boot profile的區別 看看哪個適合你的使用場景,這兩個是不一樣的使用場景,具體細節點選上面的連結。 appli

Maven ProfileSpring Proflie

一個優秀的構建系統必須足夠靈活,它應該能讓專案在不同的環境下都能成功地構建。Maven為了支援構建的靈活性,內建了三大特性,即屬性、Profile和資源過濾。這裡我們只介紹Profile的使用,以及和Spring Profile的整合。 1、Mav

maven項目之Profile---不同環境打包不同配置

程序員 不同的 fff payment 每次 pom.xml 目錄 麻煩 項目 作為一名程序員,在開發的過程中,經常需要面對不同的運行環境(開發環境、測試環境、生產環境、內網環境、外網環境等等),在不同的環境中,相關的配置一般不一樣,比如數據源配置、日誌文件配置、以及一些軟

spring profile

初始 conf dispatch web 配置 系統 files 優先 default 配置,激活profile. 處理測試環境,開發環境,生成環境的不同配置. Javaeconfig配置Profile @Profile註解指定某個bean屬於哪一個profile xm

Spring@Profile註解

logs nbsp clas gpo post pri class .html shu 他山之石 https://www.jianshu.com/p/948c303b2253 https://www.cnblogs.com/chenpi/p/6213849.html Sp

使用maven整合spring+springmvc+mybatis

cti 監聽 package 基於接口 mysql 5.7 spring 整合 rri ext inter 使用maven整合spring+springmvc+mybatis 開發環境:     1.  jdk1.8     2.  eclipse4.7.0 (Oxygen

Spring Profile和Mybatis進行多個數據源(H2和Mysql)的切換

sql pda 開箱 https tails val 收集 sqlserver jpetstore 總結: 最近在做WebMagic的後臺,遇到一個問題:後臺用到了數據庫,本來理想情況下是用Mysql,但是為了做到開箱即用,也整合了一個嵌入式數據庫H2。這裏面就有個問題了,

IDEA整合Spring+Springmvc+mybatis+maven

一、建立maven專案 File -> New Module,進入建立專案視窗 點選Next,填寫GroupId、ArtifactId和Version 接著下一步,這裡需要注在Properties中新增一個引數 archetypeCatalog=in

spring +mybatis + maven + swagger 整合專案開發關鍵架構說明

   隨著資訊系統的互聯互通要求,越來越多的服務介面及資料需要實時對接同步,如何採用一種高效的管理開發框架來實現相關的介面及服務的管理及釋出就變的十分必要了。分析了市場上目前的主流java方面的架構及開發模式,最終感覺使用springboot +mybatis +maven +swagge

4.spring:@Profile,AOP

Profile: 可以根據當前的環境,動態啟用和切換一系列的元件功能 指定元件在那個環境下才能被註冊到容器中,不指定任何環境下都能註冊到   1.加了環境標識的bean只有環境啟用的時候才能註冊到容器中     預設是default , @Pro

Maven專案中Spring整合Mybatis

新增jar包依賴 spring需要的jar包依賴 <dependency> <groupId>org.springframework</groupId> <artifactId>