【问题标题】:AspectJ, general pointcuts without constructorsAspectJ,没有构造函数的通用切入点
【发布时间】:2010-09-07 10:55:17
【问题描述】:

我做了一个分析方法:

@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 结尾的类中的所有方法(构造函数除外)?

干杯

尼克

【问题讨论】:

    标签: java constructor aspectj pointcuts


    【解决方案1】:

    这部分切入点:

    within(tld.mycompany.business..*Impl)
    

    针对所有*Impl 类中的所有连接点。这就是为什么您在每一行都看到建议标记的原因。

    你需要添加如下一行:

    execution(* tld.mycompany.business..*Impl.*(..))
    

    另外,!handler(java.lang.Exception) 没有意义,因为处理程序切入点指的是 catch 子句(不包括执行切入点)。

    最后,您的publicConstructor 切入点对我来说似乎是错误的。难道你不想删除受保护的、私有的和包保护的构造函数吗?

    【讨论】:

    • 非常感谢,我没有搞清楚内部和执行之间的区别。您如何建议我将切入点编写为您建议但不包括构造函数的执行语句?
    • 您可以使用initialization(ConstructorPattern) 切入点。应该是这样的:! initialization(*.new(..))
    猜你喜欢
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多