spring应用手册-AOP(注解)-(27)-EnableAspectJAutoProxy的exposeProxy属性

戴着假发的程序员出品 抖音ID:戴着假发的程序员 欢迎关注

EnableAspectJAutoProxy的exposeProxy属性

spring应用手册(第三部分)


前面我们以及解释完了AOP的所有配置和使用方式。

现在我们来看看下面的案例:

我们准备一个业务类:在业务类中我们有两个方法showMessage和formartMsg。我们再showMessage中调用formartMsg方法:

/**
 * @author 戴着假发的程序员
 * 
 * @description
 */
@Component
public class MessageService {
    public String showMessage(String info) {
        System.out.println("OtherInfoServcie-showMessage展示信息:"+info);
        this.formartMsg(info);
        return null;
    }
    public String formartMsg(String info){
        System.out.println("OtherInfoServcie-formartMsg对象消息"+info+"进行格式化");
        return  info;
    }
}

添加一个Aspect类,在其中增加一个前置通知:

/**
 * @author 戴着假发的程序员
 * 
 * @description
 */
@Component
@Aspect
public class DKAspect1 {
    @Pointcut("this(com.st.dk.demo8.service.MessageService)")
    public void pointcut1(){}

    @Before("pointcut1()")
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知,被增强的方法是:"+joinPoint.toString());
    }
}

测试:

ApplicationContext ac =
        new AnnotationConfigApplicationContext(AppConfig.class);
MessageService bean = ac.getBean(MessageService.class);
bean.showMessage("戴着假发的程序员");

结果:
在这里插入图片描述
这是我们会发现,shwoMessage方法被拦截了,但是formartMsg方法并没有被拦截。

这是什么原因,其实spring官方已经给了明确的解释,解释的内容有点繁杂,我就不在这里截图。

我简答解释一下,就是我们使用AOP,必然会生成一个MessageService的代理对象。所以我们调用showMessage方法就是调用了代理对象的showMessage方法,必然会被增强,但是在shwoMessage中使用的this并非代理对象,而是我们的原生对象,所以this.formartMsg必然不会被增强。

如果解决呢?

方案1:

我们在我们的业务类中注入一个自己本身,然后把this替换为注入的对象,就可以解决问题。
在这里插入图片描述
这种方式很显然,非常的不优雅。

所以spring 提供了其他的解决方案。

方案二:

修改EnableAspectJAutoProxy的属性exposeProxy为true,这是我们的代理对象接口会被暴漏在ThreadLocal中,我们就可以直接获取了。

@EnableAspectJAutoProxy(exposeProxy=true)

使用 方式:

/**
 * @author 戴着假发的程序员
 * 
 * @description
 */
@Component
public class MessageService {
    public String showMessage(String info) {
        System.out.println("OtherInfoServcie-showMessage展示信息:"+info);
        //使用AopContext的静态方法获取当前的代理对象
        ((MessageService)AopContext.currentProxy()).formartMsg(info);
        return null;
    }
    public String formartMsg(String info){
        System.out.println("OtherInfoServcie-formartMsg对象消息"+info+"进行格式化");
        return  info;
    }
}

在测试:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴着假发的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值