从3.0.7升级Spring到3.1.2会引发NoSuchBeanDefinitionException

时间:2012-08-29 16:18:59

标签: java spring exception spring-mvc autowired

您好我正在尝试将Spring从3.0.7升级到3.1.2。在3.0.7中一切正常,但是当我在我的pom中提高值时,我在启动服务器时会出错。我是新来的春天。

我得到的错误是

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'my.name.space.security.handler.AnnotationMethodHandlerAdapterConfigurer#0': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter my.name.space.security.handler.AnnotationMethodHandlerAdapterConfigurer.adapter; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

就像我说我刚接触春天一样,但看起来似乎无法自动装配到原生春季班。我运行3.0.7,这一切都很好。这是我的堆栈跟踪。

2012-08-29 11:50:24,364 [localhost-startStop-1] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ns.web.security.handler.AnnotationMethodHandlerAdapterConfigurer#0': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter com.clovrmedia.web.security.handler.AnnotationMethodHandlerAdapterConfigurer.adapter; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
  at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
  at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
  at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
  at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
  at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
  at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
  at javax.servlet.GenericServlet.init(GenericServlet.java:160)
  at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
  at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
  at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
  at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
  at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
  at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
  at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:650)
  at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1582)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  at java.lang.Thread.run(Thread.java:680)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter com.clovrmedia.web.security.handler.AnnotationMethodHandlerAdapterConfigurer.adapter; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
  ... 34 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:952)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:821)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
  ... 36 more

这是我相信提出这个例外的课程:

package com.ns.web.security.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;

/**
 * This class is a bean post-processor for AnnotationMethodHandlerAdapter. This required to set the property values
 * in the instance of AnnotationMethodHandlerAdapter created by the spring framework when using the tag
 * <annotation-driven/>
 */
public class AnnotationMethodHandlerAdapterConfigurer {
    @Autowired
    private AnnotationMethodHandlerAdapter adapter;
    private int cacheSeconds = -1;

    public void init() {
        if(cacheSeconds > -1) {
            adapter.setCacheSeconds(cacheSeconds);
        }
    }

    public int getCacheSeconds() {
        return cacheSeconds;
    }

    public void setCacheSeconds(int cacheSeconds) {
        this.cacheSeconds = cacheSeconds;
    }
}

这是一个xml声明,它规定了spring请求的行为

    <!-- Handles Spring requests -->
<servlet>
    <servlet-name>clovrpatch</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/nsWebMVCConfig.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

这是上面xml类中引用的nsWebMVCConfig.xml文件。它包含一个引发异常的类的bean声明。

   <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:p="http://www.springframework.org/schema/p"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

      <import resource="classpath:META-INF/spring/nsPropertyConfig.xml"/>

      <!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
      <context:component-scan base-package="com.ns.web" use-default-filters="false">
            <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
      </context:component-scan>

      <!-- Turns on support for mapping requests to Spring MVC @Controller methods
           Also registers default Formatters and Validators for use across all @Controllers -->
      <mvc:annotation-driven />

    <!-- This is a bean post-processor for AnnotationMethodHandlerAdapter-->
    <bean class="com.ns.web.security.handler.AnnotationMethodHandlerAdapterConfigurer" init-method="init">
        <property name="cacheSeconds" value="0" />
    </bean>

      <!-- register "global" interceptor beans to apply to all registered HandlerMappings -->
      <mvc:interceptors>
            <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
            <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/>

        <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />

    </mvc:interceptors>

      <mvc:view-controller path="/scrubbedout"/>
      <mvc:view-controller path="/scrubbedout"/>
      <mvc:view-controller path="/scrubbedout"/>
      <mvc:view-controller path="/scrubbedout" />
    <mvc:view-controller path="/scrubbedout" />
    <mvc:view-controller path="/scrubbedout" />

      <!-- store preferred language configuration in a cookie -->
      <bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale"/>     

      <!-- resolves localized <theme_name>.properties files in the classpath to allow for theme support -->
      <bean class="org.springframework.ui.context.support.ResourceBundleThemeSource" id="themeSource"/>

      <!-- store preferred theme configuration in a cookie -->
      <bean class="org.springframework.web.servlet.theme.CookieThemeResolver" id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard"/>

      <!-- allows for integration of file upload functionality -->
      <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/>

    <!-- Enable @Annotation-drive bean configuration -->
    <context:annotation-config />

    <!-- Configures Spring Social -->
    <bean class="com.ns.web.social.SocialConfig" />

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
            <property name="mediaTypes">
                  <map>
                        <entry key="html" value="text/html" />
                        <entry key="json" value="application/json" />
                  </map>
            </property>
            <property name="viewResolvers">
                  <list>
                        <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                              <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
                        </bean>
                  </list>
            </property>
            <property name="defaultViews">
                  <list>
                        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                              <property name="prefixJson" value="true" />
                        </bean>
                  </list>
            </property>
        <property name="defaultContentType" value="text/html"/>
      </bean>

    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/layouts/nsMainLayouts.xml</value>
            </list>
        </property>
        <property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory"/>
    </bean>

    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

    <!-- Default route -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/">welcomeController</prop>
            </props>
        </property>
        <property name="rootHandler" ref="welcomeController" />
    </bean>
    <!-- these are static resources. By specifing them here, they will not go through the DispatcherServlet -->

    <mvc:resources mapping="/css/**"              location="/css/"/>
    <mvc:resources mapping="/images/**"           location="/images/"/>
    <mvc:resources mapping="/js/**"               location="/js/"/>
</beans>

我觉得很蹩脚,只是提出一个堆栈跟踪和几个文件,然后说,'为我弄清楚。'但我完全超出了我的深度,并且在过去的3天里一直在谷歌搜索。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:6)

我不熟悉Spring安全性,但自{3.1}}以来,AnnotationMethodHandlerAdapter已被RequestMappingHandlerAdapter取代。这可能是其中一个原因。您可以查看link。希望它对你有所帮助。