2012-01-23 57 views
0

我一直在按照說明here來實現一個使用註釋並覆蓋DefaultAnnotationHandlerMapping的spring mvc處理程序攔截器。爲什麼不調用我的DefaultAnnotationHandlerMapping?

但是,我的攔截器從來沒有被調用過。

任何人都可以看到我在做什麼錯在這裏?

我已經在博客文章中實現了@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永遠不會被調用。

感謝您閱讀這本書 - 我知道這裏有很多!

任何人都可以看到我錯過了什麼或做錯了嗎?

謝謝!

回答

4

的問題可能是在這一行:

private TestController controller = new TestController(); 

你需要從上下文獲得「控制手柄」,而不是自己初始化。

+0

謝謝 - 我可以看到,這將是一個問題。不幸的是,在更新我的代碼後,我仍然沒有攔截... – laura

+0

我越想越想越好,這應該在servlet環境中工作,在那裏你可以得到實際的控制器請求。你有沒有嘗試部署你的代碼,看看攔截器是否被觸發? – Abhi

相關問題