First, the annotation-based ASPECTJ
1. To use ASPECTJ annotations in spring applications, you must include the AspectJ class library under Classpath:
Com.springsource.org.aopalliance.jar and Com.springsource.org.aspectj.weaver.jar; Adding an AOP schema to the <beans> root element ; Enable ASPECTJ direct support in the spring IOC container, as long as an empty XML element is defined in the Bean configuration file <aop:aspectj-autoproxy>; When the spring IOC container detects the bean The <aop:aspectj-autoproxy> element in the configuration file automatically creates a proxy for the bean that matches the aspectj slice.
2. Notification is a simple Java method labeled with some annotations, and ASPECTJ supports 5 types of notification annotations:
1) @Before: Pre-notification, execution before method execution
2) @After: Ultimate Enhancement
3) @AfterReturning: Post notification, execute after method returns result
4) @AfterThrowing: Exception notification, executed after the method throws an exception
5) @Around: Surround notification, around the execution of the method
3. A typical pointcut expression is to match various methods according to the signature of the method:
-execution (* * * * (.. ): The first * represents the matching of any modifier and return value, the second * represents an object of any class, and the third * represents any method in the argument list: Match any number of parameters.
-Execution (* com.bupt.springtest.aop.arithmeticcalculator.* (..)) : Matches all methods declared in Arithmeticcalculator, the first * represents any modifier and any return value; the second * represents either method; Match any number of parameters. If the target class or interface is in the same package as the slice, the package name can be omitted.
-Execution (Public * arithmeticcalculator.* (..)) : Matches all public methods in the Arithmeticcalculator class. -Execution (public double arithmeticcalculator.* (..)) : A method that matches the value returned in a double type in arithmeticcalculator
-Execution (public double arithmeticcalculator.* (double,..)) : match the first parameter to a method of type double. Match any number of arguments of any type
-Execution (public double arithmeticcalculator.* (double, Double)): A method that matches the parameter type double, double type
4. Case
1. Create an interface
Package Cn.happy.service;public interface Isomeservice {public void Dosome (); Public String Add (); public void Insert (); public void Update (); public void Delete (); public void Select ();
2. Implement the Interface class
Package Cn.happy.service;public class Someservice implements Isomeservice {public void Dosome () { System.out.println ("Code OK"); } Public String Add () { System.out.println ("=========log log ==============="); int i = 5/0; return "Add"; } public void Insert () { System.out.println ("Insert"); } public void Update () { System.out.println ("Update"); } public void Delete () { System.out.println ("delete"); } public void Select () { int i = 5/0; System.out.println ("select");} }
3. Writing Enhanced Classes
Package Cn.happy.service;import Org.aspectj.lang.proceedingjoinpoint;import org.aspectj.lang.annotation.*;@ Aspectpublic class Myaspect {@Before (value= "Execution (* *). Service.*.* (..)) ") public void Myaspectj () {System.out.println ("---I am the predecessor Enhancement---"); } @AfterReturning (value= "Execution (* *). Service.*.* (..)) ") public void Myaspectjafter () {System.out.println ("---I am post enhancement---"); } @Around (value= "Execution (* *). Service.*.* (..)) ") Public Object Myaspectjaround (Proceedingjoinpoint proceedingjoinpoint) throws Throwable {System.out.println ("---i am Enhanced---") around the front; Object result = Proceedingjoinpoint.proceed (); SYSTEM.OUT.PRINTLN ("---I was surrounded by enhanced---"); if (result!= null) {string str = (string) result; The return value can be changed to str.touppercase (); } else {return null; }} Exception Enhancement @Pointcut ("Execution (* *). Service.*.* (..)) ") expression annotation public void Mypoincut () {} @AfterThrowing ("Mypoincut ()") publicvoid throwing () {SYSTEM.OUT.PRINTLN ("---error---"); }//Tangent expression @Pointcut ("Execution (* *). Service.*.insert (..)) ") public void Insert () {} @Pointcut ("Execution (* *): Service.*.update (..)) ") public void Update () {} @Pointcut (Execution (* *). Service.*.delete (..)) ") public void Delete () {} @Before ("Insert () | | | Update () | | Delete () ") public void Myaspectj () {System.out.println ("---I am the predecessor enhancement Opens the thing---"); }//Final enhancement @After ("Execution (* *). Service.*.* (..)) ") public void After () {System.out.println ("I am the final enhancement"); }}
4. Writing an XML configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:p= "http://www.springframework.org/schema/p" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= " http// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/ http WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- Annotation implementation- <!--target Object-- <bean id= "Someservice" class= "Cn.happy.serviceXML.SomeService" ></ Bean> <!--enhancement notification –> - <bean id= "Beforeadvice" class= " Cn.happy.service.MyAspect "></bean> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
5. Write Test class
Package Cn.happy;import Cn.happy.service.isomeservice;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test { @Test public void Testaspect () { ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml "); Isomeservice Someservice = (isomeservice) context.getbean ("Someservice"); Someservice.insert (); Someservice.update (); Someservice.delete (); Someservice.select (); }}
Second, XML-based ASPECTJ implementation
1. XML-based implementations need to include the <aop> node in the XML configuration file.
2. Case
1) Create an interface class
Package Cn.happy.servicexml;public interface Isomeservice {public void Dosome (); Public String Add ();}
2) Implement the interface class
Package Cn.happy.servicexml;public class Someservice implements Isomeservice {public void Dosome () { System.out.println ("Code OK"); } Public String Add () { System.out.println ("log------"); int i = 5/0; return "Add";} }
3) Writing Enhanced classes
Package Cn.happy.servicexml;import Org.aspectj.lang.proceedingjoinpoint;public class Myaspect {public void Mybefore () { System.out.println ("=========before============"); } public void After () { System.out.println ("============after==============="); } Public Object Myarround (Proceedingjoinpoint proceedingjoinpoint) throws Throwable { System.out.println ("I am enhanced before wrapping" ); Object result = Proceedingjoinpoint.proceed (); System.out.println ("I was enhanced after surround"); if (result!= null) { string str = (string) result; The return value can be changed str.touppercase (); } else { return null; }
}
4). Writing an XML configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:p= "http://www.springframework.org/schema/p" xmlns:xsi= "H Ttp://www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beans HT Tp://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop.xsd > <!--target objects--<bean id= "Someservice" class= "Cn.happy . Servicexml.someservice "></bean> <!--Enhanced Class--<bean id=" aspect "class=" Cn.happy.serviceXML.MyAspe CT ></bean> <!--aop--> <aop:config> <!--set a pointcut--<aop:pointcut id= "Mycu T "expression=" Execution (* *). Servicexml.*.* (..)) " ></aop:pointcut> <aop:aspect ref= "aspect" > <aop:before method= "Mybefore" pointcut-ref= "Mycut" ></aop:before> <!--<aop:after-returning method= "after" pointcut-ref= "Mycut" ;</aop:after-returning>--> <!--<aop:around method= "Myarround" pointcut-ref= "Mycut" ></aop:ar Ound>--> </aop:aspect> </aop:config></beans>
5) Writing test classes
Package Cn.happy;import Cn.happy.servicexml.isomeservice;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test01 { //AspectJ XML @ Test public void Testaspectxml () { ApplicationContext context = new Classpathxmlapplicationcontext (" Applicationcontext.xml "); Isomeservice someservice= (Isomeservice) Context.getbean ("Someservice"); Someservice.add (); Someservice.dosome (); }}
The AspectJ of spring implements AOP