简体   繁体   中英

Spring advice don't apply to some methods of the some classes

So, I have problem with adding the aspect to already created system. Problem - the pointcut doesn't work to some classes. For example this code works good:

<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 defined as a bean in this applicationContext.

Now, other sample. This sample doesn't work.

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

Class B defined in other applicationContext, imported with <import resource="classpath*:...

also one more difference methodB - has void type, and methodA - not

I just tried this which might or might not be your configuration. But the code as mentioned by you works perfectly fine for me. Here is what i have tried based on your question and mentioned information.

If i just change the bean definition in the anotherConfig.xml file, it works fine for ClassA as well. Works for ClassB too

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 This is the starting point.

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

}

And finally the Aspect.

public class HijackBeforeAddSubmitListenerBean {

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

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM