本文共 6469 字,大约阅读时间需要 21 分钟。
1 2 3 4 5 6 7 | public interface AopProxy { Object getProxy(); Object getProxy(ClassLoader classLoader); } |
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 | public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { logger.debug( "Creating CGLIB proxy: target source is " + this .advised.getTargetSource()); } try { //此时的rootClass为BServiceImpl Class<?> rootClass = this .advised.getTargetClass(); Assert.state(rootClass != null , "Target class must be available for creating a CGLIB proxy" ); Class<?> proxySuperClass = rootClass; //这里判断rootClass是否是Cglib代理所产生的类(内部判断rootClass的className是否包含$$),对于本工程肯定不符合,跳过 if (ClassUtils.isCglibProxyClass(rootClass)) { proxySuperClass = rootClass.getSuperclass(); Class<?>[] additionalInterfaces = rootClass.getInterfaces(); for (Class<?> additionalInterface : additionalInterfaces) { this .advised.addInterface(additionalInterface); } } //验证proxySuperClass中的是否有final方法(仅仅是打印出来警告信息,不做任何处理) // Validate the class, writing log messages as necessary. validateClassIfNecessary(proxySuperClass); // 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 UndeclaredThrowableStrategy(UndeclaredThrowableException. class )); 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); } //略 } |
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 | @Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { Object oldProxy = null ; boolean setProxyContext = false ; Class<?> targetClass = null ; Object target = null ; try { if ( this .advised.exposeProxy) { // Make invocation available if necessary. oldProxy = AopContext.setCurrentProxy(proxy); setProxyContext = true ; } // May be null. Get as late as possible to minimize the time we // "own" the target, in case it comes from a pool... target = getTarget(); if (target != null ) { targetClass = target.getClass(); } List<Object> chain = this .advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); Object retVal; // Check whether we only have one InvokerInterceptor: that is, // no real advice, but just reflective invocation of the target. if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) { // 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. retVal = methodProxy.invoke(target, args); } else { // We need to create a method invocation... retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed(); } retVal = processReturnType(proxy, target, method, retVal); return retVal; } finally { if (target != null ) { releaseTarget(target); } if (setProxyContext) { // Restore old proxy. AopContext.setCurrentProxy(oldProxy); } } } |
1 2 3 | protected Object invokeJoinpoint() throws Throwable { return AopUtils.invokeJoinpointUsingReflection( this .target, this .method, this .arguments); } |
1 2 3 4 5 6 7 8 | protected Object invokeJoinpoint() throws Throwable { if ( this .publicMethod) { return this .methodProxy.invoke( this .target, this .arguments); } else { return super .invokeJoinpoint(); } } |
1 2 3 4 5 6 | public CglibMethodInvocation(Object proxy, Object target, Method method, Object[] arguments, Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers, MethodProxy methodProxy) { super (proxy, target, method, arguments, targetClass, interceptorsAndDynamicMethodMatchers); this .methodProxy = methodProxy; this .publicMethod = Modifier.isPublic(method.getModifiers()); } |
1 2 3 4 5 6 7 8 | /** * Gives a marginal performance improvement versus using reflection to * invoke the target when invoking public methods. */ @Override protected Object invokeJoinpoint() throws Throwable { //略 } |
转载地址:http://bymja.baihongyu.com/