aspectj - 关于具有字段的类的方法的 AspectJ 建议
问题描述
我想写一个环绕建议,在调用proceed()之前将相关ID设置为MDC,并在它之后返回旧值。这是我走了多远:
public aspect CorrelationIdAspect
{
private pointcut notPrivateMethod() :
execution(!private * *(..));
private pointcut methodWithContract(Contract contract) :
execution( * *.*(Contract, ..)) && args(contract, ..);
Object around(Contract contract) : methodWithContract(contract) && notPrivateMethod()
{
String oldCorrelationId = MDC.get(Constants.CORRELATION_ID);
try
{
String id = contract.getId().toString();
MDC.put(Constants.CORRELATION_ID, id);
Object result = proceed(contract);
return result;
}
finally
{
MDC.put(Constants.CORRELATION_ID, oldCorrelationId);
}
}
}
现在我希望这个建议只应用于具有类型字段的类
org.apache.logging.log4j.Logger
因为 - 显然 - 没有记录器的类不需要设置和恢复相关 ID。有人知道如何实现吗?
提前谢谢了!
解决方案
您想使用编译器选项-XhasMember
(另请参阅我的其他答案),可以在 Eclipse 中找到:
然后你
- 为你的方面添加一个标记界面,
- 使用 ITD 来声明每个具有
static Logger
字段的类都应该实现该接口,并且 - 在标记接口及其所有实现类上匹配:
public aspect CorrelationIdAspect {
// 1. marker interface
private interface HasLogger {}
// 2. use ITD in order to declare that each class having a
// 'static Logger' field ought to implement that interface
declare parents :
hasfield(static Logger *) implements HasLogger;
private pointcut notPrivateMethod() :
execution(!private * *(..));
// 3. match on the marker interface and all its implementing classes
private pointcut hasLogger() :
within(HasLogger+);
private pointcut methodWithContract(Contract contract) :
execution(* *(Contract, ..)) && args(contract, ..);
Object around(Contract contract) :
methodWithContract(contract) && notPrivateMethod() && hasLogger()
{
System.out.println(thisJoinPoint);
return proceed(contract);
}
}
推荐阅读
- ios - XCode UI测试长按左右拖拽按钮
- elasticsearch - 日期之间的 Elasticsearch 聚合字段
- ruby-on-rails-4 - 如何使用符号调用活动记录搜索
- java - 如何将对象 (JAXB) 或 byte[] (来自此对象) 转换为 `org.w3c.dom.Element`?
- javascript - 使用 NodeJS 和 Mongoose 在 MongoDB 中打开了多个连接
- c# - 在 Crystal Reports 中获取数据库字段名称
- android - 如何使用虚线选择调整 TextView 高度、宽度和旋转
- c - C - Windows 和 Unix 上的可移植代码
- python - Graph.create_png 错误类型错误:序列项 0:预期的 str 实例,找到的字节
- c# - 如何在给定小数/精度的情况下获得下一个小数