簡體   English   中英

Spring mvc 自動裝配 RequestMappingHandlerMapping

[英]Spring mvc autowire RequestMappingHandlerMapping

我正在嘗試在我的 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>

我的根上下文.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";
    }
}

您應該在自動裝配它之前啟動 RequestMappingHandlerMapping bean。 它有兩種方式:

  1. 在 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. 或使用

    @配置

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

嘗試獲取所有請求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源碼,會發現HandlerMapping接口的implementation classes ,比如

AbstractControllerUrlHandlerMapping, AbstractDetectingUrlHandlerMapping,
AbstractHandlerMapping, AbstractHandlerMethodMapping,
AbstractUrlHandlerMapping, BeanNameUrlHandlerMapping, 
ControllerBeanNameHandlerMapping, ControllerClassNameHandlerMapping,
DefaultAnnotationHandlerMapping, RequestMappingHandlerMapping,
RequestMappingInfoHandlerMapping, SimpleUrlHandlerMapping 
  • 我嘗試在我的主類中添加“ @EnableWebFlux ”並且它有效(在我的情況下)。
  • 所以我想也許“ EnableWebMvc ”可以這樣工作..
  • 它對我來說很好用,請不要↓我:)
@EnableWebFlux(for webflux)
@EnableWebMvc(for commvc)
@SpringBootApplication
public class InstoreApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(InstoreApplication.class)......
    }
}

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM