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

2022-09-02 03:30:42

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

下面是错误代码:

    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)

这是我的绒球.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>

和其余的服务.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>


答案 1

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

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

这就是我如何修复它:

我运行了 mvn 依赖项:树

[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

如您所见,spring 上下文spring 表达式库之间存在不匹配。

  1. 我显式添加了 spring 表达式依赖项:

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

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

现在当我运行mvn依赖关系时:树

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

答案 2

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

尝试检查生成的文件,以查看是否在项目中的其他位置使用了 另一个版本。mvn dependency:tree > tree.txtspring-context


推荐