AspectJ,没有构造函数的通用切入点
我做了一个分析方法:
@Around("tld.mycompany.business.aspects.SystemArchitecture.inServiceLayer() && !tld.mycompany.business.aspects.SystemArchitecture.publicConstructor()")
public Object profileBusiness(ProceedingJoinPoint pjp) throws Throwable {
try {
long start = System.currentTimeMillis();
String name = pjp.getSourceLocation().toString() + " " + pjp.getSignature().getName();
Object output = pjp.proceed();
long elapsedTime = System.currentTimeMillis() - start;
if(elapsedTime > 100)
System.err.println("TimerAspect: Businessmethod " + name + " execution time: " + elapsedTime + " ms.");
return output;
} catch (Exception ex) {
ex.printStackTrace(System.err);
throw ex;
}
}
并在 tld.mycompany.business.aspects.SystemArchitecture 中定义了切入点,并且
@Pointcut("execution(public new(..))")
public void publicConstructor() {}
我
@Pointcut("within(tld.mycompany.business..*Impl) &&
!execution(private * tld.mycompany.business.*.dataType()) &&
!handler(java.lang.Exception)")
public void inServiceLayer() {}
想分析我的服务层中不是构造函数和异常的所有方法(这样我就不会得到“不支持初始化周围(编译器限制)”和“不支持预初始化周围(编译器限制)”警告)并忽略我在其中得到的一些 dataType() 。
但是,我仍然收到有关构造函数和异常的警告。它似乎还对任何 Java 方法提供建议,因此调试我的应用程序几乎变得不可能,因为我为每一行都提供了许多建议。 Eclipse 告诉我,仅针对 profileBusiness 线就有 2747 条建议。
显然我一定误解了什么,但是什么?我怎样才能专门围绕 tld.mycompany.business 层次结构中以 Impl 结尾的类中的所有方法(构造函数除外)?
干杯
尼克
I've made a profiling method:
@Around("tld.mycompany.business.aspects.SystemArchitecture.inServiceLayer() && !tld.mycompany.business.aspects.SystemArchitecture.publicConstructor()")
public Object profileBusiness(ProceedingJoinPoint pjp) throws Throwable {
try {
long start = System.currentTimeMillis();
String name = pjp.getSourceLocation().toString() + " " + pjp.getSignature().getName();
Object output = pjp.proceed();
long elapsedTime = System.currentTimeMillis() - start;
if(elapsedTime > 100)
System.err.println("TimerAspect: Businessmethod " + name + " execution time: " + elapsedTime + " ms.");
return output;
} catch (Exception ex) {
ex.printStackTrace(System.err);
throw ex;
}
}
And defined the pointcuts in tld.mycompany.business.aspects.SystemArchitecture as
@Pointcut("execution(public new(..))")
public void publicConstructor() {}
and
@Pointcut("within(tld.mycompany.business..*Impl) &&
!execution(private * tld.mycompany.business.*.dataType()) &&
!handler(java.lang.Exception)")
public void inServiceLayer() {}
I want to profile all methods in my service layer that aren't constructors and exceptions (so that I don't get the "around on initialization not supported (compiler limitation)" and "around on pre-initialization not supported (compiler limitation)" warnings) and ignore dataType() that I've got in a few.
However, I still get the warnings about the constructor and exceptions. It also seems to advice just about any Java method there is, so debugging my application has become close to impossible as I hit many advices for every line. Eclipse tells me it has 2747 advices for the profileBusiness line alone.
Clearly I must have misunderstood something, but what? How can I make it specifically to being Around all methods (except for constructors) in the classes within the tld.mycompany.business hierarchy that end with Impl?
Cheers
Nik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
切入点的这一部分:
针对所有
*Impl
类中的所有连接点。这就是为什么您会在每一行上看到建议标记。您需要添加如下行:
另外,
!handler(java.lang.Exception)
是没有意义的,因为处理程序切入点引用了 catch 子句(不包括执行切入点)。最后,您的
publicConstructor
切入点对我来说似乎是错误的。难道您不想删除受保护的、私有的和受包保护的构造函数吗?This part of your pointcut:
targets all joinpoints in all of your
*Impl
classes. That is why you are seeing advice markers on every line.You need to add a line like:
Also,
!handler(java.lang.Exception)
is meaningless since the handler pointcut refers to catch clauses (that exclusive of execution pointcuts).Finally, your
publicConstructor
pointcut seems wrong to me. Wouldn't you also want to remove protected, private and package protected constructors?