SpringAop 原理及源码 随笔记录
来源:互联网 发布:我知你好未再百度 编辑:程序博客网 时间:2024/06/05 16:38
SpringAop 原理及源码 随笔记录
本Markdown编辑器使用[StackEdit][6]修改而来,用它写博客,将会带来全新的体验哦:
- 原理:代理模式
- 源码实现方式1:JdkDynamicAopProxy
- 源码实现2:CglibAopProxy
原理:代理模式
源码实现方式1:JdkDynamicAopProxy
jdk自带代理模式
@Override public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource()); } Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true); findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); }... prompt'''
源码实现2:CglibAopProxy
想对于常用代理模式区别在于 将实现接口的方式改为将代理类继承至被代理类。然后进行代理模式其他操作
@Override public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { logger.debug("Creating CGLIB proxy: target source is " + 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; if (ClassUtils.isCglibProxyClass(rootClass)) { proxySuperClass = rootClass.getSuperclass(); Class<?>[] additionalInterfaces = rootClass.getInterfaces(); for (Class<?> additionalInterface : additionalInterfaces) { this.advised.addInterface(additionalInterface); } } // Validate the class, writing log messages as necessary. validateClassIfNecessary(proxySuperClass, classLoader); // Configure CGLIB Enhancer... Enhancer enhancer = 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 ClassLoaderAwareUndeclaredThrowableStrategy(classLoader)); Callback[] callbacks = getCallbacks(rootClass); Class<?>[] types = new Class<?>[callbacks.length]; for (int x = 0; x < types.length; x++) { types[x] = callbacks[x].getClass(); } // fixedInterceptorMap only populated at this point, after getCallbacks call above enhancer.setCallbackFilter(new ProxyCallbackFilter( this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset)); enhancer.setCallbackTypes(types); // Generate the proxy class and create a proxy instance. return createProxyClassAndInstance(enhancer, callbacks); } catch (CodeGenerationException ex) { throw new AopConfigException("Could not generate CGLIB subclass of class [" + this.advised.getTargetClass() + "]: " + "Common causes of this problem include using a final class or a non-visible class", ex); } catch (IllegalArgumentException ex) { throw new AopConfigException("Could not generate CGLIB subclass of class [" + this.advised.getTargetClass() + "]: " + "Common causes of this problem include using a final class or a non-visible class", ex); } catch (Exception ex) { // TargetSource.getTarget() failed throw new AopConfigException("Unexpected AOP exception", ex); } }
0 0
- SpringAop 原理及源码 随笔记录
- SpringAOP基本概念及实现原理(一)
- SpringAOP基本概念及实现原理(二)
- SpringAOP基本概念及实现原理(三)
- springAOP原理
- SpringAOP原理
- SpringAOP原理介绍
- SpringAOP实现原理
- springAOP实现原理
- springAOP的原理
- SpringAOP的实现原理
- springAOP背后的原理
- SpringAop实现原理
- SpringAOP源码序列图
- springAOP源码解析(一)
- SpringAOP源码解析
- SpringAOP源码解析
- 基于SSM利用SpringAOP切面及自定义注解 记录每次操作记录(操作日志 同理)
- iOS--RunTime运行时
- CentOS7 安装mysql
- 传智播客推出少儿教育品牌-酷丁鱼
- xcode更新一直失败的解决办法
- python return break continue
- SpringAop 原理及源码 随笔记录
- 编译错误:用户定义类型未定义(IUnknown)
- 比较时间字符串的问题
- How to fix: “UnicodeDecodeError: 'ascii' codec can't decode byte”
- junit+spring Test.jar 单元测试
- poj1458
- java 集合之List
- hdu 2336 无题II 【二分+二分匹配】
- 链表删除指定的值