JdkDynamicAopProxy 源码分析

JdkDynamicAopProxy 介绍


此类是spring aop框架基于java jdk(java.lang.Proxy)代理的实现类。
JdkDynamicAopProxy实现了AopProxy接口及InvocationHandler接口

final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {
}

invoke方法实现


    @Override
    @Nullable
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //对一个对象的方法进行调用
        MethodInvocation invocation;
        Object oldProxy = null;
        boolean setProxyContext = false;
        //封装被代理的目标类
        TargetSource targetSource = this.advised.targetSource;
        Object target = null;

        try {
           //spring aop不会代理equals方法。如果有。则直接使用代理类的equals()
            if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
                // The target does not implement the equals(Object) method itself.
                return equals(args[0]);
            }
            //spring 代理不会代理hashcode方法。如果有hashcode。则直接使用代理类的hashCode()
            else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
                // The target does not implement the hashCode() method itself.
                return hashCode();
            }
            else if (method.getDeclaringClass() == DecoratingProxy.class) {
                // There is only getDecoratedClass() declared -> dispatch to proxy config.
                return AopProxyUtils.ultimateTargetClass(this.advised);
            }
            //如果一个类的接口是Advised类,则spring 不会使用增强。而是直接通过反射方法进行调用
            else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
                    method.getDeclaringClass().isAssignableFrom(Advised.class)) {
                return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
            }

            Object retVal;

            if (this.advised.exposeProxy) {
                // Make invocation available if necessary.
                oldProxy = AopContext.setCurrentProxy(proxy);
                setProxy
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值