on the principles of Spring (iii) AOP I have introduced the basic concepts and implementation principles of AOP in detail, here is a sample code.
one, the XML way
1. Testaspect: Slice Class
Package COM.SPRING.AOP; Import Org.aspectj.lang.JoinPoint; Import Org.aspectj.lang.ProceedingJoinPoint; public class Testaspect {public void Doafter (Joinpoint jp) {System.out.println ("Log ending Method:" + j P.gettarget (). GetClass (). GetName () + "." + jp.getsignature (). GetName ()); } public Object Doaround (Proceedingjoinpoint pjp) throws Throwable {long time = System.currenttimemillis ( ); Object RetVal = Pjp.proceed (); Time = System.currenttimemillis ()-time; SYSTEM.OUT.PRINTLN ("Process time:" + time + "MS"); return retVal; } public void Dobefore (Joinpoint jp) {System.out.println ("Log begining Method:" + Jp.gettarget (). Getclas S (). GetName () + "." + jp.getsignature (). GetName ()); } public void Dothrowing (Joinpoint JP, Throwable ex) {System.out.println ("method" + jp.gettarget (). GETCL (). GetName () + "." + jp.getsignature (). GetName () + "throw exception"); System.out.println (Ex.getmessage ()); } }
2. Aserviceimpl: Target Object
Package com.spring.service; Use the JDK dynamic Agent public class Aserviceimpl implements Aservice {public void BarA () { System.out.println (" Aserviceimpl.bara () "); } public void Fooa (String _msg) { System.out.println ("Aserviceimpl.fooa (msg:" + _msg + ")"); } }
3. Bserviceimpl: Target Object
Package com.spring.service; Use Cglib public class Bserviceimpl {public void BarB (String _msg, int _type) { System.out.println (" Bserviceimpl.barb (msg: "+ _msg +" type: "+ _type +") "); if (_type = = 1) throw new IllegalArgumentException ("Test exception") ; public void Foob () { System.out.println ("Bserviceimpl.foob ()"); } }
4. applicationcontext:spring configuration file
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xmlns:contex t= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" Xsi:schema location= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring- Aop-3.1.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-c Ontext-3.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.x SD "> <aop:config> <aop:aspect id=" Testaspect "ref=" Aspectbean "> <!--configuration Com.spr Ing.service all the methods of all classes or interfaces under the package--<AOP:Pointcut id= "businessservice" expression= "Execution (* com.spring.service.*.* (..))"/> <aop:before Point cut-ref= "Businessservice" method= "Dobefore"/> <aop:after pointcut-ref= "businessservice" method= "DoAfter "/> <aop:around pointcut-ref=" businessservice "method=" Doaround "/> <aop:after-throwi ng pointcut-ref= "Businessservice" method= "dothrowing" throwing= "ex"/> </aop:aspect> </aop:config > <bean id= "Aspectbean" class= "Com.spring.aop.TestAspect"/> <bean id= "Aservice" class= "com.sp Ring.service.AServiceImpl "></bean> <bean id=" bservice "class=" Com.spring.service.BServiceImpl "> </bean> </beans>
Second, the annotation (Annotation) way
1. Testannotationaspect
Package COM.SPRING.AOP; Import Org.aspectj.lang.ProceedingJoinPoint; Import Org.aspectj.lang.annotation.After; Import org.aspectj.lang.annotation.AfterReturning; Import org.aspectj.lang.annotation.AfterThrowing; Import Org.aspectj.lang.annotation.Around; Import Org.aspectj.lang.annotation.Aspect; Import Org.aspectj.lang.annotation.Before; Import Org.aspectj.lang.annotation.Pointcut; @Aspect public class Testannotationaspect {@Pointcut ("Execution (* com.spring.service.*.* (..))") private void Pointcutmethod () {}//declaration of the Pre-notification @Before ("Pointcutmethod ()") public void Dobefore () { System.out.println ("Pre-notification"); }//Declare the post-notification @AfterReturning (pointcut = "Pointcutmethod ()", returning = "result") public void Doafterreturn ing (String result) {System.out.println ("post notification"); SYSTEM.OUT.PRINTLN ("---" + result + "---"); }//Declaration exception Notification @AfterThrowing (pointcut = "Pointcutmethod ()", throwing = "e") pubLIC void doafterthrowing (Exception e) {System.out.println ("exception notification"); System.out.println (E.getmessage ()); }//declares the final notification @After ("Pointcutmethod ()") public void Doafter () {System.out.println ("final Notice"); }//Declaration wrapping Notification @Around ("Pointcutmethod ()") Public Object Doaround (Proceedingjoinpoint pjp) throws Throwable { System.out.println ("Entry Method---surround notification"); Object o = pjp.proceed (); System.out.println ("Exit Method---surround notification"); return o; } }
2. applicationcontext:spring configuration file
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xmlns:contex t= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" Xsi:schema location= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring- Aop-3.1.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-c Ontext-3.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.x SD "> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean class=" Org.springframework.aop.aspe Ctj.annotation.AnnotationAwareAspectJAutoProxyCreatoR "/> <bean id=" Aspectbean "class=" Com.spring.aop.TestAnnotationAspect "/> <bean id=" Aservice "CLA ss= "Com.spring.service.AServiceImpl" ></bean> <bean id= "bservice" class= " Com.spring.service.BServiceImpl "></bean> </beans>
Add:
Any notification (Advice) method can define the first parameter as a org.aspectj.lang.JoinPoint type. The joinpoint interface provides a number of useful methods, such as Getargs () (return method parameters), Getthis () (Return proxy object), Gettarget () (return target), Getsignature () (Returns information about the method being notified) and toString (), which prints out useful information about the method being notified.
The Signature object returned by Getsignature () can be cast to Methodsignature, which is powerful enough to get all the method information including the parameter name.
Iii. Summary
Through detailed explanations and concrete examples, I hope to present you with a complete primer of AOP.
On the example of Spring (four) AOP