1. What is AOP
AOP is the acronym for Aspect Oriented programming, meaning aspect-oriented programming, which is actually a continuation of the GOF design pattern.
2. Some terms about spring AOP:
A, Slice (Aspect): In spring AOP, slices can be implemented using common classes or @aspect annotations (@AspectJ style) in ordinary classes
B, Connection point (Joinpoint): A join point in spring AOP represents the execution of a method
C, Notification (Advice): An action performed on a particular connection point (joinpoint) of the slice. There are various types of notifications, including "Around", "before" and "after" notifications. Many of the AOP frameworks, including spring, take the interceptor notification model and maintain a chain of interceptors centered on the connection point
D, Pointcuts (Pointcut): Defines one or a set of methods that can produce notifications when they are executed, and spring defaults to the use of ASPECTJ pointcut syntax.
3. Notification type
A, forward notification (@Before): A notification to be executed before a connection point (join points), but this notification cannot prevent execution before the connection point (unless it throws an exception)
B, notification after return (@AfterReturning): A notification to be executed after a connection point is completed normally: for example, a method does not throw any exceptions, and returns normally
C, notification when an exception is thrown (@AfterThrowing): Notification when a method throws an exception exit
D, after notification (@After): When a connection point out of the time to execute the notification (whether the normal return or abnormal exit)
E, surround Notification (@Around): A notification that surrounds a join point, such as a method call. This is one of the most powerful types of notification, which can complete custom behavior before and after the method call, and it also chooses whether to continue the connection point or return directly to their own return value or throw an exception to end the execution
4, @AspectJ-style AOP configuration
The Spring AOP configuration has two flavors:
A, XML style = Implement spring AOP with declarative form
B, ASPECTJ style = implement spring AOP with annotation form
5. Example
Cutting plane Type Testaspect
Package COM.SPRING.AOP; /** * Section/public class Testaspect {public void Doafter (Joinpoint jp) {System.out.println ("Log Ending Me
Thod: "+ jp.gettarget (). GetClass (). GetName () +". "
+ jp.getsignature (). GetName ());
The 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 (). getc
Lass (). GetName () + "."
+ jp.getsignature (). GetName ()); public void dothrowing (Joinpoint JP, Throwable ex) {System.out.println (' method ' + jp.gettarget (). GetClass ().
GetName () + "." + jp.getsignature (). GetName () + "throw exception");
System.out.println (Ex.getmessage ()); } private void Sendex (String ex) {//todo send SMS or message reminder}}
Package com.spring.service;
/**
* Interface a
/public interface Aservice {public
void Fooa (String _msg);
public void BarA ();
}
Package com.spring.service;
/**
* Interface a implementation class *
/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+)");
}
Package com.spring.service;
/**
* service Class B
*
/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 ()");
}
ApplicationContext
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring -aop-2.5.xsd "default-autowire=" AutoDetect "> <aop:config> <aop:aspect id=" Testaspect "ref=" aspectbe An "> <!--Configure all methods for all classes or interfaces under Com.spring.service package--> <aop:pointcut id=" Businessservice "Expres sion= "Execution (* com.spring.service.*.* (..))"/> <aop:before pointcut-ref= "businessservice" method= "DoBefore" "/> <aop:after pointcut-ref=" businessservice "method=" Doafter "/> <aop:around pointcut-ref=" Busin
Essservice "method=" Doaround "/> <aop:after-throwing pointcut-ref= "Businessservice" method= "dothrowing" throwing= "ex"/> </aop:aspect> </aop:config> <bean id= "Aspectbean" class= "Com.spring.aop.TestAspect"/> <bean id= "Aservice" C lass= "Com.spring.service.AServiceImpl" ></bean> <bean id= "bservice"
Com.spring.service.BServiceImpl "></bean> </beans>
Test class Aoptest
public class Aoptest extends Abstractdependencyinjectionspringcontexttests {
private aservice aservice;
Private Bserviceimpl bservice;
Protected string[] Getconfiglocations () {
string[] configs = new string[] {"/applicationcontext.xml"};
return configs;
}
/**
* Test normal call/public
void Testcall ()
{
System.out.println ("Springtest JUnit test");
Aservice.fooa ("JUnit test Fooa");
Aservice.bara ();
Bservice.foob ();
Bservice.barb ("JUnit test BarB", 0);
}
/**
* Test after-throwing/public
void Testthrow ()
{
try {
bservice.barb () ' JUnit call BarB ", 1);
} catch (IllegalArgumentException e) {
}
} public
void Setaservice (Aservice service) {
Aservice = Service;
}
public void Setbservice (Bserviceimpl service) {
bservice = service;
}
}
The results of the operation are as follows:
Log begining Method:com.spring.service.AServiceImpl.fooA
aserviceimpl.fooa (msg:junit test Fooa)
log ending Method:com.spring.service.AServiceImpl.fooA
Process time:0 ms
Log Begining method: Com.spring.service.AServiceImpl.barA
Aserviceimpl.bara ()
log ending method: Com.spring.service.AServiceImpl.barA
Process time:0 ms
Log Begining method: Com.spring.service.BServiceImpl.fooB
bserviceimpl.foob ()
log ending method: Com.spring.service.BServiceImpl.fooB
Process time:0 ms
Log Begining method: Com.spring.service.BServiceImpl.barB
bserviceimpl.barb (msg:junit test BarB type:0)
log ending method: Com.spring.service.BServiceImpl.barB
Process time:0 ms
Log Begining method: Com.spring.service.BServiceImpl.barB
Bserviceimpl.barb (msg:junit call BarB type:1)
log ending method: Com.spring.service.BServiceImpl.barB method
Com.spring.service.BServiceImpl.barB Throw exception
test exception