[英]How can I lookup the method being called on a Handler from a Spring HandlerInterceptor?
I have a Spring HandlerInterceptor intercepting the frontend URL's in my application (/app/*). 我有一个Spring HandlerInterceptor拦截我的应用程序中的前端URL(/ app / *)。 I want to determine which action method in the Handler is about to be invoked from within the HandlerInterceptor.
我想确定要在HandlerInterceptor中调用Handler中的哪个动作方法。 Is there a way to look that up, do I need to inject something into the interceptor that can look that up based on the requested path?
有没有办法查看,我是否需要向拦截器注入一些可以根据请求的路径查找的内容?
The Interceptor is like this: 拦截器是这样的:
public class PageCacheInterceptor implements HandlerInterceptor {...}
It is mapped like this: 它映射如下:
<mvc:interceptors>
<bean class="com.example.web.interceptors.PageCacheInterceptor" />
</mvc:interceptors>
Background (because I know you'll ask!). 背景(因为我知道你会问!)。 I am adding simple page caching to my app and want to use an annotation like @Cacheable on each suitable method in the controller.
我正在为我的应用添加简单的页面缓存,并希望在控制器中的每个合适的方法上使用@Cacheable之类的注释。 The interceptor can then determine whether to cache a response based on the action that created it.
然后,拦截器可以根据创建响应的操作确定是否缓存响应。
For example: 例如:
@RequestMapping(value = "", method = RequestMethod.GET)
@Cacheable(events={Events.NEW_ORDER,Events.NEW_STAT})
public String home(Model model) {...}
The events are the ones that cause the cache to be invalidated. 事件是导致缓存失效的事件。 For example /widget/list action would have it's cached response invalidated by a new widget being saved.
例如,/ widget / list动作会使其缓存的响应被保存的新窗口小部件无效。
Edit: I've upgraded to the latest Spring 3.1 M2, as this blog post hinted at features I need, but it's not clear whether injecting these new classes or sub-classing them will be required. 编辑:我已经升级到最新的Spring 3.1 M2,因为这篇博文暗示了我需要的功能,但是不清楚是否需要注入这些新类或对它们进行子类化。 Has any one used them to retrieve the HandlerMethod in an interceptor?
有没有人用它们来拦截拦截器中的HandlerMethod?
Ok so the solution was actually really easy: 好的,所以解决方案实际上非常简单:
1) Upgrade to Spring 3.1 1)升级到Spring 3.1
2) RTFM (properly) 2) RTFM (正确)
For example a HandlerInterceptor can cast the handler from Object to HandlerMethod and get access to the target controller method, its annotations, etc
例如,HandlerInterceptor可以将处理程序从Object转换为HandlerMethod,并可以访问目标控制器方法,其注释等。
3) Cast the handler object to HandlerMethod in the Interceptor. 3)在Interceptor中将处理程序对象强制转换为HandlerMethod。
Then you can do this sort of thing: 然后你可以做这样的事情:
HandlerMethod method = (HandlerMethod) handler;
Cacheable methodAnnotation = method.getMethodAnnotation(Cacheable.class);
if (methodAnnotation != null) {
System.out.println("cacheable request");
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("Pre-handle");
HandlerMethod hm=(HandlerMethod)handler;
Method method=hm.getMethod(); if(method.getDeclaringClass().isAnnotationPresent(Controller.class)){
if(method.isAnnotationPresent(ApplicationAudit.class))
{
System.out.println(method.getAnnotation(ApplicationAudit.class).value());
request.setAttribute("STARTTIME",System.currentTimemillis());
}
}
return true;
}
This post has more details,hope this helps http://www.myjavarecipes.com/spring-profilingaudit-using-mvc-filters/ 这篇文章有更多细节,希望这有助于http://www.myjavarecipes.com/spring-profilingaudit-using-mvc-filters/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.