Spring Framework Source code (VI): Parsing tags for spring AOP

Source: Internet
Author: User

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" method= "Doexceptionaction"/> <AOP: After pointcut-ref= "Mycut" method= "doreleaseaction"/> <aop:around pointcut-ref= "mycut" method= " Dobasicprofiling "/> </aop:aspect></aop:config>

The server's servlet container uses a Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader class to read applicatio after the Web. xml file is loaded The Ncontext.xml file, when parsing an AOP tag, invokes the Parsecustomelement method parsing of the Beandefinitionparserdelegate instance, which will look for AOP The handler in namespace is the Aopnamespacehandler class and calls its resolve method to return the Namespacehandler instance (in which case handlermapping is called if there is no handler in this procedure Aopnamespac Ehandler's Init method).

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

public void init () {//in 2.0 XSD as well as in 2.1 xsd.registerbeandefinitionparser ("config", new Configbeandefinitionpar Ser ()); Registerbeandefinitionparser ("Aspectj-autoproxy", New Aspectjautoproxybeandefinitionparser ()); Registerbeandefinitiondecorator ("Scoped-proxy", New Scopedproxybeandefinitiondecorator ());//Only in 2.0 xsd:moved to Context namespace as of 2.1registerBeanDefinitionParser ("spring-configured", new Springconfiguredbeandefinitionparser ());}

We see here the registration of <aop:config></aop:config>, <aop:aspectj-autoproxy></aop:aspectj-autoproxy> and <aop:scoped-proxy></aop:scoped-proxy> and other key tags of the parser.

This is just about spring AOP (does not explain spring's support for ASPECTJ), so let's take a look at how the Configbeandefinitionparser parse method parses 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 of the Configbeandefinitionparser class:

     The first step of the method called Configureautoproxycreator (ParserContext, Element) method to register a aspectjawareadvisorautoproxycreator type of bean. This bean implements the Beanpostprocessor sub-interface instanitiationawarebeanpostprocessor. The implementation method is as follows:

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, BOOLEAN.FALSE); return null;}} if (beanname! = null) {Targetsource Targetsource = Getcustomtargetsource (Beanclass, beanname); if (targetsource! = null) {T His.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 each time the bean is initialized (Beanclass, Beanname, Targetsource) Method gets all the advisor associated with the Bean and selects the advisor that can be used 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 parses the label based on the node type in the configuration file. There are three types of tags that are parsed here: Pointcut, Advisor, Aspect. Here we take the advisor as an example to see the parsing process of the label.

private void Parseadvisor (Element advisorelement, ParserContext parsercontext) {abstractbeandefinition advisordef = Createadvisorbeandefinition (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);p arsercontext.registercomponent (new Advisorcomponentdefinition (Advisorbeanname, Advisordef, (beandefinition) pointcut));} else if (pointcut instanceof String) {advisordef.getpropertyvalues (). Add (Pointcut, New Runtimebeanreference ((String) pointcut));p arsercontext.registercomponent (New Advisorcomponentdefinition (AdvisoRbeanname, Advisordef));}} finally {This.parseState.pop ();}}

This parses the Pointcut attribute of the advisor tag and generates a Defaultbeanfactorypointcutadvisor advisor and registers it in ParserContext.

To summarize the implementation of Spring AOP is basically: Configure a bean that implements the Instantiationawarebeanpostprocessor interface, All advisors are found based on the configuration file's profile on advisor and Pointcut to generate the agent for the bean when it is initialized. And in the process of generating the agent, the advice is woven into the proxy object.

Spring Framework Source code (VI): Parsing tags for spring AOP

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.