Spring HandlerInterceptorAdapter postHandle是否得到保证? [英] Is Spring HandlerInterceptorAdapter postHandle guaranteed?

查看:348
本文介绍了Spring HandlerInterceptorAdapter postHandle是否得到保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个处理类似功能的拦截器.我想将它们合并.

I currently have two interceptors that handle a similar function. I'd like to merge these.

一个拦截器是访问请求记录器,它显示登录的用户,会话ID和请求的URL.

One interceptor is an access request logger, that shows the logged in user, the session id, and the requested URL.

另一个拦截器是一个过程时间记录器.

The other interceptor is a process time logger.

为了记录必须记录的所有内容,访问记录器将请求记录在preHandle方法中.这样的想法是,无论之后发生了什么(即异常),确切的访问请求都将存在.

The access logger, in order to log all that must be logged, logs the request in the preHandle method. The idea is that regardless of what happens after (ie. exceptions), the exact access request will be there.

但是,由于其性质,处理时间记录器必须登录postHandle方法.

The process time logger however, due to its nature, must log in the postHandle method.

要合并此功能,我必须将所有内容移到单个postHandle方法中.但是,如果某个地方发生异常,尤其是在应用程序代码中尚未正确处理的异常,看来我可能会丢失一些日志记录.

For merging this functionality, I'd have to move everything into a single postHandle method. However, it seems I may lose some logging if an exception occurs somewhere, especially one that is not (yet) handled properly in the application code.

是否有关于这些注意事项的保证或描述?

Is there any guarantee or description as to these considerations?

推荐答案

您可以考虑将 afterCompletion 内部的逻辑合并,即使在处理程序方法抛出异常的情况下也会调用该逻辑.一个很好的在线示例

You can consider merging the logic inside the afterCompletion which will be called even in the case when a handler method throws an exception. A good online example

public class RequestProcessingTimeInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory
            .getLogger(RequestProcessingTimeInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        long startTime = System.currentTimeMillis();
        logger.info("Request URL::" + request.getRequestURL().toString()
                + ":: Start Time=" + System.currentTimeMillis());
        request.setAttribute("startTime", startTime);
        //if returned false, we need to make sure 'response' is sent
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("Request URL::" + request.getRequestURL().toString()
                + " Sent to Handler :: Current Time=" + System.currentTimeMillis());
        //we can add attributes in the modelAndView and use that in the view page
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        long startTime = (Long) request.getAttribute("startTime");
        logger.info("Request URL::" + request.getRequestURL().toString()
                + ":: End Time=" + System.currentTimeMillis());
        logger.info("Request URL::" + request.getRequestURL().toString()
                + ":: Time Taken=" + (System.currentTimeMillis() - startTime));
    }
 }

这篇关于Spring HandlerInterceptorAdapter postHandle是否得到保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆