Spring AOP对嵌套方法不起作用的解决
作者:波波仔86 时间:2022-01-08 16:55:03
Spring AOP对嵌套方法不起作用
今天在调研系统操作记录日志时,好多教程都是借助于Spring AOP机制来实现。于是也采用这种方法来实现。在Service中的删除日志方法上注解自定义的切点,但是执行没有生效。
代码如下:
//尝试删除溢出日志
public synchronized void tryDelOverflowLog() {
logNum++;
if (logNum - LogConst.MAX_NUM > 0) {
int delNum = logNum - LogConst.MAX_NUM + LogConst.EXTRA_NUM;
logNum -= delNum;
removeOverflowLog(delNum);
}
}
//日志溢出后,删除最新入库的日志
@ServiceLog(type = LogConst.TYPE_LOG_RECORD, description = "操作日志缓存区溢出,系统自动清空缓存区")
public void removeOverflowLog(int delNum) {
custLogMapper.removeOverflowLog(delNum);
}
在使用 Spring AOP 的时候,我们从 IOC 容器中获取的 Service Bean 对象其实都是代理对象,而不是那些 Service Bean 对象本身,也就是说获取的并不是被代理对象或代理目标。当我在自己的 Service 类中使用 this 关键字嵌套调用同类中的其他方法时,由于 this 关键字引用的并不是该 Service Bean 对象的代理对象,而是其本身,故 Spring AOP 是不能拦截到这些被嵌套调用的方法的。
要解决这个问题
最简单的方法是把自身注入到自身,用注入的这个自身去调用本方法。或者你也可以不用spring aop而是用aspectj weaving,倒是可以测底的解决该问题。我采用的是把自身注入到自身中。
/**
* 通过注入自身解决,Spring AOP嵌套调用不生效的问题
*/
@Autowired
private ApplicationContext applicationContext;
private LogService self;
@PostConstruct
private void init() {
self = (LogService) applicationContext.getBean("logService");
}
//尝试删除溢出日志
public synchronized void tryDelOverflowLog() {
logNum++;
if (logNum - LogConst.MAX_NUM > 0) {
int delNum = logNum - LogConst.MAX_NUM + LogConst.EXTRA_NUM;
logNum -= delNum;
self.removeOverflowLog(delNum);
}
}
Spring AOP、嵌套调用失效及解决
加入注解
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
获取当前代理的接口
public interface ICurrentAopProxyService<T> {
default T getCurrentProxyService() {
return (T) AopContext.currentProxy();
}
}
需要嵌套调用的Service实现它
调用的时候改写代码
public SysMerchantVersion selectByMerchantId(Long merchantId) {
return getCurrentProxyService().getOne(new QueryWrapper<SysMerchantVersion>()
.lambda()
.eq(SysMerchantVersion::getMerchantId, merchantId));
}
来源:https://blog.csdn.net/bobozai86/article/details/78896487
标签:Spring,AOP,嵌套方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Flutter Widgets MediaQuery控件屏幕信息适配
2023-06-29 04:48:21
![](https://img.aspxhome.com/file/2023/2/120092_0s.jpg)
浅谈三分钟学习Java泛型中T、E、K、V、?的含义
2022-09-01 20:12:38
![](https://img.aspxhome.com/file/2023/2/64372_0s.jpg)
springboot2.0整合dubbo的示例代码
2021-11-30 06:07:11
![](https://img.aspxhome.com/file/2023/5/78045_0s.png)
Java聊天室之实现运行服务器与等待客户端连接
2023-11-23 08:16:17
![](https://img.aspxhome.com/file/2023/4/69114_0s.png)
详解Java线程池如何统计线程空闲时间
2022-11-09 07:41:10
Mybatis @SelectKey用法解读
2022-04-18 18:01:35
Java 数据结构中二叉树前中后序遍历非递归的具体实现详解
2023-02-14 12:51:18
![](https://img.aspxhome.com/file/2023/7/75757_0s.png)
C#中动态显示当前系统时间的实例方法
2023-06-20 14:46:02
SpringBoot Pom文件依赖及Starter启动器详细介绍
2022-10-08 19:30:20
![](https://img.aspxhome.com/file/2023/7/80987_0s.png)
C#导出Excel的示例详解
2021-12-03 01:55:51
Android之复选框对话框用法实例分析
2023-10-03 05:07:03
![](https://img.aspxhome.com/file/2023/1/84751_0s.png)
Spring Cloud Zuul路由网关服务过滤实现代码
2021-08-26 10:56:07
Java读取本地json文件及相应处理方法
2023-10-16 16:37:34
java ThreadLocal使用案例详解
2022-02-01 05:14:57
![](https://img.aspxhome.com/file/2023/9/72319_0s.jpg)
java递归算法实例分析
2022-02-09 07:59:17
![](https://img.aspxhome.com/file/2023/0/68170_0s.png)
C#实现把科学计数法(E)转化为正常数字值
2022-09-06 23:41:47
Spring security权限配置与使用大全
2022-03-05 15:37:21
![](https://img.aspxhome.com/file/2023/0/80900_0s.png)
C#对文件名智能排序的算法
2022-05-02 16:18:28
IDEA实用好用插件推荐及使用方法教程详解(必看)
2021-07-15 19:10:44
![](https://img.aspxhome.com/file/2023/1/62041_0s.jpg)
浅谈关于Java的GC垃圾回收器的一些基本概念
2021-11-14 10:42:52
![](https://img.aspxhome.com/file/2023/7/78877_0s.jpg)