這篇文章說明了如何經過Maven配置Spring依賴項。最新的Spring版本能夠在Maven Central上找到。html
Maven中的Spring基本依賴關係web
Spring的設計是高度模塊化的 - 使用Spring的一部分不該該並且不須要另外一部分。例如,基本的Spring Context能夠沒有Persistence或MVC Spring庫。spring
讓咱們先從一個基本Maven配置,將只使用了spring-context依賴:express
<properties>
<org.springframework.version>3.2.8.RELEASE</org.springframework.version>
<!-- <org.springframework.version>4.0.2.RELEASE</org.springframework.version> -->
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
<scope>runtime</scope>
</dependency>
複製代碼
這個依賴項 - spring-context - 定義了實際的Spring Injection Container,而且有少許的依賴項:spring-core,spring-expression,spring-aop和spring-beans。經過支持一些核心Spring技術來擴充容器:Core Spring實用程序,Spring表達式語言(SpEL),面向對象編程支持和JavaBeans機制。編程
注意咱們在運行時範圍中定義了依賴關係- 這將確保在任何特定於Spring的API上沒有編譯時依賴性。對於更高級的用例,能夠從一些選定的Spring依賴項中刪除運行時範圍,可是對於更簡單的項目,不須要針對Spring進行編譯以充分利用該框架。mvc
另請注意,從Spring 3.2開始,不須要定義CGLIB依賴項(如今已升級到CGLIB 3.0) - 它已被從新打包(全部net.sf.cglib包如今是org.springframework.cglib)而且直接在內部內聯spring-core JAR(有關其餘詳細信息,請參閱JIRA)。框架
Maven配置Spring Persistencemaven
如今讓咱們看一下Spring Persistence依賴關係 - 主要是spring-orm:模塊化
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
複製代碼
這附帶了Hibernate和JPA支持 - 例如HibernateTemplate和JpaTemplate - 以及一些額外的,持久性相關的依賴項:spring-jdbc和spring-tx。測試
JDBC數據訪問庫定義了Spring JDBC支持以及JdbcTemplate,而spring-tx表明了極其靈活的事務管理抽象。
Maven配置Spring MVC
要使用Spring Web和Servlet支持,除了上面的核心依賴項外,還須要在pom中包含兩個依賴項:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
複製代碼
spring-web依賴項包含Servlet和Portlet環境的公共web特定實用程序,而spring-webmvc支持Servlet環境的MVC。
因爲spring-webmvc將spring-web做爲依賴項,所以在使用spring-webmvc時不須要明肯定義spring-web。
使用maven配置Spring Security
在使用Maven配置Spring Security文章中深刻討論了Maven配置Spring Security依賴關係。
使用Maven配置Spring Test
Spring Test Framework能夠經過如下依賴項包含在項目中:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
複製代碼
從Spring 3.2開始,Spring MVC Test項目已經包含在覈心測試框架中 - 所以包括spring-test依賴就足夠了。
使用Milestones
Spring的發佈版本託管在Maven Central上。可是,若是項目須要使用Milestones版本,則須要將自定義Spring存儲庫添加到pom中:
<repositories>
<repository>
<id>repository.springframework.maven.milestone</id>
<name>Spring Framework Maven Milestone Repository</name>
<url>http://repo.spring.io/milestone/</url>
</repository>
</repositories>
複製代碼
已定義了一個此存儲庫,該項目能夠定義依賴項,例如:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.0.RC2</version>
</dependency>
複製代碼
使用Snapshots
與Milestones相似,Snapshots託管在自定義存儲庫中:
<repositories>
<repository>
<id>repository.springframework.maven.snapshot</id>
<name>Spring Framework Maven Snapshot Repository</name>
<url>http://repo.spring.io/snapshot/</url>
</repository>
</repositories>
複製代碼
在pom.xml中啓用SNAPSHOT存儲庫後,能夠引用如下依賴項:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.3.0.BUILD-SNAPSHOT</version>
</dependency>
複製代碼
對於4.x:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.3.BUILD-SNAPSHOT</version>
</dependency>
複製代碼