2010-09-07 73 views
1

我做了一个分析方法:在tld.mycompany.business.aspects.SystemArchitectureAspectJ的,一般不切入点构造

@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; 
} 
} 

而定义的切入点为

@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() {} 

我想剖析我的服务层中不是构造函数和异常的所有方法(所以t我没有得到“围绕初始化不支持(编译器限制)”和“围绕预初始化不支持(编译器限制)”警告)并忽略dataType(),我已经有几个。

但是,我仍然收到有关构造函数和异常的警告。它似乎也建议任何Java方法,所以调试我的应用程序几乎是不可能的,因为我为每一行都提供了很多建议。 Eclipse告诉我,它只有2747条针对profileBusiness系列的建议。

显然我一定误解了一些东西,但是什么?我怎样才能使它成为tld.mycompany.business层次结构中以Impl结尾的类中的所有方法(构造函数除外)?

干杯

回答

3

你的切入点的这一部分:

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

目标在所有的*默认地将Impl类的所有joinpoints。这就是为什么你在每一行看到建议标记的原因。

您需要添加一行:

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

此外,处理器(java.lang.Exception的)是没有意义的,因为处理器的切入点是指catch子句(执行切入点独占)。

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

+0

非常感谢,我没有发现内部和执行之间的区别。你会如何建议我将切入点写成你所建议的执行语句,但不包括构造函数? – niklassaers 2010-09-16 08:39:25

+0

你可以使用'initialization(ConstructorPattern)'切入点。应该是这样的:'!初始化(*新(..))' – Thorben 2013-05-27 17:10:54

相关问题