Java 创建名为“org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”的bean时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40381278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:39:26  来源:igfitidea点击:

Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping'

javaspringspring-mvcspring-data-jpaspring-annotations

提问by kopylov

Use jdk 1.8

使用 jdk 1.8

  1. SpringMVC-4.3.3
  2. SpringCore-4.3.3
  3. javax.servlet-api-3.0.1
  4. spring-data-jpa:1.10.4
  5. hibernate-entitymanager:4.2.5.Final
  6. hibernate-core:4.2.5.Final
  7. Simple Logging Facade for Java:1.6.1
  1. SpringMVC-4.3.3
  2. SpringCore-4.3.3
  3. javax.servlet-api-3.0.1
  4. spring-data-jpa:1.10.4
  5. hibernate-entitymanager:4.2.5.Final
  6. 休眠核心:4.2.5.Final
  7. Java的简单日志外观:1.6.1

I encountered with such error

我遇到了这样的错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation

org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”的bean时出错:init方法调用失败;嵌套异常是 java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation

My question:
Why error encoured and how fix it?

我的问题
为什么会出现错误以及如何解决它?

web.xml

网页.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
<servlet>
    <servlet-name>product</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>product</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

product-servlet.xml

产品servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.kopylov.spring.controller" />
<mvc:annotation-driven/>

Controller

控制器

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ProductController {

@RequestMapping
public String showHome(){
    return "home";
}

}

}

AppInitializer

应用初始化器

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

公共类 AppInitializer 扩展 AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses(){
    return new Class<?>[]{
            DataConfig.class
    };
}

@Override
protected Class<?>[] getServletConfigClasses(){
    return new Class<?>[0];
}

@Override
protected String[] getServletMappings(){
    return new String[0];
}

}

}

DataConfig

数据配置

@Configuration
@EnableTransactionManagement
@ComponentScan("com.kopylov.spring")
@PropertySource("classpath:/com/kopylov/spring/resources/app.properties")
@EnableJpaRepositories("com.kopylov.spring.repository")
public class DataConfig {
private static final String PROP_DATABASE_DRIVER = "db.driver";
private static final String PROP_DATABASE_PASSWORD = "db.password";
private static final String PROP_DATABASE_URL = "db.url";
private static final String PROP_DATABASE_USERNAME = "db.username";
private static final String PROP_HIBERNATE_DIALECT = "db.hibernate.dialect";
private static final String PROP_HIBERNATE_SHOW_SQL = "db.hibernate.dialect";
private static final String PROP_ENTITYMANAGER_PACKAGES_TO_SCAN = "db.entitymanager.packages.to.scan";
private static final String PROP_HIBERNATE_HBM2DDL_AUTO = "db.hibernate.hbm2ddl.auto";

@Resource
private Environment env;

@Bean
public DataSource dataSource(){
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getRequiredProperty(PROP_DATABASE_DRIVER));
    dataSource.setUrl(env.getRequiredProperty(PROP_DATABASE_URL));
    dataSource.setUsername(env.getRequiredProperty(PROP_DATABASE_USERNAME));
    dataSource.setPassword(env.getRequiredProperty(PROP_DATABASE_PASSWORD));

    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
    LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
    emfb.setDataSource(dataSource());
    emfb.setPersistenceProviderClass(HibernatePersistence.class);
    emfb.setPackagesToScan(env.getRequiredProperty(PROP_ENTITYMANAGER_PACKAGES_TO_SCAN));
    emfb.setJpaProperties(getHibernateProperties());
    return emfb;
}

@Bean
public JpaTransactionManager transactionManager(){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}

private Properties getHibernateProperties(){
    Properties properties = new Properties();
    properties.put(PROP_HIBERNATE_DIALECT, env.getRequiredProperty(PROP_HIBERNATE_DIALECT));
    properties.put(PROP_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROP_HIBERNATE_SHOW_SQL));
    properties.put(PROP_HIBERNATE_HBM2DDL_AUTO, env.getRequiredProperty(PROP_HIBERNATE_HBM2DDL_AUTO));
    return properties;
}

回答by FanOfSkynyrd

I dont think your dispatcher is configured correctly in your web.xml. You do not have any <init-param>tags. See docs

我不认为您的调度程序在您的 web.xml 中配置正确。您没有任何<init-param>标签。查看文档

http://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.html

http://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.html

Try editing to:

尝试编辑:

<servlet> <servlet-name>product</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/product-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

<servlet> <servlet-name>product</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/product-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

EDIT for additional infoAlso, I believe you will need to add some mapping to your @RequestMapping annotation, see link:

编辑其他信息此外,我相信您需要在@RequestMapping 注释中添加一些映射,请参阅链接:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping

回答by Chandana Kumara

First clean your project and build again. Check whether you are getting this error.


Then Check with this: nested exception is java.lang.NoSuchMethodError: 
org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation

I think you have missed some repositories in pom.xml or multiple jars of different versions in your pom.xml. Sometimes your spring jars not match mutually with each other.

我认为您错过了 pom.xml 中的一些存储库或 pom.xml 中不同版本的多个 jar。有时您的弹簧罐彼此不匹配。

Add this:

添加这个:

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

Remove 4.3.3.RELEASE from all spring dependencies.

从所有 spring 依赖项中删除 4.3.3.RELEASE。

Refer: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html(Read: Maven "Bill Of Materials" Dependency)

参考:http: //docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html(阅读:Maven“材料清单”依赖)

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

回答by kuhajeyan

Probably you have forgot to import your product-serlet to configuration class

可能您忘记将 product-serlet 导入到配置类

you could move your product-servlet.xml to 'resources' so that it is available in classpath

您可以将您的 product-servlet.xml 移动到“资源”,以便它在类路径中可用

so your servlet mapping looks like this

所以你的servlet映射看起来像这样

<servlet>  
        <servlet-name>product</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/product-servlet.xml</param-value>
         </init-param>  
        <load-on-startup>1</load-on-startup>  
</servlet> 
<servlet-mapping>
    <servlet-name>product</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

in your config

在你的配置中

 @Configuration
    @EnableTransactionManagement
    @ComponentScan("com.kopylov.spring")
    @PropertySource("classpath:/com/kopylov/spring/resources/app.properties")
    @EnableJpaRepositories("com.kopylov.spring.repository")
    @ImportResource("classpath:/product-servlet.xml")
    public class DataConfig {
    //..
    }

BTW, better not to mix match xml and java configuration. As java config has become norm, you could use WebMvcConfigurerAdapter

顺便说一句,最好不要混合匹配 xml 和 java 配置。由于 java 配置已成为常态,您可以使用WebMvcConfigurerAdapter

http://www.mkyong.com/spring-mvc/gradle-spring-4-mvc-hello-world-example-annotation/

http://www.mkyong.com/spring-mvc/gradle-spring-4-mvc-hello-world-example-annotation/

回答by kupendra pola

First check if all of the build path errors are resolved or not. In most of this kind of cases spring initializer throws this error if build path errors are not resolved.

首先检查所有构建路径错误是否已解决。在大多数这种情况下,如果未解决构建路径错误,spring 初始化程序会抛出此错误。

回答by DollyShukla

Perfect solution worked for me :

完美的解决方案对我有用:

Add commons-configuration-1.6.jar to the lib directory

将 commons-configuration-1.6.jar 添加到 lib 目录