2010-09-07 74 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

相關問題