ProceedingJoinPoint獲取當前方法


aspectJ切面通過ProceedingJoinPoint想要獲取當前執行的方法:

錯誤方法:


     Signature s = pjp.getSignature();
    MethodSignature ms = (MethodSignature)s;
    Method m = ms.getMethod();

這種方式獲取到的方法是接口的方法而不是具體的實現類的方法,因此是錯誤的。


正確方法:

        Signature sig = pjp.getSignature();
        MethodSignature msig = null;
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException("該注解只能用於方法");
        }
        msig = (MethodSignature) sig;
        Object target = pjp.getTarget();
        Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM