简体   繁体   中英

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

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?

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

    <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

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

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

[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-context and the spring-expression libraries.

  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> 

And now when I run 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

I had a similar problem and I was able to resolve it by explicitly managing the spring-context dependency. 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.

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

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

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

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 diagnostic why one dependency was chosen over one another. Dependency Hierarchy tab http://i61.tinypic.com/21kdloy.png
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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM