【问题标题】:Which maven dependencies to include for spring 3.0?spring 3.0 包含哪些 maven 依赖项?
【发布时间】:2011-01-15 07:17:27
【问题描述】:

我正在尝试使用 Spring 3.0(和 maven)做我的第一个项目。我在相当多的项目中一直在使用 Spring 2.5(和入门版本)。尽管如此,我还是有点困惑,我必须在 pom.xml 中将哪些模块定义为依赖项。我只想使用核心容器函数(beans、core、context、el)。

我曾经这样做过:

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

但现在我有点困惑,因为 3.0 版已经没有完整的 spring 模块了。我尝试了以下方法,但没有成功(缺少某些类)。

    <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>

任何帮助将不胜感激!

【问题讨论】:

  • 它实际上按照第二个代码示例中的说明工作。我对其他一些问题感到困惑。对不起,不必要的问题。我还是建议保留这个问题,因为也许人们从 2.5 切换到 3.0 时会遇到同样的问题。

标签: java spring maven-2 dependencies


【解决方案1】:

缺少哪些课程?类名本身应该是缺少模块的一个很好的线索。

仅供参考,我知道包含 uber spring jar 非常方便,但这在与其他项目集成时确实会导致问题。依赖系统背后的好处之一是它可以解决依赖之间的版本冲突。

如果我的库依赖于 spring-core:2.5 而你依赖于我的库和 uber-spring:3.0,那么你的类路径中现在有 2 个版本的 spring。

您可以通过排除来解决此问题,但正确列出依赖项要容易得多,而不必担心。

【讨论】:

    【解决方案2】:

    Keith Donald 在Spring Blog 上发表了一篇非常好的帖子,详细说明了如何使用Obtain Spring 3 Aritfacts with Maven,其中 cmets 详细说明了您何时需要每个依赖项...

    <!-- 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>
    

    【讨论】:

    • 谢谢,实际上不得不再次寻找它,因为我几天前发现并丢失了链接。拥有它意味着人们可能比 Spring 博客更容易找到它。.跨度>
    【解决方案3】:
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.0.RELEASE</version>
    </dependency>
    

    【讨论】:

      【解决方案4】:

      Spring(现在)可以很容易地通过仅使用一个依赖项将 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 页面了解更多信息。

      【讨论】:

      • 如果项目中也有 Spring Security,这种方法可能会有问题,因为 Maven 依赖解析的工作方式(最短路径) - 查看我的 Spring Security with Maven 文章
      • @Eugen 好点。在这种情况下,最好只声明spring-security artifacts,它使用正确的版本解析受支持的“spring-core”工件(不幸的是不是最新的稳定版本)。
      【解决方案5】:

      你可以试试这个

      <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>`
      

      【讨论】:

        【解决方案6】:

        由于这个问题似乎仍然有很多观点,请注意对于 Spring 4+,最容易开始使用 Spring BootSpring Boot starter POMs

        使用 Spring Boot,需要管理的依赖项更少(因此冲突也更少),并且设置一个工作良好、集成良好的 Spring 上下文要容易得多。我强烈推荐它。

        【讨论】:

          【解决方案7】:

          您可以为 spring jar 添加 spring-context 依赖项。您将获得以下罐子。

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

          如果你也想要 web 组件,你可以使用 spring-webmvc 依赖。

          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              <version>5.0.5.RELEASE</version>
          </dependency>
          

          你可以使用任何你想要的版本。我在这里使用了 5.0.5.RELEASE。

          【讨论】:

            【解决方案8】:

            使用 BOM 解决版本问题。

            您可能会发现第三方库或其他 Spring 项目, 引入对旧版本的传递依赖。如果你忘记 自己显式声明一个直接依赖,各种 可能会出现意想不到的问题。

            为了克服这些问题,Maven 支持“bill of the 材料”(BOM)依赖关系。

            https://docs.spring.io/spring/docs/4.3.18.RELEASE/spring-framework-reference/html/overview.html#overview-maven-bom

            <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-framework-bom</artifactId>
              <version>3.2.12.RELEASE</version>
              <type>pom</type>
            </dependency>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-11-11
              • 1970-01-01
              • 1970-01-01
              • 2017-01-23
              • 2011-08-26
              • 1970-01-01
              • 2017-11-09
              • 1970-01-01
              相关资源
              最近更新 更多