EnableAspectJAutoProxy注解的proxyTargetClass和exposeProxy说明

737 阅读1分钟

EnableAspectJAutoProxy注解里属性的说明

1.proxyTargetClass说明

该属性的作用比较简单,就是指定当前的代码方式使用JDK动态代码还是CgLib,具体看下图

proxyTargetClass目标对象特征代理效果
true目标对象实现了接口使用CGLIB代理机制
true目标对象没有接口(只有实现类)使用CGLIB代理机制
false目标对象实现了接口使用JDK动态代理机制(代理所有实现了的接口)
false目标对象没有接口(只有实现类)使用CGLIB代理机制

2.exposeProxy说明

该属性的作用是自调用的时候也走一遍代理类 exposeProxy = true 是可以看到代码的执行结果会走两边代理的类, 为false AopContext.currentProxy();则报错,直接使用this.t1()会执行不到代理类


@RestController
public class test {


    @GetMapping("/t1")
    public model test(String aaa) {
        model now = new model("xxx", 123);
//        System.out.println(t1());
        test test = (test) AopContext.currentProxy();
        test.t1();
        return now;
    }

    @GetMapping(path = "/t2")
    public HashMap t1() {
        HashMap<String, Integer> jsonObject = new HashMap<>();
        jsonObject.put("1", 2);
        jsonObject.put("3", 3);
        jsonObject.put("4", 4);
        return jsonObject;
    }
}

============================================>

请求方法为:GET , 接口请求的uri:/t1

接口名称为:com.smallthanks.controller.test :: test

接口入参: {}

============================================>

请求方法为:GET , 接口请求的uri:/t1

接口名称为:com.smallthanks.controller.test :: t1

接口入参: {}