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失效。

相关推荐
万少6 小时前
小龙虾(openclaw),轻松玩转自动发帖
前端·人工智能·后端
Jagger_8 小时前
AI 洪水淹到脖子了:剩下的是什么?我们该往哪儿跑?
后端
Victor3569 小时前
MongoDB(28)什么是地理空间索引?
后端
Victor3569 小时前
MongoDB(29)如何创建索引?
后端
皮皮林55110 小时前
面试官:什么是 fail-fast?什么是 fail-safe?
后端
陈随易10 小时前
前端大咖mizchi不满Rust、TypeScript却爱上MoonBit
前端·后端·程序员
雨中飘荡的记忆12 小时前
Multi-Agent + Skills + Spring AI 构建自主决策智能体
后端·spring
我叫黑大帅12 小时前
Go 语言并发编程的 “工具箱”
后端·面试·go