zoukankan      html  css  js  c++  java
  • 【Spring源码分析】AOP源码解析(上篇)

    前言

    前面写了六篇文章详细地分析了Spring Bean加载流程,这部分完了之后就要进入一个比较困难的部分了,就是AOP的实现原理分析。为了探究AOP实现原理,首先定义几个类,一个Dao接口:

    1 public interface Dao {
    2     
    3     public void select();
    4 
    5     public void insert();
    6     
    7 }

    Dao接口的实现类DaoImpl:

     1 public class DaoImpl implements Dao {
     2 
     3     @Override
     4     public void select() {
     5         System.out.println("Enter DaoImpl.select()");
     6     }
     7 
     8     @Override
     9     public void insert() {
    10         System.out.println("Enter DaoImpl.insert()");
    11     }
    12     
    13 }

    定义一个TimeHandler,用于方法调用前后打印时间,在AOP中,这扮演的是横切关注点的角色:

    1 public class TimeHandler {
    2 
    3     public void printTime() {
    4         System.out.println("CurrentTime:" + System.currentTimeMillis());
    5     }
    6     
    7 }

    定义一个XML文件aop.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xmlns:tx="http://www.springframework.org/schema/tx"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans
     7         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     8         http://www.springframework.org/schema/aop
     9         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    10 
    11     <bean id="daoImpl" class="org.xrq.action.aop.DaoImpl" />
    12     <bean id="timeHandler" class="org.xrq.action.aop.TimeHandler" />
    13 
    14     <aop:config proxy-target-class="true">
    15         <aop:aspect id="time" ref="timeHandler">
    16             <aop:pointcut id="addAllMethod" expression="execution(* org.xrq.action.aop.Dao.*(..))" />
    17             <aop:before method="printTime" pointcut-ref="addAllMethod" />
    18             <aop:after method="printTime" pointcut-ref="addAllMethod" />
    19         </aop:aspect>
    20     </aop:config>
    21     
    22 </beans>

    写一段测试代码TestAop.java:

     1 public class TestAop {
     2 
     3     @Test
     4     public void testAop() {
     5         ApplicationContext ac = new ClassPathXmlApplicationContext("spring/aop.xml");
     6         
     7         Dao dao = (Dao)ac.getBean("daoImpl");
     8         dao.select();
     9     }
    10     
    11 }

    代码运行结果就不看了,有了以上的内容,我们就可以根据这些跟一下代码,看看Spring到底是如何实现AOP的。

    AOP实现原理----找到Spring处理AOP的源头

    有很多朋友不愿意去看AOP源码的一个很大原因是因为找不到AOP源码实现的入口在哪里,这个确实是。不过我们可以看一下上面的测试代码,就普通Bean也好、AOP也好,最终都是通过getBean方法获取到Bean并调用方法的,getBean之后的对象已经前后都打印了TimeHandler类printTime()方法里面的内容,可以想见它们已经是被Spring容器处理过了。

    既然如此,那无非就两个地方处理:

    1. 加载Bean定义的时候应该有过特殊的处理
    2. getBean的时候应该有过特殊的处理

    因此,本文围绕【1.加载Bean定义的时候应该有过特殊的处理】展开,先找一下到底是哪里Spring对AOP做了特殊的处理。代码直接定位到DefaultBeanDefinitionDocumentReader的parseBeanDefinitions方法:

     1 protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
     2     if (delegate.isDefaultNamespace(root)) {
     3         NodeList nl = root.getChildNodes();
     4         for (int i = 0; i < nl.getLength(); i++) {
     5             Node node = nl.item(i);
     6             if (node instanceof Element) {
     7                 Element ele = (Element) node;
     8                 if (delegate.isDefaultNamespace(ele)) {
     9                     parseDefaultElement(ele, delegate);
    10                 }
    11                 else {
    12                     delegate.parseCustomElement(ele);
    13                 }
    14             }
    15         }
    16     }
    17     else {
    18         delegate.parseCustomElement(root);
    19     }
    20 }

    正常来说,遇到<bean id="daoImpl"...>、<bean id="timeHandler"...>这两个标签的时候,都会执行第9行的代码,因为<bean>标签是默认的Namespace。但是在遇到后面的<aop:config>标签的时候就不一样了,<aop:config>并不是默认的Namespace,因此会执行第12行的代码,看一下:

    1 public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
    2     String namespaceUri = getNamespaceURI(ele);
    3     NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
    4     if (handler == null) {
    5         error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
    6         return null;
    7     }
    8     return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
    9 }

    因为之前把整个XML解析为了org.w3c.dom.Document,org.w3c.dom.Document以树的形式表示整个XML,具体到每一个节点就是一个Node。

    首先第2行从<aop:config>这个Node(参数Element是Node接口的子接口)中拿到Namespace="http://www.springframework.org/schema/aop",第3行的代码根据这个Namespace获取对应的NamespaceHandler即Namespace处理器,具体到aop这个Namespace的NamespaceHandler是org.springframework.aop.config.AopNamespaceHandler类,也就是第3行代码获取到的结果。具体到AopNamespaceHandler里面,有几个Parser,是用于具体标签转换的,分别为:

    • config-->ConfigBeanDefinitionParser
    • aspectj-autoproxy-->AspectJAutoProxyBeanDefinitionParser
    • scoped-proxy-->ScopedProxyBeanDefinitionDecorator
    • spring-configured-->SpringConfiguredBeanDefinitionParser

    接着,就是第8行的代码,利用AopNamespaceHandler的parse方法,解析<aop:config>下的内容了。

    AOP Bean定义加载----根据织入方式将<aop:before>、<aop:after>转换成名为adviceDef的RootBeanDefinition

    上面经过分析,已经找到了Spring是通过AopNamespaceHandler处理的AOP,那么接着进入AopNamespaceHandler的parse方法源代码:

    1 public BeanDefinition parse(Element element, ParserContext parserContext) {
    2     return findParserForElement(element, parserContext).parse(element, parserContext);
    3 }

    首先获取具体的Parser,因为当前节点是<aop:config>,上一部分最后有列,config是通过ConfigBeanDefinitionParser来处理的,因此findParserForElement(element, parserContext)这一部分代码获取到的是ConfigBeanDefinitionParser,接着看ConfigBeanDefinitionParser的parse方法:

     1 public BeanDefinition parse(Element element, ParserContext parserContext) {
     2     CompositeComponentDefinition compositeDef =
     3             new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
     4     parserContext.pushContainingComponent(compositeDef);
     5 
     6     configureAutoProxyCreator(parserContext, element);
     7 
     8     List<Element> childElts = DomUtils.getChildElements(element);
     9     for (Element elt: childElts) {
    10         String localName = parserContext.getDelegate().getLocalName(elt);
    11         if (POINTCUT.equals(localName)) {
    12             parsePointcut(elt, parserContext);
    13         }
    14         else if (ADVISOR.equals(localName)) {
    15             parseAdvisor(elt, parserContext);
    16         }
    17         else if (ASPECT.equals(localName)) {
    18             parseAspect(elt, parserContext);
    19         }
    20     }
    21 
    22     parserContext.popAndRegisterContainingComponent();
    23     return null;
    24 }

    重点先提一下第6行的代码,该行代码的具体实现不跟了但它非常重要,configureAutoProxyCreator方法的作用我用几句话说一下:

    • 向Spring容器注册了一个BeanName为org.springframework.aop.config.internalAutoProxyCreator的Bean定义,可以自定义也可以使用Spring提供的(根据优先级来)
    • Spring默认提供的是org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator,这个类是AOP的核心类,留在下篇讲解
    • 在这个方法里面也会根据配置proxy-target-class和expose-proxy,设置是否使用CGLIB进行代理以及是否暴露最终的代理。

    <aop:config>下的节点为<aop:aspect>,想见必然是执行第18行的代码parseAspect,跟进去:

     1 private void parseAspect(Element aspectElement, ParserContext parserContext) {
     2     String aspectId = aspectElement.getAttribute(ID);
     3     String aspectName = aspectElement.getAttribute(REF);
     4 
     5     try {
     6         this.parseState.push(new AspectEntry(aspectId, aspectName));
     7         List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
     8         List<BeanReference> beanReferences = new ArrayList<BeanReference>();
     9 
    10         List<Element> declareParents = DomUtils.getChildElementsByTagName(aspectElement, DECLARE_PARENTS);
    11         for (int i = METHOD_INDEX; i < declareParents.size(); i++) {
    12             Element declareParentsElement = declareParents.get(i);
    13             beanDefinitions.add(parseDeclareParents(declareParentsElement, parserContext));
    14         }
    15 
    16         // We have to parse "advice" and all the advice kinds in one loop, to get the
    17         // ordering semantics right.
    18         NodeList nodeList = aspectElement.getChildNodes();
    19         boolean adviceFoundAlready = false;
    20         for (int i = 0; i < nodeList.getLength(); i++) {
    21             Node node = nodeList.item(i);
    22             if (isAdviceNode(node, parserContext)) {
    23                 if (!adviceFoundAlready) {
    24                     adviceFoundAlready = true;
    25                     if (!StringUtils.hasText(aspectName)) {
    26                         parserContext.getReaderContext().error(
    27                                 "<aspect> tag needs aspect bean reference via 'ref' attribute when declaring advices.",
    28                                 aspectElement, this.parseState.snapshot());
    29                         return;
    30                     }
    31                     beanReferences.add(new RuntimeBeanReference(aspectName));
    32                 }
    33                 AbstractBeanDefinition advisorDefinition = parseAdvice(
    34                         aspectName, i, aspectElement, (Element) node, parserContext, beanDefinitions, beanReferences);
    35                 beanDefinitions.add(advisorDefinition);
    36             }
    37         }
    38 
    39         AspectComponentDefinition aspectComponentDefinition = createAspectComponentDefinition(
    40                 aspectElement, aspectId, beanDefinitions, beanReferences, parserContext);
    41         parserContext.pushContainingComponent(aspectComponentDefinition);
    42 
    43         List<Element> pointcuts = DomUtils.getChildElementsByTagName(aspectElement, POINTCUT);
    44         for (Element pointcutElement : pointcuts) {
    45             parsePointcut(pointcutElement, parserContext);
    46         }
    47 
    48         parserContext.popAndRegisterContainingComponent();
    49     }
    50     finally {
    51         this.parseState.pop();
    52     }
    53 }

    从第20行~第37行的循环开始关注这个方法。这个for循环有一个关键的判断就是第22行的ifAdviceNode判断,看下ifAdviceNode方法做了什么:

     1 private boolean isAdviceNode(Node aNode, ParserContext parserContext) {
     2     if (!(aNode instanceof Element)) {
     3         return false;
     4     }
     5     else {
     6         String name = parserContext.getDelegate().getLocalName(aNode);
     7         return (BEFORE.equals(name) || AFTER.equals(name) || AFTER_RETURNING_ELEMENT.equals(name) ||
     8                 AFTER_THROWING_ELEMENT.equals(name) || AROUND.equals(name));
     9     }
    10 }

    即这个for循环只用来处理<aop:aspect>标签下的<aop:before>、<aop:after>、<aop:after-returning>、<aop:after-throwing method="">、<aop:around method="">这五个标签的。

    接着,如果是上述五种标签之一,那么进入第33行~第34行的parseAdvice方法:

     1 private AbstractBeanDefinition parseAdvice(
     2         String aspectName, int order, Element aspectElement, Element adviceElement, ParserContext parserContext,
     3         List<BeanDefinition> beanDefinitions, List<BeanReference> beanReferences) {
     4
     5     try {
     6         this.parseState.push(new AdviceEntry(parserContext.getDelegate().getLocalName(adviceElement)));
     7
     8         // create the method factory bean
     9         RootBeanDefinition methodDefinition = new RootBeanDefinition(MethodLocatingFactoryBean.class);
    10         methodDefinition.getPropertyValues().add("targetBeanName", aspectName);
    11         methodDefinition.getPropertyValues().add("methodName", adviceElement.getAttribute("method"));
    12         methodDefinition.setSynthetic(true);
    13
    14         // create instance factory definition
    15         RootBeanDefinition aspectFactoryDef =
    16                 new RootBeanDefinition(SimpleBeanFactoryAwareAspectInstanceFactory.class);
    17         aspectFactoryDef.getPropertyValues().add("aspectBeanName", aspectName);
    18         aspectFactoryDef.setSynthetic(true);
    19 
    20         // register the pointcut
    21         AbstractBeanDefinition adviceDef = createAdviceDefinition(
    22                 adviceElement, parserContext, aspectName, order, methodDefinition, aspectFactoryDef,
    23                 beanDefinitions, beanReferences);
    24 
    25         // configure the advisor
    26         RootBeanDefinition advisorDefinition = new RootBeanDefinition(AspectJPointcutAdvisor.class);
    27         advisorDefinition.setSource(parserContext.extractSource(adviceElement));
    28         advisorDefinition.getConstructorArgumentValues().addGenericArgumentValue(adviceDef);
    29         if (aspectElement.hasAttribute(ORDER_PROPERTY)) {
    30             advisorDefinition.getPropertyValues().add(
    31                     ORDER_PROPERTY, aspectElement.getAttribute(ORDER_PROPERTY));
    32         }
    33 
    34         // register the final advisor
    35         parserContext.getReaderContext().registerWithGeneratedName(advisorDefinition);
    36 
    37         return advisorDefinition;
    38     }
    39     finally {
    40         this.parseState.pop();
    41     }
    42 }

    方法主要做了三件事:

    1. 根据织入方式(before、after这些)创建RootBeanDefinition,名为adviceDef即advice定义
    2. 将上一步创建的RootBeanDefinition写入一个新的RootBeanDefinition,构造一个新的对象,名为advisorDefinition,即advisor定义
    3. 将advisorDefinition注册到DefaultListableBeanFactory中

    下面来看做的第一件事createAdviceDefinition方法定义:

     1 private AbstractBeanDefinition createAdviceDefinition(
     2         Element adviceElement, ParserContext parserContext, String aspectName, int order,
     3         RootBeanDefinition methodDef, RootBeanDefinition aspectFactoryDef,
     4         List<BeanDefinition> beanDefinitions, List<BeanReference> beanReferences) {
     5 
     6     RootBeanDefinition adviceDefinition = new RootBeanDefinition(getAdviceClass(adviceElement, parserContext));
     7     adviceDefinition.setSource(parserContext.extractSource(adviceElement));
     8         adviceDefinition.getPropertyValues().add(ASPECT_NAME_PROPERTY, aspectName);
     9     adviceDefinition.getPropertyValues().add(DECLARATION_ORDER_PROPERTY, order);
    10 
    11     if (adviceElement.hasAttribute(RETURNING)) {
    12         adviceDefinition.getPropertyValues().add(
    13                 RETURNING_PROPERTY, adviceElement.getAttribute(RETURNING));
    14     }
    15     if (adviceElement.hasAttribute(THROWING)) {
    16         adviceDefinition.getPropertyValues().add(
    17                 THROWING_PROPERTY, adviceElement.getAttribute(THROWING));
    18     }
    19     if (adviceElement.hasAttribute(ARG_NAMES)) {
    20         adviceDefinition.getPropertyValues().add(
    21                 ARG_NAMES_PROPERTY, adviceElement.getAttribute(ARG_NAMES));
    22     }
    23 
    24     ConstructorArgumentValues cav = adviceDefinition.getConstructorArgumentValues();
    25     cav.addIndexedArgumentValue(METHOD_INDEX, methodDef);
    26 
    27     Object pointcut = parsePointcutProperty(adviceElement, parserContext);
    28     if (pointcut instanceof BeanDefinition) {
    29         cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcut);
    30         beanDefinitions.add((BeanDefinition) pointcut);
    31     }
    32     else if (pointcut instanceof String) {
    33         RuntimeBeanReference pointcutRef = new RuntimeBeanReference((String) pointcut);
    34         cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcutRef);
    35         beanReferences.add(pointcutRef);
    36     }
    37 
    38     cav.addIndexedArgumentValue(ASPECT_INSTANCE_FACTORY_INDEX, aspectFactoryDef);
    39 
    40     return adviceDefinition;
    41 }

    首先可以看到,创建的AbstractBeanDefinition实例是RootBeanDefinition,这和普通Bean创建的实例为GenericBeanDefinition不同。然后进入第6行的getAdviceClass方法看一下:

     1 private Class getAdviceClass(Element adviceElement, ParserContext parserContext) {
     2     String elementName = parserContext.getDelegate().getLocalName(adviceElement);
     3     if (BEFORE.equals(elementName)) {
     4         return AspectJMethodBeforeAdvice.class;
     5     }
     6     else if (AFTER.equals(elementName)) {
     7         return AspectJAfterAdvice.class;
     8     }
     9     else if (AFTER_RETURNING_ELEMENT.equals(elementName)) {
    10         return AspectJAfterReturningAdvice.class;
    11     }
    12     else if (AFTER_THROWING_ELEMENT.equals(elementName)) {
    13         return AspectJAfterThrowingAdvice.class;
    14     }
    15     else if (AROUND.equals(elementName)) {
    16         return AspectJAroundAdvice.class;
    17     }
    18     else {
    19         throw new IllegalArgumentException("Unknown advice kind [" + elementName + "].");
    20     }
    21 }

    既然创建Bean定义,必然该Bean定义中要对应一个具体的Class,不同的切入方式对应不同的Class:

    • before对应AspectJMethodBeforeAdvice
    • After对应AspectJAfterAdvice
    • after-returning对应AspectJAfterReturningAdvice
    • after-throwing对应AspectJAfterThrowingAdvice
    • around对应AspectJAroundAdvice

    createAdviceDefinition方法剩余逻辑没什么,就是判断一下标签里面的属性并设置一下相应的值而已,至此<aop:before>、<aop:after>两个标签对应的AbstractBeanDefinition就创建出来了。

    AOP Bean定义加载----将名为adviceDef的RootBeanDefinition转换成名为advisorDefinition的RootBeanDefinition

    下面我们看一下第二步的操作,将名为adviceDef的RootBeanD转换成名为advisorDefinition的RootBeanDefinition,跟一下上面一部分ConfigBeanDefinitionParser类parseAdvice方法的第26行~32行的代码:

    1 RootBeanDefinition advisorDefinition = new RootBeanDefinition(AspectJPointcutAdvisor.class);
    2 advisorDefinition.setSource(parserContext.extractSource(adviceElement));
    3 advisorDefinition.getConstructorArgumentValues().addGenericArgumentValue(adviceDef);
    4 if (aspectElement.hasAttribute(ORDER_PROPERTY)) {
    5     advisorDefinition.getPropertyValues().add(
    6             ORDER_PROPERTY, aspectElement.getAttribute(ORDER_PROPERTY));
    7 }

    这里相当于将上一步生成的RootBeanDefinition包装了一下,new一个新的RootBeanDefinition出来,Class类型是org.springframework.aop.aspectj.AspectJPointcutAdvisor。

    第4行~第7行的代码是用于判断<aop:aspect>标签中有没有"order"属性的,有就设置一下,"order"属性是用来控制切入方法优先级的。

    AOP Bean定义加载----将BeanDefinition注册到DefaultListableBeanFactory中

    最后一步就是将BeanDefinition注册到DefaultListableBeanFactory中了,代码就是前面ConfigBeanDefinitionParser的parseAdvice方法的最后一部分了:

    1 ...
    2 // register the final advisor
    3 parserContext.getReaderContext().registerWithGeneratedName(advisorDefinition);
    4 ...

    跟一下registerWithGeneratedName方法的实现:

    1 public String registerWithGeneratedName(BeanDefinition beanDefinition) {
    2     String generatedName = generateBeanName(beanDefinition);
    3     getRegistry().registerBeanDefinition(generatedName, beanDefinition);
    4     return generatedName;
    5 }

    第2行获取注册的名字BeanName,和<bean>的注册差不多,使用的是Class全路径+"#"+全局计数器的方式,其中的Class全路径为org.springframework.aop.aspectj.AspectJPointcutAdvisor,依次类推,每一个BeanName应当为org.springframework.aop.aspectj.AspectJPointcutAdvisor#0、org.springframework.aop.aspectj.AspectJPointcutAdvisor#1、org.springframework.aop.aspectj.AspectJPointcutAdvisor#2这样下去。

    第3行向DefaultListableBeanFactory中注册,BeanName已经有了,剩下的就是Bean定义,Bean定义的解析流程之前已经看过了,就不说了。

    AOP Bean定义加载----AopNamespaceHandler处理<aop:pointcut>流程

    回到ConfigBeanDefinitionParser的parseAspect方法:

     1 private void parseAspect(Element aspectElement, ParserContext parserContext) {
     2     
     3         ...   
     4 
     5         AspectComponentDefinition aspectComponentDefinition = createAspectComponentDefinition(
     6                 aspectElement, aspectId, beanDefinitions, beanReferences, parserContext);
     7         parserContext.pushContainingComponent(aspectComponentDefinition);
     8 
     9         List<Element> pointcuts = DomUtils.getChildElementsByTagName(aspectElement, POINTCUT);
    10         for (Element pointcutElement : pointcuts) {
    11             parsePointcut(pointcutElement, parserContext);
    12         }
    13 
    14         parserContext.popAndRegisterContainingComponent();
    15     }
    16     finally {
    17         this.parseState.pop();
    18     }
    19 }

    省略号部分表示是解析的是<aop:before>、<aop:after>这种标签,上部分已经说过了,就不说了,下面看一下解析<aop:pointcut>部分的源码。

    第5行~第7行的代码构建了一个Aspect标签组件定义,并将Apsect标签组件定义推到ParseContext即解析工具上下文中,这部分代码不是关键。

    第9行的代码拿到所有<aop:aspect>下的pointcut标签,进行遍历,由parsePointcut方法进行处理:

     1 private AbstractBeanDefinition parsePointcut(Element pointcutElement, ParserContext parserContext) {
     2     String id = pointcutElement.getAttribute(ID);
     3     String expression = pointcutElement.getAttribute(EXPRESSION);
     4 
     5     AbstractBeanDefinition pointcutDefinition = null;
     6         
     7     try {
     8         this.parseState.push(new PointcutEntry(id));
     9         pointcutDefinition = createPointcutDefinition(expression);
    10         pointcutDefinition.setSource(parserContext.extractSource(pointcutElement));
    11 
    12         String pointcutBeanName = id;
    13         if (StringUtils.hasText(pointcutBeanName)) {
    14             parserContext.getRegistry().registerBeanDefinition(pointcutBeanName, pointcutDefinition);
    15         }
    16         else {
    17             pointcutBeanName = parserContext.getReaderContext().registerWithGeneratedName(pointcutDefinition);
    18         }
    19 
    20         parserContext.registerComponent(
    21                 new PointcutComponentDefinition(pointcutBeanName, pointcutDefinition, expression));
    22     }
    23     finally {
    24         this.parseState.pop();
    25     }
    26 
    27     return pointcutDefinition;
    28 }

    第2行~第3行的代码获取<aop:pointcut>标签下的"id"属性与"expression"属性。

    第8行的代码推送一个PointcutEntry,表示当前Spring上下文正在解析Pointcut标签。

    第9行的代码创建Pointcut的Bean定义,之后再看,先把其他方法都看一下。

    第10行的代码不管它,最终从NullSourceExtractor的extractSource方法获取Source,就是个null。

    第12行~第18行的代码用于注册获取到的Bean定义,默认pointcutBeanName为<aop:pointcut>标签中定义的id属性:

    • 如果<aop:pointcut>标签中配置了id属性就执行的是第13行~第15行的代码,pointcutBeanName=id
    • 如果<aop:pointcut>标签中没有配置id属性就执行的是第16行~第18行的代码,和Bean不配置id属性一样的规则,pointcutBeanName=org.springframework.aop.aspectj.AspectJExpressionPointcut#序号(从0开始累加)

    第20行~第21行的代码向解析工具上下文中注册一个Pointcut组件定义

    第23行~第25行的代码,finally块在<aop:pointcut>标签解析完毕后,让之前推送至栈顶的PointcutEntry出栈,表示此次<aop:pointcut>标签解析完毕。

    最后回头来一下第9行代码createPointcutDefinition的实现,比较简单:

    1 protected AbstractBeanDefinition createPointcutDefinition(String expression) {
    2     RootBeanDefinition beanDefinition = new RootBeanDefinition(AspectJExpressionPointcut.class);
    3     beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    4     beanDefinition.setSynthetic(true);
    5     beanDefinition.getPropertyValues().add(EXPRESSION, expression);
    6     return beanDefinition;
    7 }

    关键就是注意一下两点:

    1. <aop:pointcut>标签对应解析出来的BeanDefinition是RootBeanDefinition,且RootBenaDefinitoin中的Class是org.springframework.aop.aspectj.AspectJExpressionPointcut
    2. <aop:pointcut>标签对应的Bean是prototype即原型的

    这样一个流程下来,就解析了<aop:pointcut>标签中的内容并将之转换为RootBeanDefintion存储在Spring容器中。

  • 相关阅读:
    [Violet]蒲公英
    CF535-Div3
    逛公园
    exgcd
    线段树套线段树
    Luogu P2730 魔板 Magic Squares
    fhqtreap
    AtCoder Beginner Contest 115
    关于这个博客
    智障错误盘点
  • 原文地址:https://www.cnblogs.com/xrq730/p/6753160.html
Copyright © 2011-2022 走看看