Description
ASPECTJ is an AOP framework based on the Java language
Spring2.0 later added support for ASPECTJ pointcut expressions
@AspectJ is a new feature of AspectJ1.5 that allows you to define slices directly in the bean class by JDK5 annotation technology, so you can use XML and annotations to develop AOP
New version of the spring framework, recommend using the ASPECTJ approach to develop AOP
There are five kinds of notices ASPECTJ
Before (Formals)
A predecessor notification (executed before the method executes, if the notification throws an exception, and prevents the method from running)
After (formals) returning [(formal)]
Post notification (the method executes after normal return, and if an exception is thrown in the method, the notification cannot be performed
Must be executed after the method is executed, so the return value of the method can be obtained. )
After (formals) throwing [(formal)]
The exception stops (executes after the method throws an exception, and cannot be performed if the method does not throw an exception)
After (formals)
Final notification (execution after completion of the method, regardless of whether there is an exception in the method, similar to a finally block inside the try-catch-finally)
Around (formals)
Wrapping notifications (before and after methods are executed separately, you can block the execution of methods, you must manually execute the target method)
Correspond in XML:
Imported JAR Packages:
1.AOP Federation Specification
2.spring AOP Implementation
3.aspect specification
4.spring Aspect implementation
XML Small Example
Writing process:
1. Target class: Implement + Interface
2. Cutting class: Writing multiple notifications
3.AOP programming, applying notifications to target classes
4. Test
Target class:
Interface
Public interface UserService {public
boolean addUser ();
public void UpdateUser ();
public void DeleteUser ();
}
Realize
public class Userserviceimpl implements UserService {
@Override public
boolean addUser () {
System.out.println ("Userservicedaoimpl addUser");
return true;
}
@Override public
void UpdateUser () {
//test throws an exception notification
// int i=1/0;
System.out.println ("Userservicedaoimpl updateuser");
}
@Override public
void DeleteUser () {
System.out.println ("Userservicedaoimpl deleteuser");
}
Slice class
The five notifications have no restrictions on the method name, but the format of the method is limited. The format of the method is given in the XML configuration.
public class Myaspect {
//predecessor notification public
Void before (Joinpoint joinpoint) {
System.out.println (" Myaspect-before ");
}
Final notification of public
void after (Joinpoint joinpoint) {
System.out.println ("Myaspect-after");
}
Surround notification public
Object around (Proceedingjoinpoint joinpoint) throws throwable{System.out.println
(" Myaspect-around-before ");
Object Obj=joinpoint.proceed ();//Execute Target method
System.out.println ("myaspect-around-");
return obj;
}
The post notification public
void afterreturning (Joinpoint joinpoint,object ret) {
System.out.println (" Myaspect-afterreturning "+joinpoint.getsignature (). GetName () +" \ T "+ret);
}
Exception notification public
void afterthrowing (Joinpoint joinpoint,throwable e) {
System.out.println (" Myaspect-afterthrowing "+e.getmessage ());
}
}
Spring Configuration
<?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" Xsi:sche malocation= "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 "> <!--Create target class--> <bean id=" Userserviceid "cl ass= "Com.scx.xmlproxy.test.UserServiceImpl" ></bean> <!--Create slice class (notification)--> <bean id= "Myaspectid" class= "Com.scx.xmlproxy.test.MyAspect" ></bean> <!--AOP programming--> <aop:config proxy-target-class= " True "> <aop:aspect ref=" Myaspectid "> <aop:pointcut expression=" Execution (* Com.scx.xmlprox Y.test.*.* (..)) " Id= "MYPOintcut "/> <!--forward notification <aop:before method=" before "pointcut-ref=" Mypointcut "/>
Method Format (parameter 1) Parameter 1: Connection point Description Method: Methods name Pointcut: pointcut expression Pointcut-ref: Pointcut Reference--> <!--final Notice <aop:after method= "after" pointcut-ref=
"Mypointcut"/> Method Format (parameter 1) Parameter 1: Connection point description--> <!--surround Notification
<aop:around method= "Around" pointcut-ref= "Mypointcut"/> Method Format (parameter 1) Parameters: Org.aspectj.lang.ProceedingJoinPoint--> <!--post notification <aop:after
-returning method= "afterreturning" returning= "ret" pointcut-ref= "Mypointcut"/> Method Format (parameter 1, parameter 2)
Parameter 1: Connection point description Parameter 2: Type object, parameter name returning= "RET" Configuration--> <!--
Throw an exception <aop:after-throwing method= "afterthrowing" pointcut-ref= "Mypointcut" throwing= "E"/> Method format
(parameter 1, parameter 2) Parameter 1: Connection point Description Object Parameter 2: Get exception information, type Throwable, parameter name is configured by throwing= "E"--> </aop:aspect> </aop:config> </beans>
Test:
When testing, it is best to execute only one notification, otherwise the results will not be as orz as imagined.
@org. Junit.test public
void Testproxy () {
String xmlpath= "Com/scx/xmlproxy/test/applicationcontext.xml";
ApplicationContext applicationcontext=new Classpathxmlapplicationcontext (xmlpath);
UserService userservice= (UserService) Applicationcontext.getbean ("Userserviceid");
Userservice.adduser ();
Userservice.updateuser ();
Userservice.deleteuser ();
}
Test results:
Forward notification:
Post notification:
Exception Notification:
In order to get an exception I added int i = 1/0 inside the UpdateUser method of the implementation class. This line of code
The result shows an exception of 0, as shown in the figure
At this time we modify the final notice. Run Result:
We found that the UpdateUser method did not output because of the exception, but the final notification output.
Surround Notification:
Note Small example in writing an article to give ~