Spring mvc autowire RequestMappingHandlerMapping

时间:2015-08-08 12:52:28

标签: java spring-mvc

我正在尝试在我的spring mvc控制器中自动装配org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping以获取所有url映射并在UI上显示它们,但不成功。缺少bean错误:

 org.springframework.beans.factory.BeanCreationException: Could    not autowire field: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping web.controller.WorkController.handlerMapping; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] 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)}

我的web.xml:

<display-name>Spring MVC Application</display-name>

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

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

我的mvc-dispatcher-servlet.xml:

 <context:annotation-config/>
    <context:component-scan base-package="web.controller"/>
             <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

我的root-context.xml:

<bean id="helloBean" class="web.beans.HelloBean"/>

java控制器:

package web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import web.beans.HelloBean;

import java.util.List;

@Controller
public class WorkController {

    @Autowired RequestMappingHandlerMapping handlerMapping;
    @Autowired private HelloBean helloBean;
    @Autowired private ApplicationContext applicationContext;

    @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }
}

4 个答案:

答案 0 :(得分:4)

您应该在自动装配之前启动RequestMappingHandlerMapping bean。 它有两种方式:

  1. 在springxml配置中,例如hello bean
  2. <bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <!-- add your properties here property name="..." value="..."></property-->
    </bean>
    
    1. 或使用

      @Configuration

      @Configuration 
      @ComponentScan("your.package") 
      @EnableWebMvc   
      public class AppConfig {  
      ...
          @Bean
          public RequestMappingHandlerMapping requestMappingHandlerMapping() {
             RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
             // add properties here
             return mapping;
          }
      ...
      } 
      

答案 1 :(得分:2)

尝试获取所有请求urls,以下代码可能对您有用。

ServletContext servletContext = request.getSession().getServletContext();
WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, HandlerMapping.class, true, false);
for (HandlerMapping handlerMapping : allRequestMappings.values()) {
    if (handlerMapping instanceof RequestMappingHandlerMapping) {
          RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
          Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
          for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet()) {
             RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();
             PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
             String requestUrl = SetUtils.first(patternsCondition.getPatterns());
             System.out.println(requestUrl);
          }
    }
}

坦率地说,java reflect是获取所有请求urls的关键点。如果你深入研究spring-mvc源代码,你会发现implementation classes HandlerMapping接口,例如

AbstractControllerUrlHandlerMapping, AbstractDetectingUrlHandlerMapping,
AbstractHandlerMapping, AbstractHandlerMethodMapping,
AbstractUrlHandlerMapping, BeanNameUrlHandlerMapping, 
ControllerBeanNameHandlerMapping, ControllerClassNameHandlerMapping,
DefaultAnnotationHandlerMapping, RequestMappingHandlerMapping,
RequestMappingInfoHandlerMapping, SimpleUrlHandlerMapping 

答案 2 :(得分:0)

  • 我尝试在我的主类中添加“ @EnableWebFlux ”,它可以正常工作(在我的 情况)。
  • 所以我认为也许“ EnableWebMvc ”可以工作。.
  • 对我来说很好,请不要↓我:)
@EnableWebFlux(for webflux)
@EnableWebMvc(for commvc)
@SpringBootApplication
public class InstoreApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(InstoreApplication.class)......
    }
}

答案 3 :(得分:0)

您可以在 *.properties 文件中添加这些:

# Log restful end points
logging.level.web=TRACE
logging.level.org.springframework.web=TRACE