[英]Why isn't my DefaultAnnotationHandlerMapping being called?
我一直按照這里的說明使用注解並覆蓋DefaultAnnotationHandlerMapping實現spring mvc handler攔截器。
但是,我的攔截器從未被調用。
有人可以在這里看到我做錯了嗎?
我已經在博客文章中實現了@Interceptors。
我創建了一個攔截器:
@Component
public class InterceptorSpike extends HandlerInterceptorAdapter {
public InterceptorSpike() {
System.err.println("InterceptorSpike initialised");
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
throw new RuntimeException("Yay, intercepted!");
}
}
和一個測試控制器:
@Controller
@Interceptors({InterceptorSpike.class})
public class TestController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void doSomething() {
System.err.println("I'm doing something, have I been intercepted??");
}
}
我的處理程序的樣本(與博客文章大部分相同)
@Component
public class HandlerInterceptorAnnotationAwareHandlerMapping extends DefaultAnnotationHandlerMapping {
public HandlerInterceptorAnnotationAwareHandlerMapping() {
System.err.println("HandlerInterceptorAnnotationAwareHandlerMapping initialised");
}
...
[編輯-忽略,為了完整性。 我已經恢復了這些步驟以再次使用自動裝配]我最初使用@Component自動裝配,但是由於嘗試了不同的修復,所以將其移動到應用程序上下文中。
我添加了訂單,但沒有使用<mvc:annotation-driven/>
。 這些是我在尋找解決方案時通過一些帖子提出的。 [/編輯]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName">
<context:component-scan base-package="com.xxx"/>
<context:spring-configured/>
<!-- removed manual wiring -->
</beans>
最后,這是我的測試:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/test-app-context.xml"})
public class ControllerInterceptorTest {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private TestController controller;
@Test
public void shouldInterceptMethod() {
Map<String, DefaultAnnotationHandlerMapping> mappings = applicationContext.getBeansOfType(DefaultAnnotationHandlerMapping.class);
for (String key : mappings.keySet()) {
DefaultAnnotationHandlerMapping mapping = mappings.get(key);
System.out.println(String.format("key [%s], order [%s], value = %s", key, mapping.getOrder(), mapping.getClass().getCanonicalName()));
}
controller.doSomething();
fail("should have thrown exception");
}
}
我得到以下輸出:
HandlerInterceptorAnnotationAwareHandlerMapping initialised
InterceptorSpike initialised
key [handlerInterceptorAnnotationAwareHandlerMapping], order [2147483647], value = com.xxx.interceptors.HandlerInterceptorAnnotationAwareHandlerMapping
I'm doing something, have I been intercepted??
java.lang.AssertionError: should have thrown exception
at org.junit.Assert.fail(Assert.java:91)
...
從未調用過我的新DefaultAnnotationHandlerMapping上的getHandlerExecutionChain。
感謝您閱讀本文至今-我知道這里有很多東西!
誰能看到我錯過或做錯的事情?
謝謝!
問題可能在此行中:
private TestController controller = new TestController();
您需要從上下文中獲取“控制器句柄”,而不是初始化自己。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.