Spring:注解方式启用AOP

@EnableAspectJAutoProxy

proxyTargetClass=true:强制使用cglib动态代理

exposeProxy=true在当前线程暴露代理对象,这样就可以通过AopContext.currentProxy来拿到代理对象

复制代码
package cn.edu.tju.service5;


import org.springframework.aop.framework.AopContext;
import org.springframework.aop.framework.AopProxy;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;

@Service
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class InfoService {
    public String getHelloInfo(String str){
        return "hello,"+str;
    }
    public String getHiInfo(String str){
        //System.out.println(getHelloInfo("chopin"));
        System.out.println(((InfoService) AopContext.currentProxy()).getHelloInfo("chopin"));
        return "hi,"+str;
    }
    final public String getGreeting(String str){
        return "hay,"+str;
    }
}

当一个AOP方法调用了本类中另一个AOP方法时,被调用的AOP方法将失效,就像上述代码被注释掉的那样,AOP将失效。

AopContext使用的是ThreadLocal技术。

方法是private AOP失效。

相关推荐
开开心心就好2 小时前
多份文档怎么批量打印?支持多种格式
java·服务器·开发语言·pdf·ocr·散列表·启发式算法
Pniubi2 小时前
模拟一个充值送token怎么保证幂等?一致性?原子性?
java
企业数字化笔记3 小时前
多租户配额怎么控制?用量口径、并发预留与超额处理的实现方法
java·数据库·spring boot
IT_陈寒3 小时前
React组件莫名重渲染?你可能忘了这个小细节
前端·人工智能·后端
巴勒个啦3 小时前
Prompt Engineering 实战:5 个让 AI 生成生产级业务代码的工程模式
java·前端
用户9888321871743 小时前
给 .NET 网络调用设定“预算”:用 CancellationTokenSource.CancelAfter 处理超时取消
后端
清风儿3293 小时前
一块不会说话的电表,把我们折腾了半个月
后端
倚楼听风语3 小时前
Kotlin 协程与 suspend 原理详解
java·算法·kotlin
预知同行3 小时前
深入解析 MoE 混合专家模型:从 Router 到 All-to-All 的架构全景与生产实践
后端·架构
upala3 小时前
【仓颉语言入门 · 第二篇 第 8 课】
后端