我正在尝试org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping在我的 spring mvc 控制器中自动装配以获取所有 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)}
Run Code Online (Sandbox Code Playgroud)
我的 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>
Run Code Online (Sandbox Code Playgroud)
我的 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>
Run Code Online (Sandbox Code Playgroud)
我的根上下文.xml:
<bean id="helloBean" class="web.beans.HelloBean"/>
Run Code Online (Sandbox Code Playgroud)
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";
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您应该在自动装配它之前启动 RequestMappingHandlerMapping bean。它有两种方式:
Run Code Online (Sandbox Code Playgroud)<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;
}
...
}
Run Code Online (Sandbox Code Playgroud)尝试获取所有请求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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
坦白说,java reflect是得到所有请求的一个关键点urls。如果你深入研究spring-mvc源码,你会发现接口implementation classes, HandlerMapping比如
AbstractControllerUrlHandlerMapping, AbstractDetectingUrlHandlerMapping,
AbstractHandlerMapping, AbstractHandlerMethodMapping,
AbstractUrlHandlerMapping, BeanNameUrlHandlerMapping,
ControllerBeanNameHandlerMapping, ControllerClassNameHandlerMapping,
DefaultAnnotationHandlerMapping, RequestMappingHandlerMapping,
RequestMappingInfoHandlerMapping, SimpleUrlHandlerMapping
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18419 次 |
| 最近记录: |