亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了如何在Spring MVC攔截器中獲取方法執(zhí)行流程的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想在Spring MVC項(xiàng)目中獲得完整的執(zhí)行流程以及它們的執(zhí)行時(shí)間。

public class MetricsInterceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (handler instanceof HandlerMethod) {
        HandlerMethod hm = (HandlerMethod) handler;
        Method method = hm.getMethod();
        if (method.getDeclaringClass().isAnnotationPresent(Controller.class)) {
            if (method.isAnnotationPresent(Metrics.class)) {
               // System.out.println(method.getAnnotation(Metrics.class).value());
                System.out.println(method.getName());
            }
        }
    }
    return super.preHandle(request, response, handler);
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    super.postHandle(request, response, handler, modelAndView);
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    super.afterCompletion(request, response, handler, ex);
}

}

我使用@Metrics注釋獲取控制器類和方法,以記錄幾個(gè)方法的度量。我想要的是獲得每個(gè)方法所花費(fèi)的時(shí)間的整個(gè)方法執(zhí)行流程(控制器–>服務(wù)–>DAO)。有沒有辦法在postHandle()或AfterCompletion()中獲取該信息。請(qǐng)?zhí)岢鼋ㄗh。

推薦答案

使用Spring AOPAround建議可以實(shí)現(xiàn)這一點(diǎn)。

編寫將截獲的pointcut

包及其子包中公共方法的所有執(zhí)行
屬于的包及其子包
控制器層服務(wù)層(可選僅限于特定服務(wù))DAO層

然后寫下周圍的建議,如下

@Component
@Aspect
public class TraceAdvice {

    @Around("execution(* com.test..*(..))  &&" + " (within(com.test.controller..*) || "
        + "(within(com.test.service..*) && this(com.test.service.TestService)) || " + "within(com.test.dao..*))")
    public Object traceCall(ProceedingJoinPoint pjp) throws Throwable {
        /* This will hold our execution details in reverse order 
         * i.e. last method invoked would appear first.
         * Key = Full qualified name of method
         * Value = Execution time in ms
         */
        Map<String, Long> trace = null;
        Signature sig = pjp.getSignature();
        // Get hold of current request
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // check if we are in controller (I used RestController modify it if Controller is required instead) 
        boolean isController = sig.getDeclaringType().isAnnotationPresent(RestController.class);
        if (isController) {
            // set the request attributte if we are in controller
            trace = new LinkedHashMap<>();
            req.setAttribute("trace", trace);
        } else {
            // if its not controller then read from request atributte
            trace = (Map<String, Long>) req.getAttribute("trace");
        }
        Object result = null;
        StopWatch watch = new StopWatch();
        try {
            // start the timer and invoke the advised method
            watch.start();
            result = pjp.proceed();
        } finally {
            // stop the timer 
            watch.stop();
            String methodName = sig.getDeclaringTypeName() + "." + sig.getName();
            // make entry for the method name with time taken
            trace.put(methodName, watch.getTotalTimeMillis());
            if (isController) {
                // since controller method is exit point print the execution trace
                trace.entrySet().forEach(entry -> {
                    System.out.println("Method " + entry.getKey() + " took " + String.valueOf(entry.getValue()));
                });
            }
        }
        return result;
    }
}

在進(jìn)行必要的配置后,示例輸出應(yīng)如下所示

com.est.dao.TestDAO.getTest方法花費(fèi)350

方法com.est.service.TestService.getTest采用1954年

方法com.est.Controller.TestController.getTest采用3751

或者,可以編寫三個(gè)切入點(diǎn)和各自的Around建議,每個(gè)攔截特定的包,以消除檢查控制器的if-else部分

我已使用

測試了設(shè)置

Spring 4.3.2 Release
AspectJ 1.7.4
JDK 1.8
Tomcat 8.0

如果有任何問題,請(qǐng)?jiān)趥渥⒅懈嬷?/p>

這篇關(guān)于如何在Spring MVC攔截器中獲取方法執(zhí)行流程的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:器中 如何在 執(zhí)行 攔截 方法 流程 獲取
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績?cè)u(píng)定