spring AOP XML解析

<aop:config> 標籤的解析: <bean id="loggingAspect" class="com.zhuguang.jack.aop.aspect.AspectXml1"></bean>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.zhuguang.jack.aop.aspect.AspectXml1.*(..))" />
        <aop:aspect  ref="loggingAspect" order="1" >
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="exception"/>
        </aop:aspect>
    </aop:config>
aop:config對應的類是AspectJAwareAdvisorAutoProxyCreator。加到bean工廠。 aop:aspect 不用解析成類。 aop:before 對應的類是AspectJPointcutAdvisor,裏面有AspectJMethodBeforeAdvice,裏面有MethodLocatingFactoryBean和SimpleBeanFactoryAwareAspectInstanceFactory和pointcut。加到bean工廠。 aop:after ,aop:after-returning,aop:after-throwing與aop:before相似。 類的屬性值就是根據標籤上面寫的解析出來的。
解析自定義標籤就是建立bean的定義,<aop:before,<aop:after,<aop:after-returning,<aop:after-throwing。分別對應AspectJMethodBeforeAdvice.class;AspectJAfterAdvice.class;AspectJAfterReturningAdvice.class;AspectJAfterThrowingAdvice.class;AspectJAroundAdvice.class;  AspectJAwareAdvisorAutoProxyCreator是支持AOP功能的入口類,一定實現BeanPostProcessor接口,bean實例化的時候就回去調用這個類,就會找這個類有沒有切面,有切面就生成代理,
首先找到AopNamespaceHandler,而後找到registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser()); public class AopNamespaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());
        registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
        registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());
        registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
    }
}
ConfigBeanDefinitionParser類裏面有一個解析標籤的方法parse(), public BeanDefinition parse(Element element, ParserContext parserContext) { //element = [aop:config: null],parserContext = org.springframework.beans.factory.xml.ParserContext@2fd953a6,
        CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
        parserContext.pushContainingComponent(compositeDef);

        configureAutoProxyCreator(parserContext, element);  //ParserContext裏面有一個屬性XmlReaderContext readerContext,BeanDefinitionParserDelegate delegate,解析這個元素就是爲了建立BeanDefinition對象,建立AspectJAwareAdvisorAutoProxyCreator的bean定義而且加載到bean工廠。這個類實現了BeanPostProcessor接口,實例化任何一個bean的時候,會調用spring容器中全部BeanPostProcessor接口的類的方法,AspectJAwareAdvisorAutoProxyCreator就會去檢測要實例化的這個bean有沒有切面,有切面,就幫這個bean生成代理, 
        List<Element> childElts = DomUtils.getChildElements(element);   //[[aop:pointcut], [aop:advisor]]子標籤,
        for (Element elt: childElts) {
            String localName = parserContext.getDelegate().getLocalName(elt);   //aop下面就3個標籤,
            if (POINTCUT.equals(localName)) {
                parsePointcut(elt, parserContext);
            }
            else if (ADVISOR.equals(localName)) {
                parseAdvisor(elt, parserContext);
            }
            else if (ASPECT.equals(localName)) {
                parseAspect(elt, parserContext);
            }
        }

        parserContext.popAndRegisterContainingComponent();
        return null;
    }
private void configureAutoProxyCreator(ParserContext parserContext, Element element) {
        AopNamespaceUtils.registerAspectJAutoProxyCreatorIfNecessary(parserContext, element);
    }

public static void registerAspectJAutoProxyCreatorIfNecessary(ParserContext parserContext, Element sourceElement) {
        BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAutoProxyCreatorIfNecessary(
                parserContext.getRegistry(), parserContext.extractSource(sourceElement));  //建立beanDefinition 對象,返回AspectJAwareAdvisorAutoProxyCreator的bean的定義,
 useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
        registerComponentIfNecessary(beanDefinition, parserContext);
    }

public static BeanDefinition registerAspectJAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, Object source) {
        return registerOrEscalateApcAsRequired(AspectJAwareAdvisorAutoProxyCreator.class, registry, source);   //註冊器是DefaultListableBeanFactory,null,
    }
private static BeanDefinition registerOrEscalateApcAsRequired(Class<?> cls, BeanDefinitionRegistry registry, Object source) {  //AspectJAwareAdvisorAutoProxyCreator,bean工廠,null,
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {   //看bean工廠中有沒有AspectJAwareAdvisorAutoProxyCreator的實例,名字是internalAutoProxyCreator,有就直接返回,沒有就在bean工廠建立AspectJAwareAdvisorAutoProxyCreator的實例,名字叫作internalAutoProxyCreator。
            BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
            if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
                int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
                int requiredPriority = findPriorityForClass(cls);
                if (currentPriority < requiredPriority) {
                    apcDefinition.setBeanClassName(cls.getName());
                }
            }
            return null;
        }
        RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);  //建立AspectJAwareAdvisorAutoProxyCreator的實例
        beanDefinition.setSource(source);
        beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
        beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);   //加到bean工廠,名字叫作internalAutoProxyCreator,
        return beanDefinition;
    }
private static void useClassProxyingIfNecessary(BeanDefinitionRegistry registry, Element sourceElement) {
        if (sourceElement != null) {
            boolean proxyTargetClass = Boolean.valueOf(sourceElement.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE));  //獲取[aop:config]標籤的proxy-target-class="false"屬性,true就是cglib的動態代理,faslse就是jdk的動態代理,沒有寫就是jdk的動態代理,
            if (proxyTargetClass) {//true
                AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
            }
            boolean exposeProxy = Boolean.valueOf(sourceElement.getAttribute(EXPOSE_PROXY_ATTRIBUTE));   //sourceElement = [aop:config]標籤,
            if (exposeProxy) {
                AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
            }
        }
    }
public static void forceAutoProxyCreatorToUseClassProxying(BeanDefinitionRegistry registry) {
        if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
            BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
            definition.getPropertyValues().add("proxyTargetClass", Boolean.TRUE);   //往internalAutoProxyCreator的bean定義裏面加了一個屬性,
        }
    }
private static void registerComponentIfNecessary(BeanDefinition beanDefinition, ParserContext parserContext) {  //AspectJAwareAdvisorAutoProxyCreator的bean定義,org.springframework.beans.factory.xml.ParserContext@2fd953a6,
        if (beanDefinition != null) {
            BeanComponentDefinition componentDefinition = new BeanComponentDefinition(beanDefinition, AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);  //把AspectJAwareAdvisorAutoProxyCreator的bean定義包裝成BeanComponentDefinition,包裝的名稱叫作internalAutoProxyCreator,
            parserContext.registerComponent(componentDefinition);
        }
    }
BeanComponentDefinition extends BeanDefinitionHolder。
BeanDefinition最終會包裝成BeanDefinitionHolder,BeanDefinitionHolder有BeanDefinition beanDefinition是bean的定義和String beanName 是bean的名稱。
private void parseAspect(Element aspectElement, ParserContext parserContext) {  //解析<aop:aspect標籤,
        String aspectId = aspectElement.getAttribute(ID);   //id屬性,
        String aspectName = aspectElement.getAttribute(REF);   //ref是對應的切面類,

        try {
            this.parseState.push(new AspectEntry(aspectId, aspectName));
            List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
            List<BeanReference> beanReferences = new ArrayList<BeanReference>();

            List<Element> declareParents = DomUtils.getChildElementsByTagName(aspectElement, DECLARE_PARENTS);
            for (int i = METHOD_INDEX; i < declareParents.size(); i++) {
                Element declareParentsElement = declareParents.get(i);
                beanDefinitions.add(parseDeclareParents(declareParentsElement, parserContext));
            }

            NodeList nodeList = aspectElement.getChildNodes();//aspect的子元素,<aop:before,<aop:after,<aop:after-returning,<aop:after-throwing。
            boolean adviceFoundAlready = false;
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);   //<aop:before,<aop:after,<aop:after-returning,<aop:after-throwing。
                if (isAdviceNode(node, parserContext)) {
                    if (!adviceFoundAlready) {
                        adviceFoundAlready = true;
                        beanReferences.add(new RuntimeBeanReference(aspectName));
                    }

                    AbstractBeanDefinition advisorDefinition = parseAdvice(aspectName, i, aspectElement, (Element) node, parserContext, beanDefinitions, beanReferences);  //解析通知節點<aop:before,<aop:after,<aop:after-returning,<aop:after-throwing標籤。目的也是爲了建立BeanDefinition。 //返回AspectJPointcutAdvisor的bean定義,constructorArgumentValues屬性裏面的gebericArgumentValues裏面有AspectJMethodBeforeAdvice(如今解析的是aop:before標籤)(還有after,around,throw,exception幾種)的bean定義裏面的constructorArgumentValues屬性裏面有MethodLocatingFactoryBean的bean定義和SimpleBeanFactoryAwareAspectInstanceFactory的bean定義和pointcut。

                    beanDefinitions.add(advisorDefinition);//4種前置後置環繞等通知加進去,一個是before標籤的bean定義,一個是after的bean定義,一個是afterReturn的bean定義,一個是afterThrow的bean定義。
                }
            }
//解析aop:aspect標籤,就是把裏面的aop:before,aop:after,aop:afterthrow,aop:afterRuturnning,這幾種標籤解析成bean的定義,跟須要加入切面的類尚未創建直接的關係。

            AspectComponentDefinition aspectComponentDefinition = createAspectComponentDefinition(
                    aspectElement, aspectId, beanDefinitions, beanReferences, parserContext);
            parserContext.pushContainingComponent(aspectComponentDefinition);

            List<Element> pointcuts = DomUtils.getChildElementsByTagName(aspectElement, POINTCUT);  //這個<aop:aspect  標籤裏面的pointcut標籤,
            for (Element pointcutElement : pointcuts) {
                parsePointcut(pointcutElement, parserContext);
            }

            parserContext.popAndRegisterContainingComponent();
        }
        finally {
            this.parseState.pop();
        }
    }
private AbstractBeanDefinition parseAdvice(String aspectName, int order, Element aspectElement, Element adviceElement, ParserContext parserContext,List<BeanDefinition> beanDefinitions, List<BeanReference> beanReferences) {//aspectName = aop:aspect標籤的ref屬性的值,aspectElement = [aop:aspect: null]標籤,adviceElement = <aop:before,<aop:after,<aop:after-returning,<aop:after-throwing。

        try {
            this.parseState.push(new AdviceEntry(parserContext.getDelegate().getLocalName(adviceElement)));

            RootBeanDefinition methodDefinition = new RootBeanDefinition(MethodLocatingFactoryBean.class);   //建立這個的bean定義,
            methodDefinition.getPropertyValues().add("targetBeanName", aspectName);//loggingAspect,切面類名稱。
            methodDefinition.getPropertyValues().add("methodName", adviceElement.getAttribute("method"));  //執行的方法,
            methodDefinition.setSynthetic(true);

            RootBeanDefinition aspectFactoryDef = new RootBeanDefinition(SimpleBeanFactoryAwareAspectInstanceFactory.class); //建立這個的bean定義,
            aspectFactoryDef.getPropertyValues().add("aspectBeanName", aspectName);//loggingAspect,切面類名稱。
            aspectFactoryDef.setSynthetic(true);

            AbstractBeanDefinition adviceDef = createAdviceDefinition(adviceElement, parserContext, aspectName, order, methodDefinition, aspectFactoryDef,beanDefinitions, beanReferences);  //返回AspectJMethodBeforeAdvice的bean定義constructorArgumentValues屬性裏面有MethodLocatingFactoryBean的bean定義和SimpleBeanFactoryAwareAspectInstanceFactory的bean定義和pointcut,前置通知的beanDefinition(<aop:before>標籤的beanDefinition)。 // configure the advisor
            RootBeanDefinition advisorDefinition = new RootBeanDefinition(AspectJPointcutAdvisor.class);  //切面內
            advisorDefinition.setSource(parserContext.extractSource(adviceElement));   //[aop:before: null],
            advisorDefinition.getConstructorArgumentValues().addGenericArgumentValue(adviceDef);  //切面裏面必需要有advice,經過構造函數設置值。
            if (aspectElement.hasAttribute(ORDER_PROPERTY)) {
                advisorDefinition.getPropertyValues().add(ORDER_PROPERTY, aspectElement.getAttribute(ORDER_PROPERTY));
            }

            // register the final advisor
            parserContext.getReaderContext().registerWithGeneratedName(advisorDefinition);

            return advisorDefinition;//返回這個切面。AspectJPointcutAdvisor的bean定義,constructorArgumentValues屬性裏面的gebericArgumentValues屬性裏面有AspectJMethodBeforeAdvice的bean定義裏面的constructorArgumentValues屬性裏面有MethodLocatingFactoryBean的bean定義和SimpleBeanFactoryAwareAspectInstanceFactory的bean定義和pointcut。
        }
        finally {
            this.parseState.pop();
        }
    }
private AbstractBeanDefinition createAdviceDefinition(
            Element adviceElement, ParserContext parserContext, String aspectName, int order,
            RootBeanDefinition methodDef, RootBeanDefinition aspectFactoryDef,
            List<BeanDefinition> beanDefinitions, List<BeanReference> beanReferences) {

        RootBeanDefinition adviceDefinition = new RootBeanDefinition(getAdviceClass(adviceElement, parserContext));   //AspectJMethodBeforeAdvice的bean定義,before的類是AspectJMethodBeforeAdvice,after的類是AspectJAfterAdvice,return的類是AspectJAfterReturningAdvice,afterThrowing的類是AspectJAfterThrowingAdvice,around的類是AspectJAroundAdvice。
        adviceDefinition.setSource(parserContext.extractSource(adviceElement));

        adviceDefinition.getPropertyValues().add(ASPECT_NAME_PROPERTY, aspectName);  //切面類名稱
        adviceDefinition.getPropertyValues().add(DECLARATION_ORDER_PROPERTY, order);  

        if (adviceElement.hasAttribute(RETURNING)) {//添加屬性
            adviceDefinition.getPropertyValues().add(
                    RETURNING_PROPERTY, adviceElement.getAttribute(RETURNING));
        }
        if (adviceElement.hasAttribute(THROWING)) {//添加屬性
            adviceDefinition.getPropertyValues().add(
                    THROWING_PROPERTY, adviceElement.getAttribute(THROWING));
        }
        if (adviceElement.hasAttribute(ARG_NAMES)) {//添加屬性
            adviceDefinition.getPropertyValues().add(
                    ARG_NAMES_PROPERTY, adviceElement.getAttribute(ARG_NAMES));
        }

        ConstructorArgumentValues cav = adviceDefinition.getConstructorArgumentValues();    //構造函數參數。
        cav.addIndexedArgumentValue(METHOD_INDEX, methodDef);

        Object pointcut = parsePointcutProperty(adviceElement, parserContext);  //解析pointcut,
        if (pointcut instanceof BeanDefinition) {//有pointcut屬性,返回bean定義,
            cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcut);
            beanDefinitions.add((BeanDefinition) pointcut);
        }
        else if (pointcut instanceof String) {//有pointcut-ref屬性,返回字符串,
            RuntimeBeanReference pointcutRef = new RuntimeBeanReference((String) pointcut);
            cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcutRef);
            beanReferences.add(pointcutRef);
        }

        cav.addIndexedArgumentValue(ASPECT_INSTANCE_FACTORY_INDEX, aspectFactoryDef);

        return adviceDefinition;  //返回AspectJMethodBeforeAdvice的bean定義裏面有MethodLocatingFactoryBean的bean定義和SimpleBeanFactoryAwareAspectInstanceFactory的bean定義和pointcut。
    }
private Class<?> getAdviceClass(Element adviceElement, ParserContext parserContext) {
        String elementName = parserContext.getDelegate().getLocalName(adviceElement);
        if (BEFORE.equals(elementName)) {//前置通知
            return AspectJMethodBeforeAdvice.class;
        }
        else if (AFTER.equals(elementName)) {//後置通知
            return AspectJAfterAdvice.class;
        }
        else if (AFTER_RETURNING_ELEMENT.equals(elementName)) {//返回通知
            return AspectJAfterReturningAdvice.class;
        }
        else if (AFTER_THROWING_ELEMENT.equals(elementName)) {//異常通知
            return AspectJAfterThrowingAdvice.class;
        }
        else if (AROUND.equals(elementName)) {//環繞通知
            return AspectJAroundAdvice.class;
        }
    }
private Object parsePointcutProperty(Element element, ParserContext parserContext) {  //element = aop:before標籤,
        if (element.hasAttribute(POINTCUT) && element.hasAttribute(POINTCUT_REF)) {   //沒有pointcut和pointcut-ref屬性,直接報錯。
            parserContext.getReaderContext().error("Cannot define both 'pointcut' and );
            return null;
        }
        else if (element.hasAttribute(POINTCUT)) {
            String expression = element.getAttribute(POINTCUT);
            AbstractBeanDefinition pointcutDefinition = createPointcutDefinition(expression);   //建立AspectJExpressionPointcut的bean定義,
            pointcutDefinition.setSource(parserContext.extractSource(element));
            return pointcutDefinition;
        }
        else if (element.hasAttribute(POINTCUT_REF)) {
            String pointcutRef = element.getAttribute(POINTCUT_REF);
            return pointcutRef;//返回字符串,
        }
    }
private AspectComponentDefinition createAspectComponentDefinition(
            Element aspectElement, String aspectId, List<BeanDefinition> beanDefs,
            List<BeanReference> beanRefs, ParserContext parserContext) {

        BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[beanDefs.size()]);   //4個前置後置環繞等通知的數組,
        BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[beanRefs.size()]);
        Object source = parserContext.extractSource(aspectElement);
        return new AspectComponentDefinition(aspectId, beanDefArray, beanRefArray, source);
    }
自定義標籤都會有一個類與之對應,這個類是spring本身的類,而後加到spring的容器中,跟非自定義標籤<bean>的解析相似。
<aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.zhuguang.jack.aop.aspect.AspectXml1.*(..))" />
aop:pointcut 解析成AspectJExpressionPointcut類,aop:config不用解析。 類的屬性值就是根據標籤上面寫的解析出來的。
parsePointcut(elt, parserContext); //[aop:pointcut: null],解析pointcut標籤, 
private AbstractBeanDefinition parsePointcut(Element pointcutElement, ParserContext parserContext) {
        String id = pointcutElement.getAttribute(ID);   //pointcut
        String expression = pointcutElement.getAttribute(EXPRESSION);   //execution(* com.zhuguang.jack.aop.aspect.AspectXml1.*(..)),
        AbstractBeanDefinition pointcutDefinition = null;
        try {
            this.parseState.push(new PointcutEntry(id));
            pointcutDefinition = createPointcutDefinition(expression); //AspectJExpressionPointcut的bean定義,裏面有point的表達式,
            pointcutDefinition.setSource(parserContext.extractSource(pointcutElement));

            String pointcutBeanName = id;  //pointcut
            if (StringUtils.hasText(pointcutBeanName)) {
                parserContext.getRegistry().registerBeanDefinition(pointcutBeanName, pointcutDefinition);   //加到spring的容器,名字是pointcut,bean定義是AspectJExpressionPointcut的bean定義,
            }

            parserContext.registerComponent(new PointcutComponentDefinition(pointcutBeanName, pointcutDefinition, expression));  //pointcut,AspectJExpressionPointcut的beanDefinition,execution(* com.zhuguang.jack.aop.aspect.AspectXml1.*(..)),加到棧中,
        }
        finally {
            this.parseState.pop();
        }

        return pointcutDefinition;    //返回AspectJExpressionPointcut的bean定義,
    }
protected AbstractBeanDefinition createPointcutDefinition(String expression) {
        RootBeanDefinition beanDefinition = new RootBeanDefinition(AspectJExpressionPointcut.class);
        beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
        beanDefinition.setSynthetic(true);
        beanDefinition.getPropertyValues().add(EXPRESSION, expression);
        return beanDefinition;
    }
<aop:config proxy-target-class="false">
        <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice"/>
    </aop:config>
aop:advisor 對應的類是DefaultBeanFactoryPointcutAdvisor,aop:config不用解析,而後解析pointcut-ref屬性和 advice-ref屬性。最後加到bean容器中去。 類的屬性值就是根據標籤上面寫的解析出來的。
private void parseAdvisor(Element advisorElement, ParserContext parserContext) {    //解析Advisor標籤
        AbstractBeanDefinition advisorDef = createAdvisorBeanDefinition(advisorElement, parserContext);   //建立bean定義對象,返回DefaultBeanFactoryPointcutAdvisor的beanDefinition,他的PropertyValues屬性 = new RuntimeBeanNameReference(userTxAdvice),userTxAdvice是須要IOC依賴注入的,
        String id = advisorElement.getAttribute(ID);

        try {
            this.parseState.push(new AdvisorEntry(id));
            String advisorBeanName = id;
            if (StringUtils.hasText(advisorBeanName)) {
                parserContext.getRegistry().registerBeanDefinition(advisorBeanName, advisorDef);   //advisorDef 加到bean工廠中去,
            }
            else {
                advisorBeanName = parserContext.getReaderContext().registerWithGeneratedName(advisorDef);   //自動生成的名字org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,advisorDef 加到bean工廠中去,  }

            Object pointcut = parsePointcutProperty(advisorElement, parserContext);   //解析pointcut-ref屬性,
            if (pointcut instanceof BeanDefinition) {//根據pointcut返回beanDefinition,根據pointcut-ref返回String,
                advisorDef.getPropertyValues().add(POINTCUT, pointcut);  //  添加pointcut屬性,
                parserContext.registerComponent(new AdvisorComponentDefinition(advisorBeanName, advisorDef, (BeanDefinition) pointcut));//註冊到棧中,
            }
            else if (pointcut instanceof String) {
                advisorDef.getPropertyValues().add(POINTCUT, new RuntimeBeanReference((String) pointcut));//將pointcut-ref轉換成RuntimeBeanReference,而後添加pointcut屬性,
                parserContext.registerComponent(new AdvisorComponentDefinition(advisorBeanName, advisorDef));//註冊到棧中,
            }
        }
    }
private AbstractBeanDefinition createAdvisorBeanDefinition(Element advisorElement, ParserContext parserContext) {  //[aop:advisor: null]標籤,
        RootBeanDefinition advisorDefinition = new RootBeanDefinition(DefaultBeanFactoryPointcutAdvisor.class);
        advisorDefinition.setSource(parserContext.extractSource(advisorElement));

        String adviceRef = advisorElement.getAttribute(ADVICE_REF);   //advice-ref屬性 = userTxAdvice(ref就是引用,就須要依賴注入)
        if (!StringUtils.hasText(adviceRef)) {
            parserContext.getReaderContext().error(pshot());
        }
        else {
            advisorDefinition.getPropertyValues().add(ADVICE_BEAN_NAME, new RuntimeBeanNameReference(adviceRef));   //userTxAdvice封裝成RuntimeBeanNameReference,ref是引用要依賴注入,當發現依賴注入的類型是RuntimeBeanNameReference的時候,那麼就會去經過bean工廠去拿這個對象。
        }

        if (advisorElement.hasAttribute(ORDER_PROPERTY)) {  //排序屬性
            advisorDefinition.getPropertyValues().add(
                    ORDER_PROPERTY, advisorElement.getAttribute(ORDER_PROPERTY));
        }

        return advisorDefinition;//返回DefaultBeanFactoryPointcutAdvisor的beanDefinition,他的PropertyValues屬性 = new RuntimeBeanNameReference(userTxAdvice),userTxAdvice是須要IOC依賴注入的,
    }
private Object parsePointcutProperty(Element element, ParserContext parserContext) {  //element = [aop:advisor: null]標籤,
        if (element.hasAttribute(POINTCUT) && element.hasAttribute(POINTCUT_REF)) {  //是否有pointcut屬性
            parserContext.getReaderContext().error(ot());
            return null;
        }
        else if (element.hasAttribute(POINTCUT)) {   //pointcut屬性
            // Create a pointcut for the anonymous pc and register it.
            String expression = element.getAttribute(POINTCUT);
            AbstractBeanDefinition pointcutDefinition = createPointcutDefinition(expression);  //AspectJExpressionPointcut的bean定義,
            pointcutDefinition.setSource(parserContext.extractSource(element));
            return pointcutDefinition;
        }
        else if (element.hasAttribute(POINTCUT_REF)) {  //pointcut-ref屬性
            String pointcutRef = element.getAttribute(POINTCUT_REF);  //string:引用的名字,
            if (!StringUtils.hasText(pointcutRef)) {
                parserContext.getReaderContext().error(ement, this.parseState.snapshot());
                return null;
            }
            return pointcutRef; //string:引用的名字,
        }
    }
相關文章
相關標籤/搜索