2012-05-17 11 views
0

我想使用AOP來剖析數據訪問類中的方法。簡化的,它看起來像這樣:我應該如何爲內部類編寫切入點表達式?

package com.example.dataservices.dao; 

public class DaoImpl extends AbstractDao implements Dao 
{ 
    protected static abstract class CallbackHandler 
    { 
     abstract void processRow(final ResultSet rset); 

     abstract DataPayload getPayload(); 

     boolean isCompanyPermitted(final Long companyId) 
     { 
      return true; 
     } 

     boolean isUserPermitted(final Long userId) 
     { 
      return true; 
     } 
    } 

    protected static class CompanyCallbackHandler extends CallbackHandler 
    { 
     DataPayload payload; 

     void processRow(final ResultSet rset) 
     { 
      if (isUserPermitted(rset.getlong(1))) { 
       // do stuff 
      } 
     } 

     DataPayload getPayload(final Long companyId) 
     { 
      this.payload = new DataPayload(); 
      // query the database 
      return this.payload; 
     } 
    } 

    public DataPayload getCompanyPayload(final Long companyId) 
    { 

     final CallbackHandler handler = new CompanyCallbackHandler(); 
     return handler.getPayload(companyId); 
    } 
} 

我的切入點表達式如下:

@Pointcut(「執行(* com.example.dataservices.dao .. (..) )「)

但是這個表達式只匹配getCompanyPayload()方法。我試圖匹配方法,如CompanyCallbackHandler從它的父類CallbackHandler繼承的isUserPermitted()。

任何意見將不勝感激。

克里斯

+0

你使用Spring AOP還是AspectJ? –

回答

0

我supose您正在使用的AspectJ,Spring AOP實現沒有任何posibilities。

上執行poincut的qualifing類型

是聲明的方法的類型,以便

execution(* com.example.dataservice.dao..*.*(..))將挑選上包DAO任何執行中的類,或者如果該方法被聲明在該類重寫內部類。繼承是不夠的。

+0

謝謝 - 我想你已經發現了這個問題。在重複重建並重構目標代碼以移除內部類之後,我終於發現Spring AOP僅適用於實例化爲Spring bean的公共方法。我可以理解這個原因,但這是一個非常嚴格的限制,我認爲它可以在Spring文檔中更清晰。 – Chris

+0

哇,很高興我讀到這個!這是一個非常好的理由,因爲我本來就是在使用Aspectj。我喜歡春天環境,並且將盡可能多的放棄。但顯然不是AOP。 – Dennis