0
点赞
收藏
分享

微信扫一扫

Spring:注解方式启用AOP

纽二 2023-10-10 阅读 37

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

举报

相关推荐

0 条评论