Spring源码解析(5):AOP过程上

Spring的AOP是Spring的两大特性之一,在分析源码之前,先介绍AOP之中的几个术语。

Advice通知:

Advice定义在连接点做什么,为切面挣钱提供织入接口,在Spring AOP中,它主要描述Spring AOP围绕方法调用而注入的切面行为。(do what?)

Pointcut切点:

Pointcut决定Advice通知应该作用于哪个两节点,也就是说通过Pointcut来定义需要增强的方法的集合,这些集合的选取可以按照一定的规则来完成。这种情况下,Pointcut通常意味着标识方法,例如,这些需要增强的地方可以由某个正则表达式进行标识,或根据某个方法名进行匹配等。(do where?)

Advisor通知器:

完成对目标方法的切面增强设计(Advice)和关注点设计(Pointcut)以后,需要一个对象把它们结合起来,完成这个作用的就是Adivsor。(do where and what?)

Spring中实现Aop的方式有多种,我们分析以xml配置aop的方式进行源码分析。

测试代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                 http://www.springframework.org/schema/aop
                 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="targetClass" class="TargetClass" lazy-init="true"></bean>
    <bean id="testAdvice" class="TestAdvice"></bean>
    <aop:config proxy-target-class="true">
        <aop:aspect id="test" ref="testAdvice">
            <aop:pointcut id="allMethod" expression="execution(* TargetI.*(..))"></aop:pointcut>
            <aop:before method="before" pointcut-ref="allMethod"></aop:before>
            <aop:after method="after" pointcut-ref="allMethod"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>
public class TargetClass implements TargetI{
    @Override
    public void test() {
        System.out.println("test run....");
    }
}
public interface TargetI {
    public  void  test();
}
public class TestAdvice {

    public void before(){
        System.out.println("run before...");
    }

    public void after(){
        System.out.println("run after...");
    }
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MainClass
{
    public static void main(String [] args)
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        TargetI target = (TargetI)applicationContext.getBean("targetClass");
        target.test();
    }

}

在分析源码之前,我们应该先进行思考,spring对AOP的处理无非可以分为两步。一是Spring容器初始化时,读取配置文件,并loadBeanDefinitions。二是,Bean初始化时,根据BeanDefinitions进行对Bean的代理,返回代理对象。具体的载入BeanDfinition的过程和ioc的过程,就不再赘述,详细请参考之前的博客,这里只分析aop相关内容。

直接进入到DefaultBeanDefinitionDocumentReader的parseBeanDefinitions方法,这个方法,对xml的标签进行判断,默认标签和非默认标签调用不同的方法进行解析。<aop>这样的非默认标签,直接调用BeanDefinitionParserDelegate的parseCustomElement方法进行解析。DefaultBeanDefinitionDocumentReader面向单个xml文件,BeanDefinitionParserDelegate面向标签。

protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
		if (delegate.isDefaultNamespace(root)) {
			NodeList nl = root.getChildNodes();
			for (int i = 0; i < nl.getLength(); i++) {
				Node node = nl.item(i);
				if (node instanceof Element) {
					Element ele = (Element) node;
					if (delegate.isDefaultNamespace(ele)) {//默认标签 <bean>之类
						parseDefaultElement(ele, delegate);
					}
					else {//非默认标签<aop>之类
						delegate.parseCustomElement(ele);
					}
				}
			}
		}
		else {
			delegate.parseCustomElement(root);
		}
	}

查看BeanDefinitionParserDelegate的parseCustomElement(Element ele, BeanDefinition containingBd)方法。这个方法通过节点的明明空间获取对应的NamespaceHandler,NamespaceHandler用来产生相应得Parser,并处理节点。

	public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
		String namespaceUri = getNamespaceURI(ele);//节点的命名空间 URI
		NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
		if (handler == null) {
			error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
			return null;
		}
		return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
	}

NamespaceHandler的parse方法首先根据节点获取相应的BeanDefnitionParser,然后调用BeanDefnitionParser的parse方法解析xml。根绝localName选择不同的BeanDefnitionParser,aop:config对应ConfigBeanDefinitionParser。

public BeanDefinition parse(Element element, ParserContext parserContext) {
		return findParserForElement(element, parserContext).parse(element, parserContext);
	}
private BeanDefinitionParser findParserForElement(Element element, ParserContext parserContext) {
		String localName = parserContext.getDelegate().getLocalName(element);
		BeanDefinitionParser parser = this.parsers.get(localName);//根据localName不同选择parser,config-->ConfigBeanDefinitionParser
		if (parser == null) {
			parserContext.getReaderContext().fatal(
					"Cannot locate BeanDefinitionParser for element [" + localName + "]", element);
		}
		return parser;
	}

查看ConfigBeanDefinitionParser的parse(Element element, ParserContext parserContext)方法。这个方法中,调用configureAutoProxyCreator(parserContext, element);方法,将org.springframework.aop.config.internalAutoProxyCreator的BeanDefinitions注册到BeanFactory的Hashmap中,internalAutoProxyCreator实现了后置处理器接口,并在ioc中对bean进行代理。

@Override
	public BeanDefinition parse(Element element, ParserContext parserContext) {
		CompositeComponentDefinition compositeDef =
				new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
		parserContext.pushContainingComponent(compositeDef);
//向Spring容器注册了一个BeanName为org.springframework.aop.config.internalAutoProxyCreator的BeanDefinition
		configureAutoProxyCreator(parserContext, element);

		List<Element> childElts = DomUtils.getChildElements(element);
		for (Element elt: childElts) {
			String localName = parserContext.getDelegate().getLocalName(elt);
			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;
	}

接下来根绝子节点,选择不同的解析方法,我们的测试环境会进入parseAspect(elt, parserContext);方法。aop:before和aop:after都会转化为转化为advisor,其中存有advice信息和pointcut信息。此方法调用parseAdvice方法产生advisorDefinitions,并注入到BeanFactory中。

//解析切面,after、before等解析为advisor的Beandefinition,pointcut解析为pointcut的BeanDefinition
	private void parseAspect(Element aspectElement, ParserContext parserContext) {
		String aspectId = aspectElement.getAttribute(ID);//id
		String aspectName = aspectElement.getAttribute(REF);//name

		try {//AspectEntry id name,跟踪解析的逻辑位置
			this.parseState.push(new AspectEntry(aspectId, aspectName));
			List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
			List<BeanReference> beanReferences = new ArrayList<BeanReference>();
//对DECLARE_PARENTS进行处理
			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));
			}

			// We have to parse "advice" and all the advice kinds in one loop, to get the
			// ordering semantics right.
			NodeList nodeList = aspectElement.getChildNodes();
			boolean adviceFoundAlready = false;
			for (int i = 0; i < nodeList.getLength(); i++) {//解析aop:before和aop:after转化为advisor
				Node node = nodeList.item(i);
				if (isAdviceNode(node, parserContext)) {
					if (!adviceFoundAlready) {//第一次找到after火before这样的advisor,将aspectName加入beanReferences
						adviceFoundAlready = true;//更改标志位
						if (!StringUtils.hasText(aspectName)) {//判断字符串是否为空
							parserContext.getReaderContext().error(
									"<aspect> tag needs aspect bean reference via 'ref' attribute when declaring advices.",
									aspectElement, this.parseState.snapshot());
							return;
						}//添加该切面的beanReference
						beanReferences.add(new RuntimeBeanReference(aspectName));
					}
					AbstractBeanDefinition advisorDefinition = parseAdvice(
							aspectName, i, aspectElement, (Element) node, parserContext, beanDefinitions, beanReferences);
					beanDefinitions.add(advisorDefinition);
				}
			}

			AspectComponentDefinition aspectComponentDefinition = createAspectComponentDefinition(
					aspectElement, aspectId, beanDefinitions, beanReferences, parserContext);//合并advisorDefinition
			parserContext.pushContainingComponent(aspectComponentDefinition);

			List<Element> pointcuts = DomUtils.getChildElementsByTagName(aspectElement, POINTCUT);
			for (Element pointcutElement : pointcuts) {
				parsePointcut(pointcutElement, parserContext);//解析aop:pointcut
			}

			parserContext.popAndRegisterContainingComponent();
		}
		finally {
			this.parseState.pop();
		}
	}
parseAdvice方法如下,调用createAdviceDefinition注册切点的信息,最后执行parserContext.getReaderContext().registerWithGeneratedName(advisorDefinition);将BeanDefinition注册到BeanFactory中。
private AbstractBeanDefinition parseAdvice(
			String aspectName, int order, Element aspectElement, Element adviceElement, ParserContext parserContext,
			List<BeanDefinition> beanDefinitions, List<BeanReference> beanReferences) {

		try {// parserState是一个栈,里面只能存放ParseState.Entry类型的元素
			this.parseState.push(new AdviceEntry(parserContext.getDelegate().getLocalName(adviceElement)));

			// 创建方法的BeanDefinition,包括targetBeanName、methodName
			RootBeanDefinition methodDefinition = new RootBeanDefinition(MethodLocatingFactoryBean.class);
			methodDefinition.getPropertyValues().add("targetBeanName", aspectName);//targetBeanName
			methodDefinition.getPropertyValues().add("methodName", adviceElement.getAttribute("method"));//methodName
			methodDefinition.setSynthetic(true);

			// 创建aspectFactoryDef的BeanDefinition aspectBeanName
			RootBeanDefinition aspectFactoryDef =
					new RootBeanDefinition(SimpleBeanFactoryAwareAspectInstanceFactory.class);
			aspectFactoryDef.getPropertyValues().add("aspectBeanName", aspectName);
			aspectFactoryDef.setSynthetic(true);

			// register the pointcut 注册切点信息
			AbstractBeanDefinition adviceDef = createAdviceDefinition(
					adviceElement, parserContext, aspectName, order, methodDefinition, aspectFactoryDef,
					beanDefinitions, beanReferences);

			// configure the advisor
			RootBeanDefinition advisorDefinition = new RootBeanDefinition(AspectJPointcutAdvisor.class);
			advisorDefinition.setSource(parserContext.extractSource(adviceElement));
			advisorDefinition.getConstructorArgumentValues().addGenericArgumentValue(adviceDef);
			if (aspectElement.hasAttribute(ORDER_PROPERTY)) {
				advisorDefinition.getPropertyValues().add(
						ORDER_PROPERTY, aspectElement.getAttribute(ORDER_PROPERTY));
			}

			// register the final advisor 此处 把advisor的Definitions注册到beanFactory中的HashMap
			parserContext.getReaderContext().registerWithGeneratedName(advisorDefinition);

			return advisorDefinition;
		}
		finally {//出栈
			this.parseState.pop();
		}
	}

parseAspect方法解析并注册Advisor之后,再对pointcut进行处理,执行parsePointcut(pointcutElement, parserContext);获取pointcutDefinition,然后注册到BeanDefinition中去。

private AbstractBeanDefinition parsePointcut(Element pointcutElement, ParserContext parserContext) {
		String id = pointcutElement.getAttribute(ID);
		String expression = pointcutElement.getAttribute(EXPRESSION);

		AbstractBeanDefinition pointcutDefinition = null;

		try {
			this.parseState.push(new PointcutEntry(id));
			pointcutDefinition = createPointcutDefinition(expression);
			pointcutDefinition.setSource(parserContext.extractSource(pointcutElement));

			String pointcutBeanName = id;
			if (StringUtils.hasText(pointcutBeanName)) {//这里吧pointcut的pointcutDefinition注入到beanFactory的hashmap中
				parserContext.getRegistry().registerBeanDefinition(pointcutBeanName, pointcutDefinition);
			}
			else {
				pointcutBeanName = parserContext.getReaderContext().registerWithGeneratedName(pointcutDefinition);
			}

			parserContext.registerComponent(
					new PointcutComponentDefinition(pointcutBeanName, pointcutDefinition, expression));
		}
		finally {
			this.parseState.pop();
		}

		return pointcutDefinition;
	}

到这里,就完成了AOP的第一步,将AOP相关的BeanDefinitions信息解析并注册到BeanFactory,为AOP的实际过程做好了数据准备。

猜你喜欢

转载自blog.csdn.net/z896709283/article/details/81281902