RequestMappingHandlerAdapter':init方法的调用失败;嵌套异常是java.lang.NoSuchMethodError

时间:2016-05-25 18:57:34

标签: java spring

突然间我得到了:

[localhost-startStop-1] WARN  org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.accept.ContentNegotiationManager.getStrategies()Ljava/util/List;
[localhost-startStop-1] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.accept.ContentNegotiationManager.getStrategies()Ljava/util/List;
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

我不知道我做了什么/改变了 - 当我启动服务器并请求我的网站时,它突然来了。

您可以看到full stack trace with all exceptions here

有什么猜测会突然造成什么?

我的 pom.xml (缩写为基本依赖项):

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- ... -->

    <properties>
        <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
        <org.springframework.security-version>4.0.3.RELEASE</org.springframework.security-version>
    </properties>

    <!-- ... -->

    <dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

        <!-- Spring Dependencies -->

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

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

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

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

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


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

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

        <!-- Required by spring-security-acl -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.security-version}</version>
        </dependency>

    </dependencies>

    <profiles>
        <!-- ... -->
    </profiles>

    <build>
        <plugins>
            <!-- ... -->
        </plugins>
    </build>
</project>

1 个答案:

答案 0 :(得分:3)

如果您在项目中使用maven进行依赖关系管理,那么您必须至少遇到一次问题或者可能不止这样。问题是版本不匹配。它通常发生在你有一些依赖关系,它将相关的依赖关系与某个版本结合在一起。如果您已经包含了具有不同版本号的依赖项,那么它们在编译时和运行时也会遇到不希望的结果。

理想情况下,为了避免上述问题,您需要明确排除相关的依赖关系,但很可能您忘记这样做。

要解决版本不匹配问题,您可以使用“物料清单”(BOM)依赖关系的概念。 BOM依赖关系跟踪版本号并确保所有依赖项(直接和传递)都处于同一版本。

如何添加BOM [物料清单]依赖

Maven为此提供了标签依赖性管理。您需要在此标记中添加bom信息,如下所示。我以Spring bom文件为例。

 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.2.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
  </dependencyManagement>

使用BOM的另一个好处是,在依赖Spring Framework工件时,您不再需要指定版本属性。所以它会完美无缺。

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
    </dependency>
<dependencies>

类似于使用Spring安全性的BOM是

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-bom</artifactId>
    <version>4.0.3.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

在您的情况下,我之前遇到的版本不匹配问题