栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在AspectJ中使用AOP进行日志记录?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在AspectJ中使用AOP进行日志记录?

我创建了一个简单的方面来捕获公共方法的执行。该AspectJ代码的核心是切入点定义:

pointcut publicMethodExecuted(): execution(public * *(..));

在这里,我们将捕获任何包,任何类,具有任意数量的参数,具有任何返回类型的所有公共方法。

建议执行可以在下面的代码段中可视化:

after(): publicMethodExecuted() {    System.out.printf("Enters on method: %s. n", thisJoinPoint.getSignature());    Object[] arguments = thisJoinPoint.getArgs();    for (int i =0; i < arguments.length; i++){        Object argument = arguments[i];        if (argument != null){ System.out.printf("With argument of type %s and value %s. n", argument.getClass().toString(), argument);        }    }    System.out.printf("Exits method: %s. n", thisJoinPoint.getSignature());}

该建议使用thisJoinPoint获取方法签名和参数。就是这样。这是方面代码:

public aspect LogAspect {pointcut publicMethodExecuted(): execution(public * *(..));after(): publicMethodExecuted() {    System.out.printf("Enters on method: %s. n", thisJoinPoint.getSignature());    Object[] arguments = thisJoinPoint.getArgs();    for (int i =0; i < arguments.length; i++){        Object argument = arguments[i];        if (argument != null){ System.out.printf("With argument of type %s and value %s. n", argument.getClass().toString(), argument);        }    }    System.out.printf("Exits method: %s. n", thisJoinPoint.getSignature());}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/447066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号