(i) Spring AOP: XML-based configuration file

Source: Internet
Author: User

One of the two most important features of spring is the aspect-oriented programming, the following example is the simplest spring AOP,AOP based on the XML configuration file some of the terms I would not say, or direct operation of the intuitive

One, MAVEN relies on

        <!--spring-->        <dependency>            <groupId>org.springframework</groupId>            < artifactid>spring-context</artifactid>            <version>3.1.2.RELEASE</version>        </ Dependency>        <!--aspectj-->        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjweaver</artifactId>            <version>1.8.0</version>        </ Dependency>        <!--dynamic Agent implementation--        <dependency>            <groupId>cglib</groupId>            <artifactId>cglib</artifactId>            <version>2.2.2</version>        </ Dependency>
Second, a very common class and method

Package Cn.cjc.spring.aop.service;public class Logservice {public    void Sayhi (String userName) {        System.out.println ("Hi," + UserName);}    }
Third, Slice class

Package Cn.cjc.spring.aop.aspect;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.ProceedingJoinPoint; public class Logaspect {/** * Front notification */public void before (Joinpoint call) {String ClassName = CALL.G        Ettarget (). GetClass (). GetName ();        String methodName = Call.getsignature (). GetName ();    System.out.println ("Pre-Notification:" + ClassName + "class" + MethodName + "method to start Execution");    }/** * POST notification */public void Afterreturn () {System.out.println ("post notification");    }/** * Final notice */public void after (String userName) {System.out.println (UserName + ", final notification");    }/** * Exception notification */public void Afterthrow () {SYSTEM.OUT.PRINTLN ("exception notification"); }/** * Surround notification */public Object Around (proceedingjoinpoint call, String userName) {System.out.println (        "Surround notice");        This.before (call);        Object result = null;            try {result = Call.proceed ();        This.afterreturn (); } catch (ThroWable e) {this.afterthrow ();        } finally {This.after (userName);    } return result; }}

Iv. Spring configuration file Beans.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xsi:sche malocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframewo Rk.org/schema/aop/spring-aop-3.0.xsd "> <!--normal Bean declaration--<bean id=" Logservice "class=" CN.CJC.SPRING.AOP . Service. Logservice "/> <bean id=" Logaspect "class=" Cn.cjc.spring.aop.aspect.LogAspect "/> <!--AOP configuration starts--&lt ;aop:config> <!--declare Id=logaspect class as tangent-to-<aop:aspect ref= logaspect > <!--tangency > <aop:pointcut id= "pc1" expression= "Execution (* cn.cjc.spring.aop.service). *. * (String)) and args (name) "/> <!--Tangent 2--> <aop:pointcut id= "PC2" expression= "Execution (* cn.cjc.spring.aop.service). *.*(..))" /> <!--declares the Before method in the slice as a pre-notification and specifies a pointcut for the notification--<aop:before method= "before" pointcut-ref= "PC2"/&            Gt <!--Declare the Afterreturn method in the slice as a post notification and specify the point-of-contact for the notification--<aop:after-returning method= "Afterreturn" pointcut-ref= "PC2" /> <!--declares the After method in the slice as the final notification and specifies the pointcut of the notification, while intercepting the parameters of the Pointcut--<aop:after method= "after" pointcut-ref= "PC1" arg-names= "name"/> <!--declares the Afterthrow method in the slice as an exception notification and specifies the point-and-click of the notification--<aop:after-throwing Me Thod= "Afterthrow" pointcut-ref= "PC2"/> <!--Declare the around method in the slice as wrapping notification and specify the pointcut of the notification, while intercepting the parameters of the tangent point--<a Op:around method= "Around" pointcut-ref= "PC1" arg-names= "name"/> </aop:aspect> </aop:config></b Eans>
The expression of tangent 1 means: matches all the methods under all Cn.cjc.spring.aop.service packages and their sub-packages and only one entry is of type string

The expression of tangent 2 means: matches all the methods under all Cn.cjc.spring.aop.service packages and their sub-packages

Pre-notification: Notification of weaving before a method that matches a tangent expression is performed

Post-notification: A notification that is woven after a successful method of tangent expression matching is executed

Final notification: A notification that will be woven after a success or failure of a method that matches a tangent expression

Exception notification: Notification of weaving after a method that matches a tangent expression to perform a throw exception

Surround notification: A notification that can be woven before, after, or after a method that matches a tangent expression

In summary the meaning of the,<aop:before> tag is that the before method of the slice class is executed before the method of the tangent 2 expression matches, and then the Tangency method is executed.

V. Testing

Package Cn.cjc.spring.aop.test;import Cn.cjc.spring.aop.service.logservice;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Springaoptest {public    static void Main (string[] args) {        ApplicationContext context = new Classpathxmlapplicationcontext ("Beans.xml");        Logservice Logservice = Context.getbean ("Logservice", logservice.class);        Logservice.sayhi ("Junki");}    }

(i) Spring AOP: XML-based configuration file

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.