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

相关推荐
全干engineer1 天前
idea拉取github代码 -TLS connect error 异常问题
java·github·intellij-idea
10岁的博客1 天前
二维差分算法高效解靶场问题
java·服务器·算法
百***93501 天前
Tomcat报404问题解决方案大全(包括tomcat可以正常运行但是报404)
java·tomcat
百***81271 天前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
qq_281317471 天前
kubernetes(k8s)-pod生命周期
java·容器·kubernetes
IT界的奇葩1 天前
代码规范 spring-javaformat使用
java·spring·代码规范
披着羊皮不是狼1 天前
多用户跨学科交流系统(4)参数校验+分页搜索全流程的实现
java·spring boot
小池先生1 天前
Gradle vs Maven 详细对比
java·maven
q***23921 天前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
q***78781 天前
Spring Boot的项目结构
java·spring boot·后端