spring AOP xml声明式实现解析

加载标签解析类AopNamespaceHandler,对应解析xml文件的元素类为 ConfigBeanDefinitionParser

public class AopNamespaceHandler extends NamespaceHandlerSupport {

    /**
     * Register the {@link BeanDefinitionParser BeanDefinitionParsers} for the
     * '{@code config}', '{@code spring-configured}', '{@code aspectj-autoproxy}'
     * and '{@code scoped-proxy}' tags.
     */
    @Override
    public void init() {
        // In 2.0 XSD as well as in 2.1 XSD.
        registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser()); //解析aop xml元素
        registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
        registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());

        // Only in 2.0 XSD: moved to context namespace as of 2.1
        registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
    }
}

ConfigBeanDefinitionParser:

@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
    CompositeComponentDefinition compositeDef =
            new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
    parserContext.pushContainingComponent(compositeDef);

    configureAutoProxyCreator(parserContext, element);

    List childElts = DomUtils.getChildElements(element);
    for (Element elt: childElts) {
        String localName = parserContext.getDelegate().getLocalName(elt);
        if (POINTCUT.equals(localName)) {
            parsePointcut(elt, parserContext);//解析pointcut并注册
        }
        else if (ADVISOR.equals(localName)) {
            parseAdvisor(elt, parserContext);//解析advisor并注册
        }
        else if (ASPECT.equals(localName)) {
            parseAspect(elt, parserContext);//解析aspect并注册
        }
    }

    parserContext.popAndRegisterContainingComponent();
    return null;
}

加载aop xml的源码相对简单一点,从代码逻辑中很明显的看出封装POINTCUT,ADVISOR和ASPECT的过程

你可能感兴趣的:(spring AOP xml声明式实现解析)