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

 更新时间:2022年03月20日 14:06:06   作者:这儿有个昵称  
这篇文章主要介绍了Spring this调用当前类方法无法拦截,通过debug 查看这个proxyService1 和this的区别,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

先给出代码示例

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 拦截方法不会被执行

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

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

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

发现是和之前的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无法拦截内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringCache之 @CachePut的使用

    SpringCache之 @CachePut的使用

    这篇文章主要介绍了SpringCache之 @CachePut的使用,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Mybatis实现分页查询的详细流程

    Mybatis实现分页查询的详细流程

    这篇文章主要给大家介绍了关于Mybatis实现分页查询的详细流程,MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架,需要的朋友可以参考下
    2023-08-08
  • java事件处理模型知识点总结

    java事件处理模型知识点总结

    在本篇文章里小辫给大家分享的是一篇关于java事件处理模型知识点总结内容,有兴趣的朋友们可以学习下。
    2021-01-01
  • 关于HashMap相同key累加value的问题

    关于HashMap相同key累加value的问题

    这篇文章主要介绍了关于HashMap相同key累加value的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • java生成json实现隐藏掉关键属性

    java生成json实现隐藏掉关键属性

    这篇文章主要介绍了java生成json实现隐藏掉关键属性,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 详解Java设计模式之外观模式

    详解Java设计模式之外观模式

    在Java开发中,设计模式是一种十分常见的编程思想,它可以帮助我们解决很多实际开发中的问题,本篇文章将介绍一种常见的设计模式——外观模式,并结合实际的开发场景进行讲解,需要的朋友可以参考下
    2023-06-06
  • SpringBoot如何读取mock数据(高效调试接口)

    SpringBoot如何读取mock数据(高效调试接口)

    本文介绍如何在SpringBoot项目中读取resources目录下的mock数据文件,以便高效调试接口,在 Spring Boot 项目中,通常会将静态资源或配置文件放在 src/main/resources 目录下,下面通过实例给大家详细介绍,感兴趣的朋友一起看看吧
    2024-12-12
  • spring+Jpa多数据源配置的方法示例

    spring+Jpa多数据源配置的方法示例

    这篇文章主要介绍了spring+Jpa多数据源配置的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • java 终止线程的4种方式小结

    java 终止线程的4种方式小结

    本文主要介绍了java终止线程的4种方式小结,包含布尔标志位,interrupt()方法,stop()方法和Thread.interrupt()方法,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • SpringBoot3.0整合chatGPT的完整步骤

    SpringBoot3.0整合chatGPT的完整步骤

    ChatGPT是OpenAI推出的一个语言模型系统,它能够实时回答用户提问,包括聊天、纠正语法错误,甚至是写代码、写剧本等,由于可玩性很高,迅速在全球范围内风靡起来,下面这篇文章主要给大家介绍了关于SpringBoot3.0整合chatGPT的完整步骤,需要的朋友可以参考下
    2022-12-12

最新评论