public class AopNamespaceHandler extends NamespaceHandlerSupport { public AopNamespaceHandler() { } public void init() { this.registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser()); this.registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser()); this.registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator()); this.registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser()); } }
public BeanDefinition parse(Element element, ParserContext parserContext) { CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element)); parserContext.pushContainingComponent(compositeDef); this.configureAutoProxyCreator(parserContext, element); List childElts = DomUtils.getChildElements(element); Iterator var5 = childElts.iterator(); while(var5.hasNext()) { Element elt = (Element)var5.next(); String localName = parserContext.getDelegate().getLocalName(elt); if("pointcut".equals(localName)) { this.parsePointcut(elt, parserContext); } else if("advisor".equals(localName)) { this.parseAdvisor(elt, parserContext); } else if("aspect".equals(localName)) { this.parseAspect(elt, parserContext); } } parserContext.popAndRegisterContainingComponent(); return null; }
public static BeanDefinition registerAspectJAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, Object source) { return registerOrEscalateApcAsRequired(AspectJAwareAdvisorAutoProxyCreator.class, registry, source); }
public BeanDefinition parse(Element element, ParserContext parserContext) { AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element); this.extendBeanDefinition(element, parserContext); return null; } public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, Object source) { return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source); }
public static Object getProxy(TargetSource targetSource) { if(targetSource.getTargetClass() == null) { throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class"); } else { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(targetSource); proxyFactory.setProxyTargetClass(true); return proxyFactory.getProxy(); } }
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { if(!config.isOptimize() && !config.isProxyTargetClass() && !this.hasNoUserSuppliedProxyInterfaces(config)) { return new JdkDynamicAopProxy(config); } else { Class targetClass = config.getTargetClass(); if(targetClass == null) { throw new AopConfigException("TargetSource cannot determine target class: Either an interface or a target is required for proxy creation."); } else { return (AopProxy)(targetClass.isInterface()?new JdkDynamicAopProxy(config):new ObjenesisCglibAopProxy(config)); } } }
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); this.findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object oldProxy = null; boolean setProxyContext = false; TargetSource targetSource = this.advised.targetSource; Class targetClass = null; Object target = null; Object retVal; try { //eqauls()方法,具目标对象未实现此方法 if(!this.equalsDefined && AopUtils.isEqualsMethod(method)) { Boolean retVal2 = Boolean.valueOf(this.equals(args[0])); return retVal2; } //hashCode()方法,具目标对象未实现此方法 if(!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) { Integer retVal1 = Integer.valueOf(this.hashCode()); return retVal1; } //Advised接口或其父接口中定义的方法,直接反射调用,不利用通知 if(this.advised.opaque || !method.getDeclaringClass().isInterface() || !method.getDeclaringClass().isAssignableFrom(Advised.class)) { if(this.advised.exposeProxy) { oldProxy = AopContext.setCurrentProxy(proxy); setProxyContext = true; } //取得目标对象的类 target = targetSource.getTarget(); if(target != null) { targetClass = target.getClass(); } //获得可以利用到此方法上的Interceptor列表 List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); if(chain.isEmpty()) { //如果没有可以利用到此方法的通知(Interceptor),此直接反射调用 method.invoke(target, args) retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args); } else { //创建MethodInvocation ,封装 Interceptor调用链 ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); //调用proceed()方法, 顺次履行 Interceptor调用链 retVal = invocation.proceed(); } Class returnType = method.getReturnType(); if(retVal != null && retVal == target && returnType.isInstance(proxy) && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) { 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); } Object var13 = retVal; return var13; } retVal = AopUtils.invokeJoinpointUsingReflection(this.advised, method, args); } finally { if(target != null && !targetSource.isStatic()) { targetSource.releaseTarget(target); } if(setProxyContext) { AopContext.setCurrentProxy(oldProxy); } } return retVal; }
public ListgetInterceptorsAndDynamicInterceptionAdvice(Method method, Class targetClass) { //这里使用了cache,利用cache获得已有了的intercepter AdvisedSupport.MethodCacheKey cacheKey = new AdvisedSupport.MethodCacheKey(method); List cached = (List)this.methodCache.get(cacheKey); if(cached == null) { //这里是利用DefaultAdvisorChainFactory,获得intercepter链 cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(this, method, targetClass); this.methodCache.put(cacheKey, cached); } return cached; }
在DefaultAdvisorChainFactory类(生成连接器的工厂)中:
public ListgetInterceptorsAndDynamicInterceptionAdvice(Advised config, Method method, Class targetClass) { ArrayList interceptorList = new ArrayList(config.getAdvisors().length); Class actualClass = targetClass != null?targetClass:method.getDeclaringClass(); boolean hasIntroductions = hasMatchingIntroductions(config, actualClass); AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance(); Advisor[] var8 = config.getAdvisors(); int var9 = var8.length; for(int var10 = 0; var10 < var9; ++var10) { Advisor advisor = var8[var10]; MethodInterceptor[] interceptors1; if(advisor instanceof PointcutAdvisor) { PointcutAdvisor interceptors = (PointcutAdvisor)advisor; if(config.isPreFiltered() || interceptors.getPointcut().getClassFilter().matches(actualClass)) { interceptors1 = registry.getInterceptors(advisor); MethodMatcher mm = interceptors.getPointcut().getMethodMatcher(); if(MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) { if(mm.isRuntime()) { MethodInterceptor[] var15 = interceptors1; int var16 = interceptors1.length; for(int var17 = 0; var17 < var16; ++var17) { MethodInterceptor interceptor = var15[var17]; interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm)); } } else { interceptorList.addAll(Arrays.asList(interceptors1)); } } } } else if(advisor instanceof IntroductionAdvisor) { IntroductionAdvisor var19 = (IntroductionAdvisor)advisor; if(config.isPreFiltered() || var19.getClassFilter().matches(actualClass)) { interceptors1 = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(interceptors1)); } } else { MethodInterceptor[] var20 = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(var20)); } } return interceptorList; }
AdvisorAdapterRegistry registry。GlobalAdvisorAdapterRegistry应用单例模式:其实例是DefaultAdvisorAdapterRegistry类对象:
public abstract class GlobalAdvisorAdapterRegistry { private static AdvisorAdapterRegistry instance = new DefaultAdvisorAdapterRegistry(); public GlobalAdvisorAdapterRegistry() { } public static AdvisorAdapterRegistry getInstance() { return instance; } static void reset() { instance = new DefaultAdvisorAdapterRegistry(); } }
下一篇 awk入门学习笔记