简体   繁体   English

Spring mvc 自动装配 RequestMappingHandlerMapping

[英]Spring mvc autowire RequestMappingHandlerMapping

I'm trying to autowire org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping in my spring mvc controller in order to get all url mappings and display them on UI, but not successfull.我正在尝试在我的 spring mvc 控制器中自动装配org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping以获取所有 url 映射并将它们显示在 UI 上,但没有成功。 There is error that the bean is missing:有错误,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)}

My web.xml:我的 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>

My mvc-dispatcher-servlet.xml:我的 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>

My root-context.xml:我的根上下文.xml:

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

The java controller: 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";
    }
}

You should initiate the RequestMappingHandlerMapping bean before autowired it.您应该在自动装配它之前启动 RequestMappingHandlerMapping bean。 It has two way:它有两种方式:

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

    @Configuration @配置

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

Try to get all request urls , The code below may be useful for you.尝试获取所有请求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);
          }
    }
}

Frankly speaking, java reflect is a key point to get all request urls .坦白说, java reflect是获取所有请求urls的关键点。 if you look into the spring-mvc source deeply, you will find the implementation classes of HandlerMapping interface, such as如果深入查看spring-mvc源码,会发现HandlerMapping接口的implementation classes ,比如

AbstractControllerUrlHandlerMapping, AbstractDetectingUrlHandlerMapping,
AbstractHandlerMapping, AbstractHandlerMethodMapping,
AbstractUrlHandlerMapping, BeanNameUrlHandlerMapping, 
ControllerBeanNameHandlerMapping, ControllerClassNameHandlerMapping,
DefaultAnnotationHandlerMapping, RequestMappingHandlerMapping,
RequestMappingInfoHandlerMapping, SimpleUrlHandlerMapping 
  • i try to add " @EnableWebFlux " in my Main class and it works(in my situation).我尝试在我的主类中添加“ @EnableWebFlux ”并且它有效(在我的情况下)。
  • so i think maybe " EnableWebMvc " works so..所以我想也许“ EnableWebMvc ”可以这样工作..
  • it works fine for me, don`t ↓ me please :)它对我来说很好用,请不要↓我:)
@EnableWebFlux(for webflux)
@EnableWebMvc(for commvc)
@SpringBootApplication
public class InstoreApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(InstoreApplication.class)......
    }
}

You can add these in *.properties file:您可以在 *.properties 文件中添加这些:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring MVC中自动装配MongoRepository - Autowire MongoRepository in Spring MVC Spring MVC - Spring有时使用RequestMappingHandlerMapping,有时使用SimpleUrlHandlerMapping - Spring MVC - Spring uses sometimes RequestMappingHandlerMapping and sometimes SimpleUrlHandlerMapping 使用Spring MVC RequestMappingHandlerMapping和Spring Websocket的ServletWebSocketHandlerRegistry处理相同的URL - Handle the same URL with Spring MVC RequestMappingHandlerMapping and Spring Websocket's ServletWebSocketHandlerRegistry 当应用程序启动时,Spring MVC RequestMappingHandlerMapping会发生两次 - Spring MVC RequestMappingHandlerMapping happens twice when application starts 春季测试模拟MVC不应用自定义RequestMappingHandlerMapping - Spring Testing Mock MVC doesn't apply custom RequestMappingHandlerMapping Spring MVC:servlet中忽略了自动装配 - Spring MVC: autowire ignored in servlets 按名称自动接线在Spring MVC中不起作用 - autowire by name not working spring MVC Spring RequestMappingHandlerMapping启动慢 - Spring RequestMappingHandlerMapping Startup Slow 弹簧 RequestMappingHandlerMapping 不起作用 - spring RequestMappingHandlerMapping not working 如何在Spring MVC中自动装配多级继承类 - How to autowire for multilevel inheritance class in spring MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM