Java Spring 4.1.0.RELEASE 和 Hibernate 4.3.6.Final 的依赖问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25991831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:38:40  来源:igfitidea点击:

Problems with Dependencies for Spring 4.1.0.RELEASE and Hibernate 4.3.6.Final

javaspringhibernatemavenjpa

提问by Iceman

i want to build a RESTful service with Spring and Hibernate (and Maven). I used Spring version 4.0.3 first and everything works. But i wanted to use the newest version 4.1.0 and changed my pom.xml accordingly. But then i get an error when starting. Can you tell how the pom.xml should look like? What is the best way to create a RESTful service with spring? I found an example which uses the spring-data-rest-webmvc and i am working with this. Is there a better way?

我想用 Spring 和 Hibernate(和 Maven)构建一个 RESTful 服务。我首先使用了 Spring 4.0.3 版,一切正常。但我想使用最新版本 4.1.0 并相应地更改了我的 pom.xml。但是然后我在启动时遇到错误。你能说出 pom.xml 应该是什么样子吗?使用 spring 创建 RESTful 服务的最佳方法是什么?我找到了一个使用 spring-data-rest-webmvc 的示例,我正在使用它。有没有更好的办法?

Here is the error code:

这是错误代码:

    AM org.apache.catalina.core.StandardContext loadOnStartup
    Schwerwiegend: Servlet /spring threw load() exception
    java.lang.NoSuchMethodError: 
    org.springframework.expression.spel.SpelParserConfiguration.<init>
    (Lorg/springframework/expression/spel/SpelCompilerMode;Ljava/lang/ClassLoader;)V
at org.springframework.context.expression.StandardBeanExpressionResolver.<init>(StandardBeanExpressionResolver.java:98)
at        org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:553)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

Here my pom.xml

这是我的 pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>de.rwth-aachen.prime</groupId>
      <artifactId>spring</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>spring Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
              <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
              <groupId>org.springframework.data</groupId>
              <artifactId>spring-data-rest-webmvc</artifactId>
              <version>2.2.0.RELEASE</version>
         </dependency>
         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0</version>
        </dependency>
        <!-- Hibernate uses slf4j for logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.5</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.32</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.0.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.0.2</version>
            </dependency>
            <dependency>
                <groupId>org.docx4j</groupId>
                <artifactId>docx4j</artifactId>
                <version>3.0.0</version>
            </dependency>
      </dependencies>
      <build>
        <finalName>spring</finalName>
      </build>
        <properties>
            <java-version>1.7</java-version>
            <spring.version>4.1.0.RELEASE</spring.version>
        </properties>
    </project>

and the rest-servlet.xml

和 rest-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd">
        <context:component-scan base-package="spring" />
<mvc:annotation-driven />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/firsthibernate"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


<bean id="elementDao" class="dao.ElementDaoImpl">
<constructor-arg>
    <ref bean="sessionFactory" />
</constructor-arg>
</bean>

回答by burcakulug

I had a similar problem and I was able to resolve it by explicitly managing the spring-contextdependency. One of the modules was using the wrong version of that. I see you already have a dependency management for that, but it won't hurt to double check if that is somehow overridden by another lower level project.

我有一个类似的问题,我能够通过显式管理spring-context依赖项来解决它。其中一个模块使用了错误的版本。我看到你已经有了一个依赖管理,但仔细检查它是否被另一个较低级别的项目以某种方式覆盖不会有什么坏处。

try mvn dependency:tree > tree.txtand check the generated file to see if another version of spring-contextis used somewhere else in the project.

尝试mvn dependency:tree > tree.txt检查生成的文件,看看是否spring-context在项目的其他地方使用了另一个版本。

回答by Vlad Mihalcea

In my current project I was having:

在我目前的项目中,我有:

<spring.version>4.1.2.RELEASE</spring.version>
<spring-data-mongodb.version>1.6.1.RELEASE</spring-data-mongodb.version>

This is how I fixed it:

这是我修复它的方式:

I ran mvn dependency:tree

我跑了mvn dependency:tree

[INFO] ------------------------------------------------------------------------
[INFO] Building MongoDb Facts 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mongodb-facts ---
[INFO] com.vladmihalcea:mongodb-facts:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-jms:jar:4.1.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.1.2.RELEASE:compile
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-context:jar:4.1.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-messaging:jar:4.1.2.RELEASE:compile
...
[INFO] +- org.springframework.data:spring-data-mongodb:jar:1.6.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.0.7.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.9.1.RELEASE:compile
[INFO] |  \- org.mongodb:mongo-java-driver:jar:2.12.3:compile

As you can see there's a mismatch between the spring-contextand the spring-expressionlibraries.

正如您所看到的,spring-contextspring-expression库之间存在不匹配。

  1. I explicitly added the spring-expression dependency:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
  2. And I excluded the spring-expression from spring-data-mongodb:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>${spring-data-mongodb.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  1. 我明确添加了 spring-expression 依赖项:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
  2. 我从 spring-data-mongodb 中排除了 spring-expression:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>${spring-data-mongodb.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

And now when I run mvn dependency:tree

现在当我运行mvn dependency:tree

--- maven-dependency-plugin:2.8:tree (default-cli) @ mongodb-facts ---
com.vladmihalcea:mongodb-facts:jar:1.0-SNAPSHOT
+- org.springframework:spring-jms:jar:4.1.2.RELEASE:compile
|  +- org.springframework:spring-aop:jar:4.1.2.RELEASE:compile
|  |  \- aopalliance:aopalliance:jar:1.0:compile
|  +- org.springframework:spring-context:jar:4.1.2.RELEASE:compile
|  \- org.springframework:spring-messaging:jar:4.1.2.RELEASE:compile
+- org.springframework:spring-expression:jar:4.1.2.RELEASE:compile
+- org.springframework.data:spring-data-mongodb:jar:1.6.1.RELEASE:compile
|  +- org.springframework.data:spring-data-commons:jar:1.9.1.RELEASE:compile
|  \- org.mongodb:mongo-java-driver:jar:2.12.3:compile

回答by magiccrafter

Try adding this dependency in your pom.xml file if you plan using springframework project version => 4.1.0

如果您计划使用 springframework 项目版本 => 4.1.0,请尝试在 pom.xml 文件中添加此依赖项

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.2.0.RELEASE</version>
</dependency>

回答by Rudy Vissers

I would like to add the following over what was written by vlad-mihalcea.
1) If you open the tab Dependency Hierarchy into your Eclipse you will get some diagnosticwhy one dependency was chosen over one another. Dependency Hierarchy tab
It was my problem. Because of an in house artifact, spring-expression 3.2.8.RELEASE was chosen over 4.1.6.RELEASE and my Spring Boot server didn't start anymore! You read 'omitted for conflict'.
2) Using the tag 'exclusion' is NOT the only possibility. You can move the dependency down the POM (in my case utilities) and in this case it is the correct version of expression that was chosen. 3.2.8.RELEASE was discarded in favor of 4.1.6.RELEASE.

我想在 vlad-mihalcea 所写的内容上添加以下内容。
1) 如果您在 Eclipse 中打开选项卡 Dependency Hierarchy,您将获得一些诊断信息,为什么选择了一个依赖项而不是另一个。 这是我的问题。由于内部工件,spring-expression 3.2.8.RELEASE 被选择而不是 4.1.6.RELEASE 并且我的 Spring Boot 服务器不再启动!你读到“因冲突而省略”。 2)使用标签“排除”不是唯一的可能性。您可以将依赖项向下移动到 POM(在我的例子中是实用程序),在这种情况下,它是选择的正确版本的表达式。3.2.8.RELEASE 被弃用,取而代之的是 4.1.6.RELEASE。依赖层次结构选项卡

回答by Anil

You might be missing the spring-expression..jar configuration in POM file. add the dependency in pom file.

您可能缺少 POM 文件中的 spring-expression..jar 配置。在 pom 文件中添加依赖项。

<dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-expression</artifactId>
         <version>${spring.version}</version> 
</dependency>