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 相关文章推荐
Java并发编程之Executor接口的使用
Jun 21 Java/Android
Java 中的 Unsafe 魔法类的作用大全
Jun 26 Java/Android
Spring Boot 实现敏感词及特殊字符过滤处理
Jun 29 Java/Android
Java SSM配置文件案例详解
Aug 30 Java/Android
Java 实现限流器处理Rest接口请求详解流程
Nov 02 Java/Android
正则表达式拆分url实例代码
Feb 24 Java/Android
Android超详细讲解组件ScrollView的使用
Mar 31 Java/Android
Spring Security使用单点登录的权限功能
Apr 03 Java/Android
JAVA长虹键法之建造者Builder模式实现
Apr 10 Java/Android
Android使用EventBus发送消息,Fragment中接收消息的方法会执行多次
Apr 24 Java/Android
Java 多线程协作作业之信号同步
May 11 Java/Android
Java中Dijkstra(迪杰斯特拉)算法
May 20 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
《星际争霸II》全新指挥官斯台特曼现已上线
2020/03/08 星际争霸
一个ubbcode的函数,速度很快.
2006/10/09 PHP
php cookie 登录验证示例代码
2009/03/16 PHP
php 把数字转换成汉字的代码
2015/07/21 PHP
php通过curl添加cookie伪造登陆抓取数据的方法
2016/04/02 PHP
PHP7新特性之抽象语法树(AST)带来的变化详解
2018/07/17 PHP
PHP实现的CURL非阻塞调用类
2018/07/26 PHP
PHP内置函数生成随机数实例
2019/01/18 PHP
用js生产批量批处理执行命令
2008/07/28 Javascript
用Javascript同时提交多个Web表单的方法
2009/12/26 Javascript
jQuery使用$获取对象后检查该对象是否存在的实现方法
2016/09/04 Javascript
微信小程序 引用其他js文件实现代码
2017/02/22 Javascript
微信小程序 ecshop地址三级联动实现实例代码
2017/02/28 Javascript
js实现单张图片平移切换效果
2017/10/11 Javascript
Angular简单验证功能示例
2017/12/22 Javascript
动态规划之矩阵连乘问题Python实现方法
2017/11/27 Python
Python之用户输入的实例
2018/06/22 Python
python 实现将多条曲线画在一幅图上的方法
2019/07/07 Python
Python的几种主动结束程序方式
2019/11/22 Python
Python要如何实现列表排序的几种方法
2020/02/21 Python
Python数组拼接np.concatenate实现过程
2020/04/18 Python
python 实现aes256加密
2020/11/27 Python
x-ua-compatible content=”IE=7, IE=9″意思理解
2013/07/22 HTML / CSS
Merrell迈乐澳大利亚网站:购买户外登山鞋
2017/05/28 全球购物
创造美妙香氛体验:Aera扩散器和香水
2018/11/25 全球购物
法国在线药房:1001Pharmacies
2021/03/07 全球购物
施惠特软件测试面试题以及笔试题
2015/05/13 面试题
中国文明网签名寄语
2014/01/18 职场文书
工作决心书
2014/03/11 职场文书
奠基仪式主持词
2014/03/20 职场文书
关于感恩的演讲稿400字
2014/08/26 职场文书
2014年学校党建工作汇报材料
2014/11/02 职场文书
故宫英文导游词
2015/01/31 职场文书
导游词之丹东鸭绿江
2019/10/24 职场文书
MySQL系列之十一 日志记录
2021/07/02 MySQL
纯 CSS 自定义多行省略的问题(从原理到实现)
2021/11/11 HTML / CSS