溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

spring中AOP怎么使用

發(fā)布時(shí)間:2021-06-25 15:21:36 來(lái)源:億速云 閱讀:173 作者:Leah 欄目:大數(shù)據(jù)

spring中AOP怎么使用,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

//AOP切面 :多個(gè)切面時(shí),@Order(i)注解來(lái)標(biāo)識(shí)切面的優(yōu)先級(jí)。i的值越小,優(yōu)先級(jí)越高
@Order(5)
@Aspect
@Component
public class SysAspect {

    @Pointcut("execution(public * com.zypcy.expend.controller.*.*(..))")
    public void log(){}

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SysAspect.class);

    //統(tǒng)計(jì)請(qǐng)求的處理時(shí)間
    ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) throws Throwable{
        startTime.set(System.currentTimeMillis());
        //接收到請(qǐng)求,記錄請(qǐng)求內(nèi)容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //記錄請(qǐng)求的內(nèi)容
        System.out.println("Aspect_URL:"+request.getRequestURL().toString());
        System.out.println("Aspect_Method:"+request.getMethod());
    }

    @AfterReturning(returning = "ret" , pointcut = "log()")
    public void doAfterReturning(Object ret){
        //處理完請(qǐng)求后,返回內(nèi)容
        System.out.println("方法返回值:"+ JSON.toJSONString(ret) +",方法執(zhí)行時(shí)間:"+ (System.currentTimeMillis() - startTime.get()));
    }
}

看完上述內(nèi)容,你們掌握spring中AOP怎么使用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI