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 相关文章推荐
为什么在foreach循环中JAVA集合不能添加或删除元素
Jun 11 Java/Android
Java 将PPT幻灯片转为HTML文件的实现思路
Jun 11 Java/Android
解析Java异步之call future
Jun 14 Java/Android
Springboot如何使用logback实现多环境配置?
Jun 16 Java/Android
Netty结合Protobuf进行编解码的方法
Jun 26 Java/Android
Spring Boot两种全局配置和两种注解的操作方法
Jun 29 Java/Android
Java反应式框架Reactor中的Mono和Flux
Jul 25 Java/Android
JVM钩子函数的使用场景详解
Aug 23 Java/Android
java调用Restful接口的三种方法
Aug 23 Java/Android
解决springboot druid数据库连接失败后一直重连的方法
Apr 19 Java/Android
Elasticsearch Recovery 详细介绍
Apr 19 Java/Android
tree shaking对打包体积优化及作用
Jul 07 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下判断网址是否有效的代码
2011/10/08 PHP
PHP用FTP类上传文件视频等的简单实现方法
2016/09/23 PHP
基于ThinkPHP5.0实现图片上传插件
2017/09/25 PHP
JS target与currentTarget区别说明
2011/08/28 Javascript
将查询条件的input、select清空
2014/01/14 Javascript
JS中怎样判断undefined(比较不错的方法)
2014/03/27 Javascript
JavaScript声明变量时为什么要加var关键字
2014/09/29 Javascript
基于jQuery实现以手风琴方式展开和折叠导航菜单
2016/01/28 Javascript
Jquery实现$.fn.extend和$.extend函数
2016/04/14 Javascript
JS原型与原型链的深入理解
2017/02/15 Javascript
vue 运用mock数据的示例代码
2017/11/07 Javascript
基于vue实现可搜索下拉框定制组件
2020/03/26 Javascript
在vue中实现点击选择框阻止弹出层消失的方法
2018/09/15 Javascript
vue添加axios,并且指定baseurl的方法
2018/09/19 Javascript
详解小程序如何避免多次点击,重复触发事件
2019/04/08 Javascript
nodejs中实现修改用户路由功能
2019/05/24 NodeJs
vue实现的多页面项目如何优化打包的步骤详解
2020/07/19 Javascript
js制作提示框插件
2020/12/24 Javascript
[01:46]2020完美世界全国高校联赛秋季赛报名开启
2020/10/15 DOTA
Python打印scrapy蜘蛛抓取树结构的方法
2015/04/08 Python
Python实现以时间换空间的缓存替换算法
2016/02/19 Python
Python的“二维”字典 (two-dimension dictionary)定义与实现方法
2016/04/27 Python
Python实现读取txt文件并画三维图简单代码示例
2017/12/09 Python
用scikit-learn和pandas学习线性回归的方法
2019/06/21 Python
Pycharm中import torch报错的快速解决方法
2020/03/05 Python
python matplotlib绘制三维图的示例
2020/09/24 Python
Python定时任务框架APScheduler原理及常用代码
2020/10/05 Python
德国药房apodiscounter中文官网:德国排名前三的网上药店
2019/06/03 全球购物
数据库面试要点基本概念
2013/10/31 面试题
国际商贸专业自荐信
2014/06/09 职场文书
庆七一宣传标语
2014/10/08 职场文书
装修公司管理制度
2015/08/05 职场文书
销售口号霸气押韵
2015/12/24 职场文书
Pytorch 如何实现LSTM时间序列预测
2021/05/17 Python
避坑之 JavaScript 中的toFixed()和正则表达式
2022/04/19 Javascript
什么是clearfix (一文搞清楚css清除浮动clearfix)
2023/05/21 HTML / CSS