1 spring 动态代理的一些说明
1 spring 5.0 默认使用的 代理方式 依旧是 jdk 动态代理(之前的版本也是)
2 springboot 2.X 开始 默认使用代理方式 cglib(springboot 2.X 默认使用 spring 5,给人的感觉是 spring 5.0 开始默认使用的 cglib 代理)
3 可以使用 spring.aop.proxy-target-class=false 切换成 jdk 动态代理(spring boot 默认是true),spring xml 方式使用 <aop:aspectj-autoproxy proxy-target-class="true"/>
4 spring aop默认使用的代理方式是 jdk 代理 的意思是,有父类接口的就使用jdk 动态代理,没有的还是只能使用 cglib
2 jdk 动态代理 Proxy 例子(通过 Proxy.newProxyInstance 得到实例)
3 cglib Enhancer 动态代理 例子(通过 Enhancer.create 得到代理类)
值得注意的是,可以指定多个 增强实现,但是多个增强实现的时候必须指定 拦截器 ,返回使用的增强逻辑下标
4 java静态代理实现
5 动态代理和静态代理的区别:
1 动态代理和静态代理的区别在于 代理类是硬编码产生的还是通过 java 反射产生的
2 动态代理的优势有两个 第一是代理类动态产生,第二是增强的逻辑动态替换
3 静态代理 如果使用 InvocationHandler 接口也可以实现 增强逻辑的动态替换,但是不能实现代理类的动态生成
4 代理类的动态生指的是 不需要硬编码就能实现一个包含一个或者多个接口的代理实现类,这是动态代理最大的优势
5 当前调用方法名字 可以通过当前线程 调用记录得到 Thread.currentThread().getStackTrace()[1]
6 单个类指定代理模式可以通过设置 BeanDifination的 proxyTargetClass 属性来设置 definition.getPropertyValues().add("proxyTargetClass", Boolean.TRUE);
6 动态的获取当前 执行方法的方法名