Mode one: ASPECTJ-based XML configuration
Mode two: The annotation method based on AspectJ
ASPECTJ-based XML configuration
1) Introduction of the relevant Jar package
2) Create a spring core configuration file, you must import the constraints of AOP
<? XML version = "1.0" encoding = "UTF-8" ?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xsi: schemalocation= "http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aophttp:// Www.springframework.org/schema/aop/spring-aop.xsd ">
</Beans>
3) using expressions to configure Pointcuts
[1] entry point: the actual enhancement method
[2] Execution (< access modifier >?< return value type >< method name full path > (< from Parameter >) < exception >)
There are several ways to make an expression:
①execution (* cn.aop.Book.add (..)) Enhancements to all methods named Add in the Book class under the CN.AOP package
*: Indicates that it can be any access modifier
Cn.aop.Book.add: The full path of the method
(..): Indicates that there can be parameters or no
②execution (* cn.aop.book.* (..)) Enhancements to all methods in the book class under the CN.AOP package
③execution (* * * * (..)) All methods in all classes under all packages are enhanced
④execution (* save* (..)) Enhancements to all methods that start with Save
Example of a predecessor enhancement:
<?XML version= "1.0"encoding= "UTF-8"?> <Beansxmlns= "Http://www.springframework.org/schema/beans" 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/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--1Configuration objects (creating objects)- <BeanID= "book"class= "Cn.bdqn.SpringAspectJ.Book"/> <BeanID= "MyBook"class= "Cn.bdqn.SpringAspectJ.Mybook"/> <!--2ConfigurationAOPOperation- <AOP: Config> <!--2.1Configure Pointcuts- <AOP:p ointcutexpression= "Execution" (* CN. springaspectj.book.* (..)) "ID= "POINTCUT1"/> <!--2.2Configure Facets Use the reinforcement to the method above,to specify which enhancement- <AOP: Aspectref= "MyBook"> <!--Configuring enhanced Types Method:which method is used in the enhanced class as the predecessor Enhancement- <AOP: BeforeMethod= "Before"Pointcut-ref= "POINTCUT1"/> </AOP: Aspect> </AOP: Config> </Beans> |
4) writing test Classes
Test { @org. junit.test Test () { Classpathxmlapplicationcontext ("beans.xml"). Getbean ("book"); Book.add (); } } |
The test results are as follows:
Post-enhanced instances are the same as pre-built
Surround Enhancement:
Classes that need to be enhanced
Book { Add () { System. out. println ("Add ..."); } } |
Methods in the Enhanced class:
Around (Proceedingjoinpoint proceedingjoinpoint) { // method before System. out. println (" method before ..."); // execute the Enhanced method { Proceedingjoinpoint.proceed (); (Throwable throwable) { Throwable.printstacktrace (); } // method After System. out. println (" after method "); } |
XML configuration file
<?XML version= "1.0"encoding= "UTF-8"?> <Beansxmlns= "Http://www.springframework.org/schema/beans" 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/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--1Configuration Objects- <BeanID= "book"class= "Cn.bdqn.SpringAspectJ.Book"/> <BeanID= "MyBook"class= "Cn.bdqn.SpringAspectJ.Mybook"/> <!--2ConfigurationAOPOperation- <AOP: Config> <!--2.1Configure Pointcuts- <AOP:p ointcutexpression= "Execution (* cn.bdqn.springaspectj.book.* (..))"ID= "POINTCUT1"/> <!--2.2Configure Facets Use the reinforcement to the method above,to specify which enhancement- <AOP: Aspectref= "MyBook"> <!--Configuring enhanced Types Method:which method is used in the enhanced class as the predecessor Enhancement- <AOP: AroundMethod= "Around"Pointcut-ref= "POINTCUT1"/> </AOP: Aspect> </AOP: Config> </Beans> |
Test class
Test { @org. junit.test Test () { Classpathxmlapplicationcontext ("beans.xml"). Getbean ("book"); Book.add (); } } |
Execution effect
Mode two: The annotation method based on AspectJ
1) Introduction of the relevant Jar package
2) Create a spring core configuration file, you must import the constraints of AOP
The above two steps are directly referenced based on ASPECTJ the XML How to configure
3) Open the AOP operation in the spring configuration file
4) add @aspectj annotations to the reinforcement class
add @before to the enhanced method annotation @before is a front-mounted enhancement
5) Create the test class and run the effect as follows
Two ways to implement spring AOP using ASPECTJ