2010-12-09 123 views
6

我配置了Spring的AspectJ,它能夠在捕獲從類中調用的公共方法時正常工作。現在我想要做這樣的事情:Aspectj和捕捉私有或內部方法

public class SomeLogic(){ 

    public boolean someMethod(boolean test){ 

     if(test){ 
     return innerA(); 
     } else { 
     return innerB(); 
     } 
    } 


    private boolean innerA() {// some logic} 
    private boolean innerA() {// some other logic} 

} 

SomeLogic是SpringBean。 innerA()和innerB()方法可以聲明爲private或public - someMethod()方法是從Struts動作中調用的。是否有可能使用AspectJ從someMethod()調用的方法innerA()或innerB()?

我的配置(基於XML):

<aop:aspect id="innerAAspect" ref="INNER_A"> 
     <aop:pointcut id="innerAService" expression="execution(* some.package.SomeLogic.innerA(..))"/> 
    </aop:aspect> 

    <aop:aspect id="innerAAround" ref="INNER_A"> 
     <aop:around pointcut-ref="innerAService" method="proceed"/> 
    </aop:aspect> 


    <aop:aspect id="innerBAspect" ref="INNER_B"> 
     <aop:pointcut id="innerBService" expression="execution(* some.package.SomeLogic.innerB(..))"/> 
    </aop:aspect> 

    <aop:aspect id="innerBAround" ref="INNER_B"> 
     <aop:around pointcut-ref="innerBService" method="proceed"/> 
    </aop:aspect> 
+0

告訴我們您的AspectJ表達式的含義。我以前從來沒有遇到公共vs私人的問題。 – 2010-12-09 19:04:10

+0

你可以發表一個例子嗎? – awonline 2010-12-09 19:49:11

回答

5

是很容易趕上與AspectJ的私有方法。

所有的私有方法之前打印一個句子一個例子:

@Pointcut("execution(private * *(..))") 
public void anyPrivateMethod() {} 

@Before("anyPrivateMethod()") 
public void beforePrivateMethod(JoinPoint jp) { 
    System.out.println("Before a private method..."); 
} 

如果您熟悉Eclipse,我建議制定的AspectJ與STS或只安裝AJDT plugin

有關Spring AOP功能的更多信息,請參見Spring參考文檔here