org.springframework.beans.factory.BeanCreationException:创建定义名称“requestMappingHandlerMapping”的bean时出错

Posted

技术标签:

【中文标题】org.springframework.beans.factory.BeanCreationException:创建定义名称“requestMappingHandlerMapping”的bean时出错【英文标题】:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined 【发布时间】:2016-06-21 09:54:46 【问题描述】:

我正在开发 Spring MVC,Spring Security 示例,在开发此代码时遇到以下错误:花费了很多天后,我仍然无法解决此错误。请尽快指导我。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]: Factory method 'requestMappingHandlerMapping' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/MethodIntrospector$MetadataLookup
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835) ~[spring-context-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446) [spring-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328) [spring-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) [spring-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016) [catalina.jar:7.0.59]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5524) [catalina.jar:7.0.59]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.59]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) [catalina.jar:7.0.59]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) [catalina.jar:7.0.59]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_45]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_45]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]

依赖树:

[INFO] ------------------------------------------------------------------------
[INFO] Building spring-security-mvc-persisted-remember-me 1.0
[INFO] ------------------------------------------------------------------------
[WARNING] The artifact junit:junit-dep:jar:4.11 has been relocated to junit:junit:jar:4.11
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ spring-security-mvc-persisted-remember-me ---
[WARNING] The artifact junit:junit-dep:jar:4.11 has been relocated to junit:junit:jar:4.11
[INFO] org.baeldung:spring-security-mvc-persisted-remember-me:war:1.0
[INFO] +- org.springframework.security:spring-security-web:jar:4.0.3.RELEASE:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework.security:spring-security-core:jar:4.0.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.2.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.2.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.2.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.2.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:4.2.2.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-config:jar:4.0.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-aop:jar:4.2.2.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-taglibs:jar:4.0.3.RELEASE:compile
[INFO] |  \- org.springframework.security:spring-security-acl:jar:4.0.3.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:4.2.4.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:4.2.4.RELEASE:compile
[INFO] +- org.springframework:spring-tx:jar:4.2.4.RELEASE:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.0.1:provided
[INFO] +- javax.servlet:jstl:jar:1.2:runtime
[INFO] +- com.h2database:h2:jar:1.4.190:compile
[INFO] +- postgresql:postgresql:jar:9.1-901.jdbc4:runtime
[INFO] +- com.google.guava:guava:jar:19.0:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.12:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] |  \- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile
[INFO] +- org.slf4j:log4j-over-slf4j:jar:1.7.12:compile
[INFO] +- junit:junit:jar:4.11:test
[INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] \- org.mockito:mockito-core:jar:1.10.19:test
[INFO]    \- org.objenesis:objenesis:jar:2.1:test
[INFO] ------------------------------------------------------------------------

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.baeldung</groupId>
    <artifactId>spring-security-mvc-persisted-remember-me</artifactId>
    <version>1.0</version>
    <name>spring-security-mvc-persisted-remember-me</name>
    <packaging>war</packaging>


    <properties>
        <!-- Spring -->
        <org.springframework.version>4.2.4.RELEASE</org.springframework.version>
        <org.springframework.security.version>4.0.3.RELEASE</org.springframework.security.version>
        <java.version>1.8</java.version>

        <!-- persistence -->
        <hibernate.version>4.3.11.Final</hibernate.version>
        <mysql-connector-java.version>5.1.37</mysql-connector-java.version>
        <h2.version>1.4.190</h2.version>
        <postgresql.version>9.1-901.jdbc4</postgresql.version>


        <!-- logging -->
        <org.slf4j.version>1.7.12</org.slf4j.version>
        <logback.version>1.1.3</logback.version>

        <!-- various -->
        <hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
        <javax.servlet.version>3.0.1</javax.servlet.version>
        <jstl.version>1.2</jstl.version>

        <!-- util -->
        <guava.version>19.0</guava.version>
        <commons-lang3.version>3.4</commons-lang3.version>

        <!-- testing -->
        <org.hamcrest.version>1.3</org.hamcrest.version>
        <junit.version>4.11</junit.version>
        <mockito.version>1.10.19</mockito.version>

        <httpclient.version>4.5</httpclient.version>
        <httpcore.version>4.4.1</httpcore.version>

        <rest-assured.version>2.4.1</rest-assured.version>

        <!-- Maven plugins -->
        <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
        <maven-war-plugin.version>2.5</maven-war-plugin.version>
        <maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
        <maven-resources-plugin.version>2.7</maven-resources-plugin.version>
        <cargo-maven2-plugin.version>1.4.11</cargo-maven2-plugin.version>

    </properties>

    <dependencies>

        <!-- Spring Security -->

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</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-taglibs</artifactId>
            <version>$org.springframework.security.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>$org.springframework.version</version>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$org.springframework.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>$org.springframework.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>$org.springframework.version</version>
        </dependency>


        <!-- web -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>$javax.servlet.version</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>$jstl.version</version>
            <scope>runtime</scope>
        </dependency>

        <!-- persistence -->

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>$h2.version</version>
        </dependency>

        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>$postgresql.version</version>
            <scope>runtime</scope>
        </dependency>

        <!-- utils -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>$guava.version</version>
        </dependency>


        <!-- logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>$org.slf4j.version</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>$logback.version</version>
            <!-- <scope>runtime</scope> -->
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>$org.slf4j.version</version>
        </dependency>

        <dependency> 
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>$org.slf4j.version</version>
        </dependency>

        <!-- test scoped -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit-dep</artifactId>
            <version>$junit.version</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>$org.hamcrest.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>$org.hamcrest.version</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>$mockito.version</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>spring-security-mvc-persisted-remember-me</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>$maven-compiler-plugin.version</version>
                <configuration>
                    <source>$java.version</source>
                    <target>$java.version</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>$maven-war-plugin.version</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>$maven-surefire-plugin.version</version>
                <configuration>
                    <excludes>
                        <!-- <exclude>**/*ProductionTest.java</exclude> -->
                    </excludes>
                    <systemPropertyVariables>
                        <!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
                    </systemPropertyVariables>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>$cargo-maven2-plugin.version</version>
                <configuration>
                    <wait>true</wait>
                    <container>
                        <containerId>jetty8x</containerId>
                        <type>embedded</type>
                        <systemProperties>
                            <!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
                        </systemProperties>
                    </container>
                    <configuration>
                        <properties>
                            <cargo.servlet.port>8082</cargo.servlet.port>
                        </properties>
                    </configuration>
                </configuration>
            </plugin>

        </plugins>

    </build>
</project>

【问题讨论】:

【参考方案1】:

看起来 spring-webmvc 所需的缺失类自 spring-core 版本 4.2.3 (https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/MethodIntrospector.html) 起可用,但您使用的是 4.2.2。 尝试使用版本 4.2.3 添加显式版本的 spring-core 或尝试更新版本的 spring-security-web 4.0.4.RELEASE。本版本使用4.2.5版本的spring-core。

【讨论】:

如果我使用 spring-boot-dependencies,版本 1.4.1.RELEASE?

以上是关于org.springframework.beans.factory.BeanCreationException:创建定义名称“requestMappingHandlerMapping”的bean时出错的主要内容,如果未能解决你的问题,请参考以下文章

REST API:org.springframework.beans.factory.UnsatisfiedDependencyException:

Spring security-org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.sec

org.springframework.beans.factory.UnsatisfiedDependencyException:

没有实现 [org.springframework.beans.factory.xml.NamespaceHandler] 接口

Spring Boot 错误 org.springframework.beans.factory.UnsatisfiedDependencyException

org.springframework.beans.factory.UnsatisfiedDependencyException:创建 bean 时出错