本文介绍了Spring 4.1.0.RELEASE和Hibernate 4.3.6.Final的依赖问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用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抛出load()异常
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)
位于org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
位于org.springframework。
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
在org.springframework.web.servlet。
在org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
在org.springframework.web.servlet。 FrameworkServlet.initServletBe org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)处的
(位于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)
在org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
在org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
在org.apache .catalina.core.StandardContext.startInternal(StandardContext.java:5460)$ or
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)$ java.util.concurrent.ThreadPoolExecutor.runWorker
(Thr $ java.util.concurrent.ThreadPoolExecutor
$ Worker.run(ThreadPoolExecutor.java:615)$ b $ java.util.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>
< version> 0.0.1-SNAPSHOT< / version>
< name> spring Maven Webapp< / name>
< url> http://maven.apache.org< / url>
<依赖关系>
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.11< / version>
< scope> test< / scope>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-orm< / artifactId>
< version> $ {spring.version}< / version>
< type> jar< / type>
< scope>编译< / scope>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-expression< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework.data< / groupId>
< artifactId> spring-data-rest-webmvc< / artifactId>
< version> 2.2.0.RELEASE< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 4.3.6.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.apache.commons< / groupId>
< artifactId> commons-dbcp2< / artifactId>
< version> 2.0< / version>
< /依赖关系>
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-simple< / artifactId>
< version> 1.7.5< / version>
< /依赖关系>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.32< / version>
< /依赖关系>
< dependency>
< groupId> org.apache.logging.log4j< / groupId>
< artifactId> log4j-api< / artifactId>
< version> 2.0.2< / version>
< /依赖关系>
< dependency>
< groupId> org.apache.logging.log4j< / groupId>
< artifactId> log4j-core< / artifactId>
< version> 2.0.2< / version>
< /依赖关系>
< dependency>
< groupId> org.docx4j< / groupId>
< artifactId> docx4j< / artifactId>
< version> 3.0.0< / version>
< /依赖关系>
< /依赖关系>
< build>
< finalName> spring< / finalName>
< / build>
<属性>
< java-version> 1.7< / java-version>
< spring.version> 4.1.0.RELEASE< /spring.version>
< / properties>
< / project>

和其余的servlet.xml

 <?xml version =1.0encoding =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 =dataSourceclass =org.apache.commons.dbcp2.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / firsthibernate/>
< property name =usernamevalue =root/>
< property name =passwordvalue =/>
< / bean>

< bean id =sessionFactory
class =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocationvalue =classpath:hibernate.cfg.xml/>
< / bean>

< tx:annotation-driven />
< bean id =transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>


< bean id =elementDaoclass =dao.ElementDaoImpl>
< constructor-arg>
< ref bean =sessionFactory/>
< / constructor-arg>
< / bean>


解决方案

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

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

这就是我修正它的方法:

我运行了mvn依赖项:tree

  [INFO] --------- -------------------------------------------------- ------------- 
[INFO]构建MongoDb事实1.0-SNAPSHOT
[INFO] ----------------- -------------------------------------------------- -----
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree(default-cli)@mongodb-facts ---
[信息] 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] | | \\ a- aopalliance:aopalliance:jar:1.0:编译
[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:编译
[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:编译

您可以看到 spring-context spring-expression 库之间存在不匹配。


  1. 我明确地添加了spring-expression依赖项:

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


  2. 我从spring-data-mongodb中排除了spring-expression:

     < dependency> 
    < groupId> org.springframework.data< / groupId>
    < artifactId> spring-data-mongodb< / artifactId>
    < version> $ {spring-data-mongodb.version}< / version>
    <排除项>
    <排除>
    < groupId> org.springframework< / groupId>
    < artifactId> spring-expression< / artifactId>
    < /排除>
    < /排除>
    < /依赖关系>


现在当我运行 mvn依赖: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
| | \\ a- aopalliance:aopalliance:jar:1.0:编译
| + - org.springframework:spring-context:jar:4.1.2.RELEASE:compile
| \- org.springframework:spring-messaging:jar:4.1.2.RELEASE:compile
+ - org org.springframework:spring-messaging:jar:4.1.2.RELEASE:compile
+ - org.springframework:spring-expression: .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:编译


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

这篇关于Spring 4.1.0.RELEASE和Hibernate 4.3.6.Final的依赖问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:32