一、简介
  JdkDynamicAopProxy 代理类是spring 默认的JDK动态的代理类实现。它实现了Java 动态代理接口InvocationHandler接口和Spring定义的AopProxy接口。AopProxy定义了返回代理的对象。
二、阅读
public JdkDynamicAopProxy(AdvisedSupport config) throws AopConfigException {
Assert.notNull(config, "AdvisedSupport must not be null");
if (config.getAdvisors().length == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) {
throw new AopConfigException("No advisors and no TargetSource specified");
}
this.advised = config;
}
  从其构造函数来看,该类的入参是AdvisedSupport,并把入参设置到成员变量中。其中AdvisedSupport类是Aop代理的配置管理类,里面包括了代理的对象和被代理对象需要织入的通知Advice.
从该类实现了AopProxy接口,那么久可以返回代理的对象,其实现如下:
 
@Override
public Object getProxy(@Nullable ClassLoader classLoader) {
if (logger.isTraceEnabled()) {
logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource());
}
Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true);
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}
  首先从AopProxyUtils.completeProxiedInterfaces工具方法得到需要代理的接口,接着通过findDefinedEqualsAndHashCodeMethods私有方法判断这些需要的代理接口proxiedInterfaces是否重新定义了equals和hashcode方法,并把接口记录到this.equalsDefined 和 this.hashCodeDefined成员变量中。最后核心其实是通过Java的动态代理类Proxy 产生一个代理对象。所以每调用一次getProxy都会产生新的一个代理对象。
  接着我们需要看下的就是InvocationHandler接口的invoke方法是如何实现的。注入和原理如下:
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object oldProxy = null;
boolean setProxyContext = false; // 通过this.advised得到代理的目标类,
TargetSource targetSource = this.advised.targetSource;
Object target = null; try {
// 如何代理接口没有定义equals方法,则调用JdkDynamicAopProxy的重写的equals方法
if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
// The target does not implement the equals(Object) method itself.
return equals(args[0]);
}
// 如何代理接口没有定义equals方法,则调用JdkDynamicAopProxy的重写的equals方法
else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
// The target does not implement the hashCode() method itself.
return hashCode();
}
//如果该方法申明类是DecoratingProxy,则直接最终的被代理类的类Class
else if (method.getDeclaringClass() == DecoratingProxy.class) {
// There is only getDecoratedClass() declared -> dispatch to proxy config.
return AopProxyUtils.ultimateTargetClass(this.advised);
}
//如果目标对象是Advice类型,则直接使用反射进行调用
//opaque-->标记是否需要阻止通过该配置创建的代理对象转换为Advised类型,默认值为false,表示代理对象可以被转换为Advised类型 else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
method.getDeclaringClass().isAssignableFrom(Advised.class)) {
// Service invocations on ProxyConfig with the proxy config...
return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
} Object retVal;
// 看是否需要暴露代理对象,如果需要放到threadLocal上
if (this.advised.exposeProxy) {
// Make invocation available if necessary.
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
} // Get as late as possible to minimize the time we "own" the target,
// in case it comes from a pool.
target = targetSource.getTarget();
Class<?> targetClass = (target != null ? target.getClass() : null); // 得到该调用方法的拦截链
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); // Check whether we have any advice. If we don't, we can fallback on direct
// reflective invocation of the target, and avoid creating a MethodInvocation.
// 如果拦截链为空,直接返回调到该方法
if (chain.isEmpty()) {
// We can skip creating a MethodInvocation: just invoke the target directly
// Note that the final invoker must be an InvokerInterceptor so we know it does
// nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
// 否则创建 MethodInvocation 执行拦截调用
MethodInvocation invocation =
new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
// Proceed to the joinpoint through the interceptor chain.
retVal = invocation.proceed();
} // Massage return value if necessary.
Class<?> returnType = method.getReturnType();
if (retVal != null && retVal == target &&
returnType != Object.class && returnType.isInstance(proxy) &&
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
// Special case: it returned "this" and the return type of the method
// is type-compatible. Note that we can't help if the target sets
// a reference to itself in another returned object.
// 如果返回值是this,那么返回值替换为代理对象,而不是原对象。
retVal = proxy;
}
else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
throw new AopInvocationException(
"Null return value from advice does not match primitive return type for: " + method);
}
return retVal;
}
finally {
if (target != null && !targetSource.isStatic()) {
// Must have come from TargetSource.
targetSource.releaseTarget(target);
}
if (setProxyContext) {
// Restore old proxy.
AopContext.setCurrentProxy(oldProxy);
}
}
}
  从上面我们知道有几个特殊的接口,当方法的代理类是DecoratingProxy,或者是Advised都做了特殊的处理。原因在于在创建JdkDynamicAopProxy 对象是,有Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true); 这么一句语句。它返回的代理接口就可能包括DecortingProxy 和Advised。具体如下:
static Class<?>[] completeProxiedInterfaces(AdvisedSupport advised, boolean decoratingProxy) {
Class<?>[] specifiedInterfaces = advised.getProxiedInterfaces();
// 首先得到用户设置的代理接口,如果没有检查和设置代理类是否是一个接口
if (specifiedInterfaces.length == 0) {
// No user-specified interfaces: check whether target class is an interface.
Class<?> targetClass = advised.getTargetClass();
if (targetClass != null) {
if (targetClass.isInterface()) {
advised.setInterfaces(targetClass);
}
else if (Proxy.isProxyClass(targetClass)) {
advised.setInterfaces(targetClass.getInterfaces());
}
specifiedInterfaces = advised.getProxiedInterfaces();
}
}
// 如果用户代理的接口设置中没有SpringProxy接口,需要把代理类实现SpringProxy接口,该接口是个标记接口,说明是Spring代理
boolean addSpringProxy = !advised.isInterfaceProxied(SpringProxy.class);
// 如果代理类可以转化为Advised类型,并且用户没有指定Advised,则添加该接口,
//所以这里可以知道一般Spring的代理类都实现了该Advised接口
boolean addAdvised = !advised.isOpaque() && !advised.isInterfaceProxied(Advised.class);
//如果decoratingProxy==true,并且用户未添加DecoratingProxy接口,则代理需要实现该接口DecoratingProxy
boolean addDecoratingProxy = (decoratingProxy && !advised.isInterfaceProxied(DecoratingProxy.class));
int nonUserIfcCount = 0;
if (addSpringProxy) {
nonUserIfcCount++;
}
if (addAdvised) {
nonUserIfcCount++;
}
if (addDecoratingProxy) {
nonUserIfcCount++;
}
Class<?>[] proxiedInterfaces = new Class<?>[specifiedInterfaces.length + nonUserIfcCount];
System.arraycopy(specifiedInterfaces, 0, proxiedInterfaces, 0, specifiedInterfaces.length);
int index = specifiedInterfaces.length;
if (addSpringProxy) {
proxiedInterfaces[index] = SpringProxy.class;
index++;
}
if (addAdvised) {
proxiedInterfaces[index] = Advised.class;
index++;
}
if (addDecoratingProxy) {
proxiedInterfaces[index] = DecoratingProxy.class;
}
// 所以从上面分析得知,proxiedInterfaces会返回用户制定的代理接口+SpringProxy、Advised、DecoratingProxy
return proxiedInterfaces;
}
三、测试 
public interface Human {
Human getInstance();
}

  

public class ChineseHuman implements Human {

	private String desc;

	public ChineseHuman() {
} public ChineseHuman(String desc) {
this.desc = desc;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} @Override
public Human getInstance() {
System.out.println("human desc:" + desc);
return this;
}
}

  

public class JdkDynamicAopProxyTests {

	@Test
public void testJdkProxy() {
AdvisedSupport advisedSupport = new AdvisedSupport();
advisedSupport.setInterfaces(Human.class);
advisedSupport.setTargetSource(new TargetSource() {
@Override
public Class<?> getTargetClass() {
return ChineseHuman.class;
} @Override
public boolean isStatic() {
return false;
} @Override
public Object getTarget() throws Exception {
return new ChineseHuman("中国人");
} @Override
public void releaseTarget(Object target) throws Exception { }
});
JdkDynamicAopProxy jdkDynamicAopProxy = new JdkDynamicAopProxy(advisedSupport); Human human = (Human) Proxy.newProxyInstance(JdkDynamicAopProxy.class.getClassLoader(), new Class[]{Human.class}, jdkDynamicAopProxy); System.out.println(human.getInstance()); // JDK原生代理,必然为true,因为本身是生成的是代理的接口的对象
System.out.println("human instanceof Human:" + (human instanceof Human)); //false
System.out.println("human instanceof ChineseHuman:" + (human instanceof ChineseHuman));
//false
System.out.println("human instanceof Advised:" + (human instanceof Advised));
//false
System.out.println("human instanceof SpringProxy:" + (human instanceof SpringProxy));
//false
System.out.println("human instanceof DecoratingProxy:" + (human instanceof DecoratingProxy));
//true, 有个面试题是这么问的,java动态代理为啥代理是接口,不能是类,
// 其根原在于JDK的动态代理实现方式是代理对象继承了java.lang.reflect.Proxy,并且java单继承,所以无法代理类
System.out.println("human instanceof Proxy:" + (human instanceof Proxy)); System.out.println("Spring JDK代理扩展========="); human = (Human) jdkDynamicAopProxy.getProxy(); System.out.println(human.getInstance()); // 该代理对象的getInstance返回是其本身,原因也在于上面分析的红色部分原因。
System.out.println("human== human.getInstance():" + (human == human.getInstance())); // true
System.out.println("human instanceof Human:" + (human instanceof Human));
//false
System.out.println("human instanceof ChineseHuman:" + (human instanceof ChineseHuman));
//true
System.out.println("human instanceof Advised:" + (human instanceof Advised));
// true
System.out.println("human instanceof SpringProxy:" + (human instanceof SpringProxy));
// true
System.out.println("human instanceof DecoratingProxy:" + (human instanceof DecoratingProxy));
//true
System.out.println("human instanceof Proxy:" + (human instanceof Proxy));
}
}

  从上面的测试可以知道,一个对象被Spring的JDK动态代理后,其代理对象会实现其用户指定的代理接口外,还会实现Advised,SpringProxy,DecoratingProxy接口。

 
 
 
 

SPRING 阅读--JdkDynamicAopProxy的更多相关文章

  1. Spring阅读方法

    转自:http://www.cnblogs.com/xing901022/p/4178963.html 最近没什么实质性的工作,正好有点时间,就想学学别人的代码.也看过一点源码,算是有了点阅读的经验, ...

  2. Java最新趋势之Spring阅读

    (原文地址:点我) This Week in Spring: Cloud Native and the State of Java This compilation of news and tutor ...

  3. Sping学习笔记(一)----Spring源码阅读环境的搭建

    idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...

  4. spring applicationContext.xml和hibernate.cfg.xml设置

    applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  5. 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。

    基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别. 我还是喜欢基于Schema风格的Spring事务管理,但也有很多人在用基于@Tras ...

  6. Spring -- <tx:annotation-driven>注解基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)的区别。

    借鉴:http://jinnianshilongnian.iteye.com/blog/1508018 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional ...

  7. Spring <tx:annotation-driven>注解 JDK动态代理和CGLIB动态代理 区别。

    基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别. 我还是喜欢基于Schema风格的Spring事务管理,但也有很多人在用基于@Tras ...

  8. 全网最新最详细最明白教程之Spring源码搭建,没有之一,超详细

    相关帖子有很多但是都不是最新的Gradle,我在使用Gradle最新版编译的时候简直坑死我了,弄了好久.接下来给大家详细说一下这个安装过程,以及相关的软件版本号. 相关软件.依赖的版本号: Gradl ...

  9. Confluence 使用常见问题列表

    Confluence 6 管理 Atlassian 提供的 App 摘要: Confluence 用户可以使用桌面应用来编辑一个已经上传到 Confluence 的文件,然后这个文件自动保存回 Con ...

随机推荐

  1. JS代码简洁之道--函数

    函数的参数越少越好 有一个准则是:如果你的函数参数超过两个,就应该改为对象传入. 这样做是合理的,因为当函数参数超过两个时,参数顺序开始变得难以记忆,而且容易出现一种很尴尬的情况:比如我只需要传入第三 ...

  2. 为DLL文件添加强名称

    程序在编译时出现类似 "错误 1 程序集生成失败 -- 引用的程序集“XXXXXXXXXX”没有强名称" 这样的错误,是因为它不是强名称的,则需要进行以下操作: 例如:com.so ...

  3. 洛谷 P1692 【部落卫队】

    啊这道题其实暴力就行了,算是一道搜索入门题吧. 搜索变量就应该是当前到哪一位了,然后进行枚举,当前的一位加或者不加,然后知道搜完为止. 判断当前一位可不可以加的时候本来想用vector的,但是没调出来 ...

  4. C++ MFC 操作文件夹及属性(新建,删除[包含子文件[夹]],剪切,复制,重命名)

    源文件:http://pan.baidu.com/s/169HCL 运行mfc缺失的动态连接库:http://pan.baidu.com/s/17pGlT 截图: 不足之处仅供参考,哈哈.

  5. 如何查看docker run启动参数命令

    通过runlike去查看一个容器的docker run启动参数 安装pip yum install -y python-pip 安装runlike pip install runlike 查看dock ...

  6. Oracle中truncate表不更新last_ddl_time列

    Oracle中truncate表不更新last_ddl_time列 问题描述 最近发现数据库中定时job的某张表,每天都有truncate动作,由于调整了job的interval时间,想查看last_ ...

  7. PHPstorm快捷键的学习

    1.Ctrl + 空格 当输入代码时,PHPstorm 会自动出现联想选项. 但是,如果在输入时联想时错过了选择,我们要想让他再一次出现联想,通常采用的方法是在先前的输入后面再输入字符,这时联想又会出 ...

  8. 资深前端工程师带你认识网页后缀html、htm、shtml、shtm有什么区别?

    每一个网页或者说是web页都有其固定的后缀名,不同的后缀名对应着不同的文件格式和不同的规则.协议.用法,最常见的web页的后缀名是.html和.htm,但这只是web页最基本的两种文件格式,今天我们来 ...

  9. Mysql中Union和OR性能对比

    博客已搬家,更多内容查看https://liangyongrui.github.io/ Mysql中Union和OR性能对比 在leetcode上看到一篇文章,整理一下 参考:https://leet ...

  10. 属性复制神器-mapstruct

    我们之前说到项目中会用到各种object,vo,bo,dto等等.我们需要在不同的对象上复制属性. 一.BeanUtils和PropertyUtils 我们最常用的就是Common包里面的BeanUt ...