2010-02-10 46 views
111

我想用Spring 3.0(和maven)做我的第一個項目。我在一些項目中一直使用Spring 2.5(和入門版本)。不過,我有點困惑,我必須在我的pom.xml中定義爲依賴關係的模塊。我只想使用核心容器函數(bean,core,context,el)。哪些maven依賴包含在spring 3.0中?

我已經習慣這樣做:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring</artifactId> 
    <version>2.5.6</version> 
</dependency> 

但現在我有點困惑,因爲有3.0的版本沒有完整包裝的彈簧組件了。我嘗試了以下,但它沒有工作(一些類缺失)。

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>3.0.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-expression</artifactId> 
     <version>3.0.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>3.0.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>3.0.0.RELEASE</version> 
    </dependency> 

任何幫助,將不勝感激!

+0

它實際上按照第二個代碼示例中的規定工作。我對其他一些問題感到困惑。對不起,沒有必要的問題。無論如何,我會建議保留這個問題,也許人們會遇到同樣的問題,從2.5切換到3.0。 – 2010-02-10 15:13:36

回答

218

有一個非常不錯的職位上Spring Blog從基思·唐納德細節HOWTO Obtain Spring 3 Aritfacts with Maven,有詳細註釋時你需要每個依賴關係......

<!-- Shared version number properties --> 
<properties> 
    <org.springframework.version>3.0.0.RELEASE</org.springframework.version> 
</properties> 
<!-- Core utilities used by other modules. 
    Define this if you use Spring Utility APIs 
    (org.springframework.core.*/org.springframework.util.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Expression Language (depends on spring-core) 
    Define this if you use Spring Expression APIs 
    (org.springframework.expression.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-expression</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Bean Factory and JavaBeans utilities (depends on spring-core) 
    Define this if you use Spring Bean APIs 
    (org.springframework.beans.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-beans</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Aspect Oriented Programming (AOP) Framework 
    (depends on spring-core, spring-beans) 
    Define this if you use Spring AOP APIs 
    (org.springframework.aop.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-aop</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Application Context 
    (depends on spring-core, spring-expression, spring-aop, spring-beans) 
    This is the central artifact for Spring's Dependency Injection Container 
    and is generally always defined--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Various Application Context utilities, including EhCache, JavaMail, Quartz, 
    and Freemarker integration 
    Define this if you need any of these integrations--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context-support</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Transaction Management Abstraction 
    (depends on spring-core, spring-beans, spring-aop, spring-context) 
    Define this if you use Spring Transactions or DAO Exception Hierarchy 
    (org.springframework.transaction.*/org.springframework.dao.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-tx</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- JDBC Data Access Library 
    (depends on spring-core, spring-beans, spring-context, spring-tx) 
    Define this if you use Spring's JdbcTemplate API 
    (org.springframework.jdbc.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-jdbc</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA and iBatis. 
    (depends on spring-core, spring-beans, spring-context, spring-tx) 
    Define this if you need ORM (org.springframework.orm.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-orm</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, 
    Castor, XStream, and XML Beans. 
    (depends on spring-core, spring-beans, spring-context) 
    Define this if you need OXM (org.springframework.oxm.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-oxm</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Web application development utilities applicable to both Servlet and 
    Portlet Environments 
    (depends on spring-core, spring-beans, spring-context) 
    Define this if you use Spring MVC, or wish to use Struts, JSF, or another 
    web framework with Spring (org.springframework.web.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Spring MVC for Servlet Environments 
    (depends on spring-core, spring-beans, spring-context, spring-web) 
    Define this if you use Spring MVC with a Servlet Container such as 
    Apache Tomcat (org.springframework.web.servlet.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Spring MVC for Portlet Environments 
    (depends on spring-core, spring-beans, spring-context, spring-web) 
    Define this if you use Spring MVC with a Portlet Container 
    (org.springframework.web.portlet.*)--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc-portlet</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 
<!-- Support for testing Spring applications with tools such as JUnit and TestNG 
    This artifact is generally always defined with a 'test' scope for the 
    integration testing framework and unit testing stubs--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-test</artifactId> 
    <version>${org.springframework.version}</version> 
    <scope>test</scope> 
</dependency> 
+3

謝謝,實際上不得不再次尋找它,因爲我發現並在幾天前失去了鏈接..在這裏意味着它可能比春季博客更容易找到人。 – Tim 2010-02-10 15:49:48

+0

對於Spring 4 [考慮切換到Spring Boot](http://stackoverflow.com/a/30644556/53444) – Tim 2015-06-04 13:03:30

1

缺少哪些類?班級名稱本身應該是缺失模塊的一個很好的線索。我知道它包括超級彈簧罐非常方便,但是這與其他項目集成時真的會引起問題。依賴系統背後的好處之一是它可以解決依賴關係之間的版本衝突。

如果我的庫依賴於spring-core:2.5,並且依賴於我的庫和uber-spring:3.0,那麼現在在類路徑中有2個spring版本。

你可以用排除來解決這個問題,但它更容易正確列出依賴關係,而不必擔心它。

2
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc</artifactId> 
    <version>3.0.0.RELEASE</version> 
</dependency> 
27

春季(現在)讓只需使用一個依賴項就可以輕鬆地將Spring添加到項目中,例如

<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-context</artifactId> 
<version>3.1.2.RELEASE</version> 
</dependency> 

這將解決以

[INFO] The following files have been resolved: 
[INFO] aopalliance:aopalliance:jar:1.0:compile 
[INFO] commons-logging:commons-logging:jar:1.1.1:compile 
[INFO] org.springframework:spring-aop:jar:3.1.2.RELEASE:compile 
[INFO] org.springframework:spring-asm:jar:3.1.2.RELEASE:compile 
[INFO] org.springframework:spring-beans:jar:3.1.2.RELEASE:compile 
[INFO] org.springframework:spring-context:jar:3.1.2.RELEASE:compile 
[INFO] org.springframework:spring-core:jar:3.1.2.RELEASE:compile 
[INFO] org.springframework:spring-expression:jar:3.1.2.RELEASE:compile 

看一看在Spring Framework documentation頁面瞭解更多信息。

+0

如果您在項目中也有Spring Security,那麼這種方法可能會有問題,因爲Maven依賴解決方案作品(最短路徑) - 查看我的[Spring Security with Maven](http://www.baeldung.com/spring-security-with-maven)文章 – Eugen 2013-05-08 13:02:35

+1

@Eugen好點。在這種情況下,更好的辦法是聲明[spring-security artifacts](http://www.springsource.org/spring-security#documentation),它可以解決受支持的「spring-core」工件的正確版本不幸的是不是最新的穩定版本)。 – FrVaBe 2013-05-08 14:09:13

0

你可以試試這個

<dependencies> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>3.1.0.RELEASE</version> 
    </dependency> 
</dependencies>` 
2

因爲這個問題似乎仍然得到了相當多的意見,可能要注意,對於春天有用4+最簡單的方法開始使用Spring BootSpring Boot starter POMs

使用Spring Boot,管理的依賴性較少(因此衝突較少),並且設置一個工作良好的集成的Spring Context變得非常容易。我強烈推薦它。

相關問題