关于AOP无法切入同类调用方法的问题

在Spring AOP中,当Service内部方法互相调用时,切面无法正常工作。原因是此类调用未通过代理,因此不会触发切面。解决方案是设置'proxy-target-class'为'true'和'expose-proxy'为'true',并通过AopContext.currentProxy()获取代理对象,然后使用代理对象调用方法,从而实现切入。
摘要由CSDN通过智能技术生成
               

最近在开发中遇到一个问题,当在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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值