DefaultAnnotationHandlerMapping

DefaultAnnotationHandlerMapping

我一直按照here的说明使用注解并覆盖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();


您需要从上下文中获取“控制器句柄”,而不是初始化自己。

10-04 10:17