Spring this调用当前类方法无法拦截的示例代码


Posted in Java/Android onMarch 20, 2022

先给出代码示例

package com.example.demo.service;
import org.springframework.stereotype.Service;
@Service
public class ProxyService {
    public void  testA(){
        System.out.println("进入A");
        this.testB();
    }
    public void testB() {
        System.out.println("进入b");
    }

}
package com.example.demo.annotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectjTest {
    @Around("execution(* com.example.demo.service.ProxyService.testB())")
    public void recordProxy(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        joinPoint.proceed();
        long end = System.currentTimeMillis();
        System.out.println("花费时间:"+(end-start));
    }
}
package com.example.demo.api;
import com.example.demo.service.ProxyService;
import com.example.demo.service.UserService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class ProxyApi {
//    @Autowired
//    ProxyService proxyService1;
    @Autowired
    private ApplicationContext applicationContext;
    @PostMapping("/proxy")
    public String test1() {
        ProxyService proxyService1 =  applicationContext.getBean(ProxyService.class);;
        proxyService1.testA();
        return "string";
    }
}

运行上面的代码会发现 配置aop 拦截方法不会被执行

Spring this调用当前类方法无法拦截的示例代码

我们通过debug 查看这个proxyService1 和this的区别,看看他们的值是什么

Spring this调用当前类方法无法拦截的示例代码

Spring this调用当前类方法无法拦截的示例代码

发现不一样,其实这就是问题的原因。

1、当我们在aop配置拦截的时候会指定类下面的方法路径,在spring启动的时候会先去加载这个ProxyService类,生成一个bean,但是因为你用aop配置了,所以需要代理这个ProxyService类,所以最终存在spring容器中的bean对象就是被代理后的bean对象。所以,我们在用容器获取bean或者用依赖注入获取bean的地址路径显示的是被代理后的bean 。
2、this获取的当前对象方法的一个引用,所以在调用testB方法的时候用的不是被代理的对象,自热不会经过aop拦截,原理和我们使用普通动态代理一样,只能是代理对象才能走自定义的方法。
3、可以通过debug 查看当ProxyService类被代理前和后的zhi值

Spring this调用当前类方法无法拦截的示例代码

Spring this调用当前类方法无法拦截的示例代码

发现是和之前的debug截图里面的值相符合的哈。

解决方法,就是在调用testB方法的时候用spring容器里的bean对象

@Service
public class ProxyService {
    @Autowired
    private  ProxyService proxyService;
    
    public void  testA(){
        System.out.println("进入A");
        proxyService.testB();
    }
    public void testB() {
        System.out.println("进入b");
}

或者

@Service
public class ProxyService {
    public void  testA(){
        System.out.println("进入A");
        ProxyService currentProxy = (ProxyService) AopContext.currentProxy();
        currentProxy.testB();
    }
    public void testB() {
        System.out.println("进入b");
    }
}

最终结果正确

Spring this调用当前类方法无法拦截的示例代码

到此这篇关于Spring this调用当前类方法无法拦截的文章就介绍到这了,更多相关Spring this无法拦截内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
Spring Boot 排除某个类加载注入IOC的操作
Aug 02 Java/Android
Java org.w3c.dom.Document 类方法引用报错
Aug 07 Java/Android
使用jpa之动态插入与修改(重写save)
Nov 23 Java/Android
JVM的类加载器和双亲委派模式你了解吗
Mar 13 Java/Android
Java实现二分搜索树的示例代码
Mar 17 Java/Android
MyBatis配置文件解析与MyBatis实例演示
Apr 07 Java/Android
教你在 Java 中实现 Dijkstra 最短路算法的方法
Apr 08 Java/Android
Java后端 Dubbo retries 超时重试机制的解决方案
Apr 14 Java/Android
Android自定义双向滑动控件
Apr 19 Java/Android
Java+swing实现抖音上的表白程序详解
Jun 25 Java/Android
利用Java连接Hadoop进行编程
Jun 28 Java/Android
基于Android10渲染Surface的创建过程
Aug 14 Java/Android
SpringCloud Feign请求头删除修改的操作代码
Mar 20 #Java/Android
JavaWeb实现显示mysql数据库数据
关于Mybatis中SQL节点的深入解析
springboot 自定义配置 解决Boolean属性不生效
Mar 18 #Java/Android
使用Java去实现超市会员管理系统
Mar 18 #Java/Android
详解Spring Security中的HttpBasic登录验证模式
RestTemplate如何通过HTTP Basic Auth认证示例说明
You might like
php类的自动加载操作实例详解
2016/09/28 PHP
PHP等比例压缩图片的实例代码
2018/07/26 PHP
通过JavaScript使Div居中并随网页大小改变而改变
2013/06/24 Javascript
设置jsf的选择框h:selectOneMenu为不可编辑状态的方法
2014/01/07 Javascript
iframe窗口高度自适应的实现方法
2014/01/08 Javascript
AngularJS 入门教程之HTML DOM实例详解
2016/07/28 Javascript
jquery+Jscex打造游戏力度条
2020/09/12 Javascript
数组Array的排序sort方法
2017/02/17 Javascript
微信小程序 本地数据读取实例
2017/04/27 Javascript
jQuery模拟实现天猫购物车动画效果实例代码
2017/05/25 jQuery
在Vue项目中引入腾讯验证码服务的教程
2018/04/03 Javascript
vue-rx的初步使用教程
2018/09/21 Javascript
使用typescript构建Vue应用的实现
2019/08/26 Javascript
2020淘宝618理想生活列车自动领喵币js脚本的代码
2020/06/02 Javascript
UEditor 自定义图片视频尺寸校验功能的实现代码
2020/10/20 Javascript
javascript实现京东快递单号的查询效果
2020/11/30 Javascript
浅谈Python中的数据类型
2015/05/05 Python
给Python入门者的一些编程建议
2015/06/15 Python
Python二叉树的定义及常用遍历算法分析
2017/11/24 Python
python实现时间o(1)的最小栈的实例代码
2018/07/23 Python
在Pycharm中设置默认自动换行的方法
2019/01/16 Python
Python操作MySQL数据库的两种方式实例分析【pymysql和pandas】
2019/03/18 Python
python mac下安装虚拟环境的图文教程
2019/04/12 Python
python程序中的线程操作 concurrent模块使用详解
2019/09/23 Python
python利用platform模块获取系统信息
2020/10/09 Python
Python项目打包成二进制的方法
2020/12/30 Python
美国知名的百货清仓店:Neiman Marcus Last Call
2016/08/03 全球购物
如果有两个类A,B,怎么样才能使A在发生一个事件的时候通知B
2016/03/12 面试题
采购部部门职责
2013/12/15 职场文书
函授药学自我鉴定
2014/02/07 职场文书
大学军训感言400字
2014/03/11 职场文书
企业宣传口号
2014/06/12 职场文书
六年级学生期末评语
2014/12/26 职场文书
上市公司财务总监岗位职责
2015/04/03 职场文书
经销商会议开幕词
2016/03/04 职场文书
node快速搭建后台的实现步骤
2022/02/18 NodeJs