Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring mvc autowire RequestMappingHandlerMapping_Java_Spring Mvc - Fatal编程技术网

Java Spring mvc autowire RequestMappingHandlerMapping

Java Spring mvc autowire RequestMappingHandlerMapping,java,spring-mvc,Java,Spring Mvc,我试图在我的SpringMVC控制器中自动连接org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping,以获取所有url映射并在UI上显示它们,但没有成功。缺少bean时出错: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.

我试图在我的SpringMVC控制器中自动连接
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)}
My 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>

尝试获取所有请求
URL
,下面的代码可能对您有用

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);
          }
    }
}

您应该在自动连接RequestMappingHandlerMappingbean之前启动它。 它有两种方式:

  • 在springxml配置中,例如HelloBean
    • 我尝试在我的主类中添加“@EnableWebFlux”,它在我的 情况)
    • 所以我认为也许“使能WebMVC”是这样的
    • 这对我来说很好,不是吗↓ 请告诉我:)
    @EnableWebFlux(适用于webflux)
    @启用WebMVC(用于commvc)
    @SpringBoot应用程序
    公共类仪器应用{
    公共静态void main(字符串[]args){
    ConfigurableApplicationContext applicationContext=新的SpringApplicationBuilder(InstoreApplication.class)。。。。。。
    }
    }
    
    您可以在*.properties文件中添加这些属性:

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

    什么是SetUtils.first()?@NickJ将其视为
    final Set patterns=patternsCondition.getPatterns();String requestUrl=patterns.stream().findFirst().orElse(StringUtils.EMPTY)参考:
    
    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";
        }
    }
    
    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);
              }
        }
    }
    
    AbstractControllerUrlHandlerMapping, AbstractDetectingUrlHandlerMapping,
    AbstractHandlerMapping, AbstractHandlerMethodMapping,
    AbstractUrlHandlerMapping, BeanNameUrlHandlerMapping, 
    ControllerBeanNameHandlerMapping, ControllerClassNameHandlerMapping,
    DefaultAnnotationHandlerMapping, RequestMappingHandlerMapping,
    RequestMappingInfoHandlerMapping, SimpleUrlHandlerMapping 
    
    <bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <!-- add your properties here property name="..." value="..."></property-->
    </bean>
    
    @Configuration 
    @ComponentScan("your.package") 
    @EnableWebMvc   
    public class AppConfig {  
    ...
        @Bean
        public RequestMappingHandlerMapping requestMappingHandlerMapping() {
           RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
           // add properties here
           return mapping;
        }
    ...
    } 
    
    # Log restful end points
    logging.level.web=TRACE
    logging.level.org.springframework.web=TRACE