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

相关推荐
正在努力Coding40 分钟前
SpringAI - 工具调用
java·spring·ai
Loo国昌1 小时前
【LangChain1.0】第五阶段:RAG高级篇(高级检索与优化)
人工智能·后端·语言模型·架构
我尽力学1 小时前
面试 总结
java·spring boot·面试
爬台阶的蚂蚁1 小时前
Spring AI Alibaba基础概念
java·spring·ai
计算机学姐2 小时前
基于SpringBoot的演唱会抢票系统
java·spring boot·后端·spring·tomcat·intellij-idea·推荐算法
huohuopro2 小时前
Mybatis的七种传参方式
java·开发语言·mybatis
Lee_SmallNorth2 小时前
变态需求之【角色不同访问数据库的用户不同】
java·开发语言·数据库
扶苏-su2 小时前
Java网络编程:InetAddress 详解
java·开发语言·网络
李慕婉学姐2 小时前
Springboot连锁火锅管理及预测系统sh5s1gn1(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
Anastasiozzzz2 小时前
leetcode力扣hot100困难题--4.俩个正序数列的中位数
java·算法·leetcode·面试·职场和发展