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·开发语言·数据库
cch89189 分钟前
汇编与Go:底层到高层的编程差异
java·汇编·golang
chushiyunen12 分钟前
python中的@Property和@Setter
java·开发语言·python
禾小西16 分钟前
Java中使用正则表达式核心解析
java·python·正则表达式
2401_8955213418 分钟前
SpringBoot Maven快速上手
spring boot·后端·maven
yoyo_zzm18 分钟前
JAVA (Springboot) i18n国际化语言配置
java·spring boot·python
APIshop25 分钟前
Java获取京东商品详情接口(item_get)实战指南
java·linux·数据库
Mr.Entropy28 分钟前
springboot2.x集成Flyway
java
disgare33 分钟前
关于 spring 工程中添加 traceID 实践
java·后端·spring
李白的粉33 分钟前
基于springboot+vue的旅游民宿管理系统
java·spring boot·vue·毕业设计·课程设计·源代码·旅游民宿管理系统