Announcement Announcement Module
Collapse
No announcement yet.
Unable to Autowire RequestMappingHandlerMapping in Controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to Autowire RequestMappingHandlerMapping in Controller

    Hi,

    I am trying to autowire the RequestMappingHandlerMapping instance in my controller to build a feature similar to the EndpointDocumentationController example by Rossen at https://github.com/rstoyanchev/spring-mvc-31-demo.
    However we are using the mvc namespace for configuration and I get an exception saying "No matching bean was found for auto-wiring".

    Code:
    @Controller
    public class EndpointController {
        private RequestMappingHandlerMapping handlerMapping;
        @Autowired
        public EndpointController(RequestMappingHandlerMapping handlerMapping) {
            this.handlerMapping = handlerMapping;
        }
    }
    Exception stack trace -
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No m
    atching bean of type [org.springframework.web.servlet.mvc.method.annotat ion.Requ
    estMappingHandlerMapping] found for dependency: expected at least 1 bean which q
    ualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.
    raiseNoSuchBeanDefinitionException(DefaultListable BeanFactory.java:924)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.
    doResolveDependency(DefaultListableBeanFactory.jav a:793)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.
    resolveDependency(DefaultListableBeanFactory.java: 707)
    at org.springframework.beans.factory.support.Construc torResolver.resolve
    AutowiredArgument(ConstructorResolver.java:795)
    at org.springframework.beans.factory.support.Construc torResolver.createA
    rgumentArray(ConstructorResolver.java:723)
    ... 23 more

    I am able to reproduce this in the https://github.com/SpringSource/spring-mvc-showcase by adding the following configuration to the root-context.xml -

    Code:
    <context:component-scan base-package="org.springframework.samples.mvc" />
    <context:annotation-config/>
    It works fine by not having the above configuration, which I need in my application to load other beans.
    What am I doing wrong?

    Thanks.

    Regards
    Kaizer

  • #2
    If you're doing a component scan in the root context is it possible that it detects your controllers as well? Since the root context is a parent of the DispatcherServlet application context, it doesn't see any web related beans like the ones declared with mvc:annotation-driven. Plus you probably end up with controller beans declared in the root context and the controller beans declared in the DispatcherServlet context.

    The component-scan element supports all sorts of ways to exclude something from the scan. Try excluding by @Controller annotation.

    Comment


    • #3
      Thanks Rossen! That worked!

      So, does that mean that having a component-scan in both the context files would load all Service, Repository, etc beans twice?

      Regards
      Kaizer

      Comment


      • #4
        Yes, if you declare or detect the same beans in both places then they would be created in each application context. That's why component scanning provides lots of options for fine-grained control.

        Comment


        • #5
          I also cannot autowire RequestMappingHandlerMapping within a controller (it's an Abstract controller, but I don't think it should matter).
          I am using Spring 3.1.0 with @EnableWebMvc. Is there something specific I need to do?

          Thx,
          Norm

          Comment


          • #6
            It seems that autowiring into a non-abstract class did the trick!
            Could someone explain why this matters? I always thought we could easily autowire into an abstract class.

            Comment

            Working...
            X