• Spring及Spring boot 第二章 AOP 源码 AopProxy系列


    Spring及Spring boot 第二章 AOP 源码 AopProxy系列

    在这里插入图片描述
    在这里插入图片描述

    CglibAopProxy

    在这里插入图片描述

    getProxy获得Proxy
    public Object getProxy(@Nullable ClassLoader classLoader) {
            if (logger.isTraceEnabled()) {
                logger.trace("Creating CGLIB proxy: " + this.advised.getTargetSource());
            }
    
            try {
                Class<?> rootClass = this.advised.getTargetClass();
                Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");
                Class<?> proxySuperClass = rootClass;
                int x;
                if (rootClass.getName().contains("$$")) {
                    proxySuperClass = rootClass.getSuperclass();
                    Class<?>[] additionalInterfaces = rootClass.getInterfaces();
                    Class[] var5 = additionalInterfaces;
                    int var6 = additionalInterfaces.length;
    
                    for(x = 0; x < var6; ++x) {
                        Class<?> additionalInterface = var5[x];
                        this.advised.addInterface(additionalInterface);
                    }
                }
    
                this.validateClassIfNecessary(proxySuperClass, classLoader);
                Enhancer enhancer = this.createEnhancer();
                if (classLoader != null) {
                    enhancer.setClassLoader(classLoader);
                    if (classLoader instanceof SmartClassLoader && ((SmartClassLoader)classLoader).isClassReloadable(proxySuperClass)) {
                        enhancer.setUseCache(false);
                    }
                }
    
                enhancer.setSuperclass(proxySuperClass);
                enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
                enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
                enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(classLoader));
                Callback[] callbacks = this.getCallbacks(rootClass);
                Class<?>[] types = new Class[callbacks.length];
    
                for(x = 0; x < types.length; ++x) {
                    types[x] = callbacks[x].getClass();
                }
    
                enhancer.setCallbackFilter(new CglibAopProxy.ProxyCallbackFilter(this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
                enhancer.setCallbackTypes(types);
                return this.createProxyClassAndInstance(enhancer, callbacks);
            } catch (IllegalArgumentException | CodeGenerationException var9) {
                throw new AopConfigException("Could not generate CGLIB subclass of " + this.advised.getTargetClass() + ": Common causes of this problem include using a final class or a non-visible class", var9);
            } catch (Throwable var10) {
                throw new AopConfigException("Unexpected AOP exception", var10);
            }
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    在这里插入图片描述

    0:进行 AOP 代理的通用拦截器【重点关注】
    1:执行目标方法的拦截器
    2:空的 Callback 对象,例如 finalize() 方法,不需要进行任何处理
    3:目标对象调度器,用于获取目标对象
    4:配置管理器的调度器,会返回一个 AdvisedSupport 对象
    5:处理 equals(Object) 方法的拦截器
    6:处理 hashCode() 方法的拦截器

    getCallbacks
    	private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
    		// Parameters used for optimization choices...
    		boolean exposeProxy = this.advised.isExposeProxy();
    		boolean isFrozen = this.advised.isFrozen();
    		boolean isStatic = this.advised.getTargetSource().isStatic();
    
    		// Choose an "aop" interceptor (used for AOP calls).
    		Callback aopInterceptor = new DynamicAdvisedInterceptor(this.advised);
    
    		// Choose a "straight to target" interceptor. (used for calls that are
    		// unadvised but can return this). May be required to expose the proxy.
    		Callback targetInterceptor;
    		if (exposeProxy) {
    			targetInterceptor = (isStatic ?
    					new StaticUnadvisedExposedInterceptor(this.advised.getTargetSource().getTarget()) :
    					new DynamicUnadvisedExposedInterceptor(this.advised.getTargetSource()));
    		}
    		else {
    			targetInterceptor = (isStatic ?
    					new StaticUnadvisedInterceptor(this.advised.getTargetSource().getTarget()) :
    					new DynamicUnadvisedInterceptor(this.advised.getTargetSource()));
    		}
    
    		// Choose a "direct to target" dispatcher (used for
    		// unadvised calls to static targets that cannot return this).
    		Callback targetDispatcher = (isStatic ?
    				new StaticDispatcher(this.advised.getTargetSource().getTarget()) : new SerializableNoOp());
    
    		Callback[] mainCallbacks = new Callback[] {
    				aopInterceptor,  // for normal advice
    				targetInterceptor,  // invoke target without considering advice, if optimized
    				new SerializableNoOp(),  // no override for methods mapped to this
    				targetDispatcher, this.advisedDispatcher,
    				new EqualsInterceptor(this.advised),
    				new HashCodeInterceptor(this.advised)
    		};
    
    		Callback[] callbacks;
    
    		// If the target is a static one and the advice chain is frozen,
    		// then we can make some optimizations by sending the AOP calls
    		// direct to the target using the fixed chain for that method.
    		if (isStatic && isFrozen) {
    			Method[] methods = rootClass.getMethods();
    			Callback[] fixedCallbacks = new Callback[methods.length];
    			this.fixedInterceptorMap = CollectionUtils.newHashMap(methods.length);
    
    			// TODO: small memory optimization here (can skip creation for methods with no advice)
    			for (int x = 0; x < methods.length; x++) {
    				Method method = methods[x];
    				List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, rootClass);
    				fixedCallbacks[x] = new FixedChainStaticTargetInterceptor(
    						chain, this.advised.getTargetSource().getTarget(), this.advised.getTargetClass());
    				this.fixedInterceptorMap.put(method, x);
    			}
    
    			// Now copy both the callbacks from mainCallbacks
    			// and fixedCallbacks into the callbacks array.
    			callbacks = new Callback[mainCallbacks.length + fixedCallbacks.length];
    			System.arraycopy(mainCallbacks, 0, callbacks, 0, mainCallbacks.length);
    			System.arraycopy(fixedCallbacks, 0, callbacks, mainCallbacks.length, fixedCallbacks.length);
    			this.fixedInterceptorOffset = mainCallbacks.length;
    		}
    		else {
    			callbacks = mainCallbacks;
    		}
    		return callbacks;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
  • 相关阅读:
    【炼金术士】BatchSize对网络训练的影响
    idea类和方法配置注释模板
    C++设计模式(工厂方法模式)
    机器学习案例(十一):水质分析与预测
    Kotlin okhttp3 HttpClient
    Matlab神经网络工具箱——一个例子搞定神经网络算法
    测试环境搭建教程(APP+WEB)
    vscode插件开发(二)插件结构
    TENSEAL: A LIBRARY FOR ENCRYPTED TENSOR OP- ERATIONS USING HOMOMORPHIC ENCRYPTION 解读
    【ASP.NET Core】在Blazor中获取 HTTP 上下文信息
  • 原文地址:https://blog.csdn.net/qq_44587855/article/details/123241315