创建名为'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping'

时间:2016-11-02 13:33:21

标签: java spring spring-mvc spring-data-jpa spring-annotations

使用jdk 1.8

  1. 用SpringMVC-4.3.3
  2. SpringCore-4.3.3
  3. 的javax.servlet-API-3.0.1
  4. 弹簧数据JPA:1.10.4
  5. 冬眠-的EntityManager:4.2.5.Final
  6. 冬眠核:4.2.5.Final
  7. Java简单日志外观:1.6.1
  8. 我遇到了这样的错误

      

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

    我的问题
    为什么错误鼓励以及如何解决它?

    的web.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>
    

    产物-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/>
    

    控制器

    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扩展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;
    }
    

5 个答案:

答案 0 :(得分:1)

您可能忘记将产品系列导入配置类

您可以将product-servlet.xml移动到'resources',以便在classpath中可用

所以你的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>

在您的配置中

 @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,最好不要混合匹配xml和java配置。由于java配置已经成为常态,您可以使用 WebMvcConfigurerAdapter

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

我认为你的调度程序在你的web.xml中没有正确配置。您没有任何<init-param>代码。见文档

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

尝试编辑:

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

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

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

答案 3 :(得分:0)

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

我认为你错过了pom.xml中的一些存储库或pom.xml中不同版本的多个jar。有时你的春天的罐子彼此不相称。

添加:

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

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

参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html(阅读:Maven&#34;物料清单&#34;依赖性)

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

答案 4 :(得分:0)

首先检查所有构建路径错误是否都已解决。 在大多数情况下,如果无法解决构建路径错误,Spring初始化器会抛出此错误。