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

时间:2014-09-23 09:42:59

标签: java spring hibernate maven jpa

我想用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)

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

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

4 个答案:

答案 0 :(得分:28)

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

<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-context spring-expression 库之间存在不匹配。

  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>
    
  3. 现在当我运行 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
    

答案 1 :(得分:3)

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

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

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

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