spring aop无法拦截类内部的方法调用
1.概念
拦截器的实现原理就是动态代理,实现AOP机制。Spring 的代理实现有两种:一是基于 JDK Dynamic Proxy 技术而实现的;二是基于 CGLIB 技术而实现的。如果目标对象实现了接口,在默认情况下Spring会采用JDK的动态代理实现AOP
2.问题
在类C中,方法A调用方法B,
B方法被AOP拦截。(可能是方法B上在另外的@Aspect切面定义上了切点/也可能是加了@Transactional事务注解,底层原理也是AOP实现),最终A-》B ,B并不会触发AOP。
3.解决方案
1.手动获取代理对象
1.将当前的代理类暴露给线程使用,以下2种自己选一个实现即可。
注解实现方案:springboot:启动类上加注解:@EnableAspectJAutoProxy(exposeProxy=true):
配置实现方案:<aop:aspectj-autoproxy expose-proxy="true" />
2.A中调用B:不要直接用this(因为this是目标对象,自然无法实现代理类的增强方法@before等),而是先去尝试获取代理类:UserServiceImpl service = AopContext.currentProxy() != null ? (UserService)AopContext.currentProxy() : this;
2.去除AOP切面增强,把切面方法单独封装接口方法,在需要的地方调用
spring aop无法拦截类内部的方法调用的更多相关文章
- Spring AOP无法拦截Controller中的方法
想使用AOP Annotation配置Spring MVC的Controller进行拦截, 发现无法拦截Controller的方法, 却可以拦截Service层的方法. 一开始: Spring的配置文 ...
- Spring aop:decare-parent 为类增加新的方法
Spring aop:decare-parent 为类增加新的方法: 使用XML配置的方式: XML: <?xml version="1.0" encoding=" ...
- Spring @Cacheable注解 && 事务@Transactional 在同一个类中的方法调用不生效
@Cacheable 注解在对象内部调用不会生效 代码示例:ProductServiceImpl.java public List<ProductInfoVO> getProductLis ...
- Spring AOP开发时如何得到某个方法内调用的方法的代理对象?
Spring AOP开发时如何得到某个方法内调用的方法的代理对象? 问题阅读起来拗口,看代码 在方法中调用其他方法很常见,也经常使用,如果在一个方法内部调用其他方法,比如 public class U ...
- Spring AOP不拦截从对象内部调用的方法原因
拦截器的实现原理很简单,就是动态代理,实现AOP机制.当外部调用被拦截bean的拦截方法时,可以选择在拦截之前或者之后等条件执行拦截方法之外的逻辑,比如特殊权限验证,参数修正等操作. 但是最近在项目中 ...
- Spring AOP无法拦截内部方法调用
当在同一个类中,A方法调用B方法时,AOP无法工作的问题 假设一个接口里面有两个方法: package demo.long; public interface CustomerService { pu ...
- Spring AOP获取拦截方法的参数名称跟参数值
注意:这种方式需要JDK1.8版本支持 开始:http://www.cnblogs.com/wing7319/p/9592184.html 1.aop配置: <aop:aspectj-autop ...
- Spring AOP(基于代理类的AOP实现)
#基于代理类的AOP实现:step1: 1 package com.sjl.factorybean; /**切面类*/ import org.aopalliance.intercept.MethodI ...
- spring---aop(3)---Spring AOP的拦截器链
写在前面 时间断断续续,这次写一点关于spring aop拦截器链的记载.至于如何获取spring的拦截器,前一篇博客已经写的很清楚(spring---aop(2)---Spring AOP的JDK动 ...
随机推荐
- elbow 求拐点
distancePointLine <- function(x, y, slope, intercept) { ## x, y is the point to test. ## slope, ...
- Speak Confident English
I hope you don'd mind me asking... I know I shouldn't ask, but.... - Well, before I answer that ...
- Linux出现wrong ELF class: ELFCLASS64
安装软件时出现问题 ×.so.×:wrong ELF class: ELFCLASS64 ,大致的意思是软件是32位的,需要32位的 ×.so.×动态链接库,而系统是64位的所提供的该 动态链接库 ...
- C# 创建对象的方法
1.实例化方法,也就是new(): //where T:new () 表示T必须有构造函数 public static T Create<T> where T:new () { retur ...
- jQuery 省份选择
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 设计图与html 对比
简易打开旧版火狐 网页版火狐添加组件 新版有时也会没有 谷歌是腾讯的
- javax.net.ssl.SSLException: Certificate doesn't match any of the subject alternative names
问题:在使用 org.apache.http.*下的 CloseableHttpClient 发送https请求时报了以上错误 解决方案一:使用java.net.HttpURLConnection i ...
- redis缓存工具类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis ...
- 用mybatis中的insert方法插入数据,返回值为1,但数据库却没有数据
刚才在写东西的时候,用mybatis中的 <insert id="add" parameterType="cn.entity.Computer"> ...
- eclipse工具类及插件(Eclipse超好用的插件推荐)
https://blog.csdn.net/ghostxbh/article/details/80054948