最近在开发中遇到一个问题,当在Service中定义了一个方法并且切入之后,从Controller里面调用该方法可以实现切入,但是当在同一个Service中实现另一方法并调用改方法时却无法切入。代码类似于:
1、service
package zmx.spring.aop.test2;import org.springframework.aop.framework.AopContext;public class TestService { public void callMethodA(){ System.out.println("Method A is called"); callMethodB(); //----------->AOP不能 //((TestService) AopContext.currentProxy()).callMethodB(); System.out.println("Method A is called over"); } public void callMethodB(){ System.out.println("Method B is called"); }}
2、aspect
package zmx.spring.aop.test2;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;@Aspectpublic class TestAspect { @AfterReturning("execution(* zmx.spring.aop.test2.TestService.callMethodB(..))") public void after() { System.out.println("after call and do something."); }}
3、mainTest
package zmx.spring.aop.test2;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainTest { @Test public void testCallMethodA(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("zmx/spring/applicationContext.xml"); TestService testService = ctx.getBean("testService", TestService.class); testService.callMethodA(); }}
4、applicationContext.xml
<?xml vers