2014-01-29 39 views
1

所以,我在將方面添加到已創建的系統時遇到了問題。問題 - 切入點不適用於某些類。 例如此代碼的工作好:定義爲在這個ApplicationContext豆春天的建議並不適用於某些類的某些方法

<aop:config proxy-target-class="true"> 
     <aop:pointcut id="addSubmitListener" 
         **expression="execution (* com.solutions.foo.ClassA.methodA(..))"/>** 
     <aop:aspect ref="hijackBeforeAddSubmitListenerBean"> 
      <aop:around method="proceedWhileNotDash" pointcut-ref="addSubmitListener"/> 
     </aop:aspect> 
    </aop:config> 

ClassA的。

現在,其他樣本。此示例不起作用。在其他的applicationContext定義

<aop:pointcut id="addSubmitListener" 
         expression="execution (* com.click.otherfoo.ClassB.methodB(..))"/> 

B級,與<import resource="classpath*:...

進口也多了一個差的methodB - 已void類型,了methodA - 不

回答

0

我只是想這可能是也可能不是你的組態。但是你提到的代碼對我來說工作得很好。這是我根據你的問題和提到的信息嘗試過的。

如果我只是更改anotherConfig.xml文件中的bean定義,它也可以很好地工作於ClassA。工程ClassB

的applicationContext.xml

<import resource="classpath*:anotherConfig.xml"/> 

<context:component-scan base-package="com.appContextexample"/> 
<aop:aspectj-autoproxy/> 

<aop:config proxy-target-class="true"> 
     <aop:pointcut id="addSubmitListener" expression="execution (* com.click.otherfoo.ClassB.methodB(..))"/> 
     <aop:aspect ref="hijackBeforeAddSubmitListenerBean"> 
      <aop:around method="proceedWhileNotDash" pointcut-ref="addSubmitListener"/> 
     </aop:aspect> 
    </aop:config> 

<bean id = "hijackBeforeAddSubmitListenerBean" class = "com.appContextexample.HijackBeforeAddSubmitListenerBean"/> 

</beans> 

anotherConfig.xml

<bean id="classA" class="com.click.otherfoo.ClassB"></bean> 

Application.java這是起點。

public class Application { 

    public static void main(final String[] args) { 
     final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
     final ClassB bean = (ClassB) context.getBean("classA"); 
     bean.methodB("test"); 

     System.out.println("appContext created" + bean); 
    } 

ClassB.java

package com.click.otherfoo; 

public class ClassB { 

    public void methodB(final String str) { 
     System.out.println(str); 
    } 

} 

最後的看點。

public class HijackBeforeAddSubmitListenerBean { 

    public Object proceedWhileNotDash(final ProceedingJoinPoint pjp) throws Throwable { 
     System.out.println("Called the aspect"); 
     pjp.proceed(); 
     return null; 

    } 
相關問題