ASPECTJ is a tangent-oriented framework that extends the Java language, defines the AOP syntax,
Ability to provide code weaving at compile time, so it has a dedicated compiler to generate class files that conform to bytecode byte coding specifications
Make sure to use JDK 5.0 or later.
01. Using Annotation Callout Enhancement (AspectJ): Replaces the configuration of the Aop:pointcut node in the configuration file
Add a configuration file for jar and log4j
Aspectj-1.8.7.jar
Aspectjweaver.jar
To add a header file:
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xsi: add: (Can be found in the document: "40.2.6 the TX (transaction) schema")
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop.xsd
You can enable support for @aspectj annotations by simply adding the <aop:aspectj-autoproxy/> element to the beans child node.
Spring will automatically create an agent for the matching bean;
Control inversion: Also declare an instance of Userlogger: <bean class= "ASPECTJ_AOP. Userlogger "></bean>
★ Use the @aspect logo to give a class to the spring container management
@Aspect
public class Userlogger {
Private static final Logger log = Logger.getlogger (Userlogger.class);
Matches all of the type return values, all method names in the Proxy_dynamic package, all parameter numbers and types
@Before ("Execution (* Aspectj_dao). dosomthing.* ()) ")
public void before () {
System.out.println ("Pre-Enhancement Execution 111!");
}
@AfterReturning ("Execution (* Aspectj_dao). dosomthing.* ()) ")
public void after () {
System.out.println ("Pre-Enhancement Execution 222!");
}
}
★ In the above Operation @Before & @AfterReturning need to set the pointcut separately:
Execution (modifiers-pattern? ret-type-pattern Declaring-type-pattern?
Name-pattern (Param-pattern) Throws-pattern?)
In addition to the return type pattern (Ret-type-pattern in the code snippet above), the name pattern and the parameter mode,
All parts are optional, and the return type mode feels that the return type of the method must match one connection point in turn.
The most frequently used return type pattern is *, and he represents a match for any return type. A fully qualified type name will only match the method that returns the given type.
The name pattern matches the method name. You can use * as all or part of the naming pattern. The parameter pattern is slightly more complex: () matches a method that does not accept any parameters,
and (..) Matches a method that interprets any type of parameter (0 or more).
The pattern (*) matches a method that accepts any type of argument. Mode (*,string) matches a method that accepts two parameters,
The first can be any type, and the second must be of type string.
02. Using the Joinpoint operation
@Before ("Execution (* Aspectj_dao). dosomthing.* ()) ")
public void before (Joinpoint point) {
System.out.println ("Pre-Enhancement Execution 111!");
SYSTEM.OUT.PRINTLN ("target object:" +point.gettarget ());
System.out.println ("Method:" +point.getsignature (). GetName ());
System.out.println ("Number of arguments:" +point.getargs (). length);
}
@AfterReturning (value= "Execution (* Aspectj_dao. dosomthing.* ()) ", returning=" ReturnValue ")
public void afterreturning (Joinpoint point,object returnvalue) {
System.out.println ("Pre-Enhancement Execution 222!");
The return value of System.out.println (Point.getsignature (). GetName () + "method is:" +returnvalue);
}
/**
* Java.lang.object[]getargs (): Gets the parameter list of the connection point method Runtime
* Signature getsignature (): Gets the connection point method before the object
* Java.lang.ObjectgetTarget (): Gets the target object where the connection point resides
* Java.lang.ObjectgetThis (): Gets the proxy object itself
*/
Surround enhancement
@Around (value= "Execution (* Aspectj_dao. dosomthing.* ()) ")
public void Around (Proceedingjoinpoint pjp) throws throwable{
SYSTEM.OUT.PRINTLN ("Surround-forward enhancement");
Pjp.proceed ();
System.out.println ("Surround back enhancement");
}
Exception enhancement
@AfterThrowing (value= "Execution (* Aspectj_dao. dosomthing.* ()) ")
public void Afterthrowing () throws throwable{
SYSTEM.OUT.PRINTLN ("Exception enhancement");
}
Final enhancement (This method is performed anyway)
@After (value= "Execution (* Aspectj_dao. dosomthing.* ()) ")
public void After () throws throwable{
System.out.println ("Final Enhancement 、、、");
}
——————————————— Split Line ———————————————————————————————————————————
03.Aspect XML-based annotation configuration (using Pojo as the enhanced Class) for spring and program decoupling
The Applicationcontext.xml document was replaced here for Applicationcontextxml.xml
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
01. Pre-enhancement matches all types of return values, all method names in the Proxy_dynamic package, all parameter numbers and types
public void before (Joinpoint point) {
System.out.println ("Pre-Enhancement Execution 111!");
}
02. Rear-Mounted enhancements
public void afterreturning (Joinpoint point,object returnvalue) {
System.out.println ("Post-Enhancement Execution 222!");
System.out.println ("Gettarget object:" +point.gettarget (). GetClass ());
System.out.println ("Getthis object:" +point.getthis (). GetClass ());
The return value of System.out.println (Point.getsignature (). GetName () + "method is:" +returnvalue);
}
03. Surround Enhancement
public void Around (Proceedingjoinpoint pjp) throws throwable{
SYSTEM.OUT.PRINTLN ("Surround-forward enhancement");
Pjp.proceed ();
System.out.println ("Surround back enhancement");
}
04. Exception Enhancement
public void Afterthrowing () throws throwable{
SYSTEM.OUT.PRINTLN ("Exception enhancement");
}
05. Final enhancement (this method is performed anyway)
public void After () throws throwable{
System.out.println ("Final Enhancement 、、、");
}
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
These are the common wording of Pojo.
How do I make the Pojo implementation enhanced?
Add a configuration to the configuration file:
<!--declares the bean that the enhancement method is in--
<bean id= "Myaspect" class= "ASPECTJ_AOP. Userlogger_xml "></bean>
<bean id= "Do1" class= "Aspectj_dao. Dosomthing "></bean>
<aop:config>
<aop:pointcut expression= "Execution (* Aspectj_dao. dosomthing.* ()) "id=" MyPoint "/>
<aop:aspect ref= "Myaspect" >
<aop:before method= "Before" pointcut-ref= "MyPoint"/>
<aop:after-returning method= "afterreturning" pointcut-ref= "MyPoint" returning= "ReturnValue"/>
<aop:after method= "after" pointcut-ref= "MyPoint"/>
<aop:around method= "Around" pointcut-ref= "MyPoint"/>
<aop:after-throwing method= "afterthrowing" pointcut-ref= "MyPoint"/>
</aop:aspect>
</aop:config>
Instance link: Link: http://pan.baidu.com/s/1miM00Uc Password: 3LFL
Spring aop--Pre-enhancement and post-enhancement using annotations aspect and non-intrusive configuration