Spring Framework Source (vi): Spring AOP parsing tab

Source: Internet
Author: User
Tags aop

First look at the Spring Framework Configuration example:

<aop:config>
 <aop:aspect id= "MYAOP" ref= "Log" > <aop:pointcut id= "mycut" expression= "
 Execution (* cn.itcast.service. *.*(..))" />
 <aop:before pointcut-ref= "mycut" method= doaccesscheck "/>"
 <aop:after-returning pointcut-ref= "Mycut" method= "Doreturncheck"/> <aop:after-throwing pointcut-ref= "Mycut"
 Doexceptionaction "/>
 <aop:after pointcut-ref= mycut" method= "doreleaseaction"/>
 pointcut-ref= "Mycut" method= "dobasicprofiling"/>
 </aop:aspect>
</aop:config>

After the server's servlet container loads the Web.xml file, it uses a Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader class to read the Applicatio Ncontext.xml file, when parsing an AOP tag, it invokes the Parsecustomelement method resolution of the Beandefinitionparserdelegate instance, which looks for AOP The handler in namespace is the Aopnamespacehandler class and calls its resolve method to return the Namespacehandler instance (this procedure will call handlermapping if there is no handler in the Aopnamespac Ehandler init method).

Let's take a look at the Aopnamespacehandler initialization process:

The public void init () {
		//in 2.0 XSD as as-in 2.1 xsd.
		Registerbeandefinitionparser ("config", new Configbeandefinitionparser ());
		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 SPRINGC Onfiguredbeandefinitionparser ());
	}

We have seen here separately registered <aop:config></aop:config>, <aop:aspectj-autoproxy></aop:aspectj-autoproxy> and The parser for major labels such as <aop:scoped-proxy></aop:scoped-proxy>.

Just to introduce spring AOP (not to explain Spring's support for ASPECTJ), so let's look at how the parse method of configbeandefinitionparser resolves the tag.

Public Beandefinition Parse (element element, ParserContext parsercontext) {
		compositecomponentdefinition Compositedef =
				New Compositecomponentdefinition (Element.gettagname (), Parsercontext.extractsource (Element));
		Parsercontext.pushcontainingcomponent (compositedef);

		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;
	}

member functions for the Configbeandefinitionparser class:

     The first step of the method calls the Configureautoproxycreator (ParserContext, Element) method to register a aspectjawareadvisorautoproxycreator type of bean. This bean implements the Beanpostprocessor sub-interface instanitiationawarebeanpostprocessor. The implementation method is shown in the following illustration:

 public Object postprocessbeforeinstantiation (class<?> beanclass, String beanname) throws

		beansexception {Object CacheKey = Getcachekey (Beanclass, beanname);
				if (Beanname = = NULL | |!this.targetsourcedbeans.contains (beanname)) {if (This.advisedBeans.containsKey (CacheKey)) {
			return null; } if (Isinfrastructureclass (beanclass) | | | Shouldskip (BEANCLASS, Beanname)) {this.advisedBeans.put (CacheKey, Boolea
				N.false);
			return null;
			} if (Beanname!= null) {Targetsource Targetsource = Getcustomtargetsource (Beanclass, beanname);
				if (Targetsource!= null) {This.targetSourcedBeans.add (beanname);
				object[] specificinterceptors = Getadvicesandadvisorsforbean (Beanclass, Beanname, Targetsource);
				Object proxy = Createproxy (Beanclass, Beanname, Specificinterceptors, Targetsource);
				This.proxyTypes.put (CacheKey, Proxy.getclass ());
			return proxy;
	} return null; }

      It will use Getadvicesandadvisorsforbean (Beanclass, Beanname, Targetsource) Each time the bean is initialized Method gets all the advisor associated with the Bean and selects the available advisor based on the advisor-related configuration in the configuration file. Next Call Createproxy (Beanclass, Beanname, specificinterceptors) to create the proxy (AOP uses proxy mode to weave the code). When the agent is created, AOP resolves the label based on the node type in the configuration file. There are three types of tags that are parsed: Pointcut, Advisor, and Aspect. Here we take the advisor for example to look at the parsing process of the tag.

private void Parseadvisor (Element advisorelement, ParserContext parsercontext) {abstractbeandefinition advisordef = CRE
		Ateadvisorbeandefinition (Advisorelement, ParserContext);

		String id = advisorelement.getattribute (ID);
			try {this.parseState.push (new Advisorentry (ID));
			String advisorbeanname = ID; if (Stringutils.hastext (Advisorbeanname)) {parsercontext.getregistry (). Registerbeandefinition (AdvisorBeanName,
			ADVISORDEF);
			else {advisorbeanname = Parsercontext.getreadercontext (). Registerwithgeneratedname (Advisordef);
			Object pointcut = Parsepointcutproperty (advisorelement, ParserContext);
				if (pointcut instanceof beandefinition) {advisordef.getpropertyvalues (). Add (Pointcut, pointcut); Parsercontext.registercomponent (New Advisorcomponentdefinition (Advisorbeanname, Advisordef, (BeanDefinition) point
			Cut)); else if (pointcut instanceof String) {advisordef.getpropertyvalues (). Add (Pointcut, new Runtimebeanreference (Str Ing) (pointcut));
			Parsercontext.registercomponent (New Advisorcomponentdefinition (Advisorbeanname, advisordef));
		finally {This.parseState.pop (); }
	}

This resolves the pointcut attribute of the advisor tag and generates a Defaultbeanfactorypointcutadvisor advisor and registers it in the ParserContext.

Summing up the implementation of Spring AOP is basically: Configuring a bean that implements the Instantiationawarebeanpostprocessor interface, All of the advisor is found on the configuration file for the advisor and Pointcut configuration to generate agents as needed for the bean initialization. And in the process of generating the agent advice into the proxy object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.