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 相关文章推荐
springboot中一些比较常用的注解总结
Jun 11 Java/Android
详解Java实践之建造者模式
Jun 18 Java/Android
SpringCloud的JPA连接PostgreSql的教程
Jun 26 Java/Android
Spring整合Mybatis的全过程
Jun 28 Java/Android
解决SpringBoot文件上传临时目录找不到的问题
Jul 01 Java/Android
java中用float时,数字后面加f,这样是为什么你知道吗
Sep 04 Java/Android
Java tomcat手动配置servlet详解
Nov 27 Java/Android
Java版 简易五子棋小游戏
May 04 Java/Android
解决Springboot PostMapping无法获取数据的问题
May 06 Java/Android
Spring中bean集合注入的方法详解
Jul 07 Java/Android
前端与RabbitMQ实时消息推送未读消息小红点实现示例
Jul 23 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实现框架(一)
2006/10/09 PHP
PHPWind 发帖回帖Api PHP版打包下载
2010/02/08 PHP
smarty缓存用法分析
2014/12/16 PHP
php上传大文件失败的原因及应对策略
2015/10/20 PHP
php使用APC实现实时上传进度条功能
2015/10/26 PHP
PHP count()函数讲解
2019/02/03 PHP
IE与Firefox下javascript getyear年份的兼容性写法
2007/12/20 Javascript
javascript中字符串替换函数replace()方法与c# 、vb 替换有一点不同
2010/06/25 Javascript
JavaScript中将一个值转换为字符串的方法分析[译]
2012/09/21 Javascript
JS加jquery简单实现标签元素的显示或隐藏
2013/09/23 Javascript
JavaScript返回上一页的三种方法及区别介绍
2015/07/04 Javascript
Jqgrid之强大的表格插件应用
2015/12/02 Javascript
学习javascript面向对象 javascript实现继承的方式
2016/01/04 Javascript
使用PBFunc在Powerbuilder中支付宝当面付款功能
2016/10/01 Javascript
node学习记录之搭建web服务器教程
2017/02/16 Javascript
JS正则表达式验证密码格式的集中情况总结
2017/02/23 Javascript
angularjs中使用ng-bind-html和ng-include的实例
2017/04/28 Javascript
Three.js 再探 - 写一个微信跳一跳极简版游戏
2018/01/04 Javascript
JavaScript运行原理分析
2018/02/09 Javascript
Vue打包后访问静态资源路径问题
2019/11/08 Javascript
微信小程序实现吸顶效果
2020/01/08 Javascript
[01:23]2019完美世界全国高校联赛(春季赛)合肥全国总决赛
2019/06/10 DOTA
python网络编程之文件下载实例分析
2015/05/20 Python
Python外星人入侵游戏编程完整版
2020/03/30 Python
使用Python写一个小游戏
2018/04/02 Python
基于Python生成个性二维码过程详解
2020/03/05 Python
加拿大在线隐形眼镜和眼镜店:VisionPros
2019/10/06 全球购物
美国折扣地毯销售网站:Rugs.com
2020/03/27 全球购物
机电专业大学生求职信
2013/10/04 职场文书
心得体会范文
2014/01/04 职场文书
党员批评与自我批评
2014/02/12 职场文书
优乐美广告词
2014/03/14 职场文书
应届生求职信范文
2014/06/30 职场文书
暑期政治学习心得体会
2014/09/02 职场文书
湘江北去观后感
2015/06/15 职场文书
Python Pandas模块实现数据的统计分析的方法
2021/06/24 Python