In the previous article we talked about the XML implementation of spring, and here we talk about how to implement AOP using annotations. We have already talked about the simple understanding of AOP, which is not mentioned here.
Annotated way to implement AOP we are mainly divided into the following steps (self-organized, there is a better way, welcome to exchange [email protected]):
1. It is declared as a slice class before the slice class (the class to which the Pointcut is served) is decorated with @aspect annotations.
2. Use the @pointcut annotation to declare a tangent point in order to tell the slice, who is its service object. (The method body for this annotation adornment is empty and does not require write functionality such as public void Say () {}; It is possible that the method name can be used as a reference to the specific service function of the target, which can be understood as a proxy object method of the Tangent object)
3. Before the corresponding method with the corresponding notification type annotation decoration, the corresponding method is declared a slice function, in order to point out the service
4. Open the AOP annotations automatic proxy in the spring configuration file. such as:<aop:aspectj-autoproxy/>
This may still be very abstract, then, nonsense said, our code to speak, the code is as follows:
Knight Category: (read the last article on the know what the knight is, Hey Hey)
1 PackageCom.cjh.aop2;2 3 Importorg.springframework.stereotype.Component;4 5 /**6 * @authorCaijh7 *8 * July 11, 2017 afternoon 3:53:199 */Ten@Component ("Knight") One Public classBraveknight { A Public voidsaying () { -System.out.println ("I Am a knight. (Tangent method) "); - } the}
Slice class: (The note is mainly reflected here)
1 PackageCom.cjh.aop2;2 3 ImportOrg.aspectj.lang.ProceedingJoinPoint;4 ImportOrg.aspectj.lang.annotation.After;5 ImportOrg.aspectj.lang.annotation.Around;6 ImportOrg.aspectj.lang.annotation.Aspect;7 ImportOrg.aspectj.lang.annotation.Before;8 ImportOrg.aspectj.lang.annotation.Pointcut;9 Importorg.springframework.stereotype.Component;Ten One /** A * @authorCaijh - * Email:[email protected] - * July 12, 2017 morning 9:31:43 the */ - /** - * Annotated way to declare AOP - * 1. Use @aspect annotations to declare a class as a slice (if you annotate a bean object with a @component ("") annotation, you must turn on the annotation scan in the spring configuration file <context:component-scan Base-package= "Com.cjh.aop2"/> + * Otherwise declare a Bean object in the Spring configuration file) - * 2. Add the appropriate comment before the slice needs to implement the corresponding method, that is, the notification type. + * 3. There is a surround notification, the surround notification method must have Proceedingjoinpoint type parameters passed in, and then execute the corresponding proceed () method, wrapping can be achieved. A */ at@Component ("Annotationtest") - @Aspect - Public classAnnotationtest { - //Defining Pointcuts -@Pointcut ("Execution (* *.saying (..))") - Public voidsayings () {} in /** - * Pre-notification (the Sayings () method in the annotation is actually the method name that defines the pointcut pointcut annotation, which is just a proxy object, does not need to write a specific method, to * Equivalent to the ID name of the XML declaration slice, as below, equivalent to id= "embark", for other notification type reference) + * <aop:config> - <aop:aspect ref= "Mistrel" > the <!--defining pointcuts -- * <aop:pointcut expression= "Execution (* *.saying (..))" id= " embark"/> $ <!--declaring a pre-notification (called before the Tangency method is executed)-Panax Notoginseng <aop:before method= "Beforsay" pointcut-ref= "Embark"/> - <!--declaring a post-notification (called after the tangent method is executed)- the <aop:after method= "Aftersay" pointcut-ref= "Embark"/> + </aop:aspect> A </aop:config> the */ +@Before ("Sayings ()") - Public voidSayHello () { $System.out.println ("Note type pre-notification"); $ } - //Post Notification -@After ("Sayings ()") the Public voidSaygoodbey () { -System.out.println ("Note type post notification");Wuyi } the //surround notifications. Note that you want to have the Proceedingjoinpoint parameter passed in. -@Around ("Sayings ()") Wu Public voidSayaround (Proceedingjoinpoint PJP)throwsthrowable{ -System.out.println ("Annotation type wrapping Notification: Surround Front "); AboutPjp.proceed ();//Execution Method $System.out.println ("Annotation type wrapping Notification: Surround "); - } -}
Spring configuration file:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 Xmlns:context= "Http://www.springframework.org/schema/context"5 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"6 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 8 Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.3.xsd ">9 <!--Turn on annotation scanning -Ten <Context:component-scanBase-package= "Com.cjh.aop2"/> One <!--Open the AOP annotation method, this step s must not be less so that the AOP annotations in the Java class will take effect - A <Aop:aspectj-autoproxy/> - </Beans>
Test code:
1 PackageCom.cjh.aop2;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 /**7 * 8 * @authorCaijh9 * Email:[email protected]Ten * July 11, 2017 afternoon 6:27:06 One */ A Public classTest { - Public Static voidMain (string[] args) { -ApplicationContext AC =NewClasspathxmlapplicationcontext ("Com/cjh/aop2/beans.xml"); theBraveknight br = (braveknight) Ac.getbean ("Knight"); - br.saying (); - } -}
Operation Result:
annotation type Wrapping Notification: Surround Front
Note Type pre-notification
I'm a knight. (Tangent method)
annotation type Wrapping Notification: After wrapping
Note Type post-placement notification
======================== Split Line ===================================
Because of the use of annotations, so the configuration file a lot less content, only need a sentence <context:component-scan base-package= "Com.cjh.aop2"/> declares the package to be scanned, and the framework automatically scans for comments and generates bean objects. There is a @component ("Knight") this annotation, and <bean id= "Knight" class= "Com.cjh.aop2.BraveKnight"/> This configuration, the same meaning, The framework automatically recognizes and creates a Braveknight object named Knight. So with annotations, you just need to turn on the annotation scan configuration, no need to do the same bean configuration.
If spring Aop:error at:: 0 can ' t ' find referenced pointcut sleepponit errors during the run, then it is likely that the version of the spring package is a problem,
I use the Spring4 version, then also need to add Aspectjrt-1.7.4.jar and Aspectjweaver-1.7.4.jar two packs, Cloud Disk Address: Link: http://pan.baidu.com/s/1qXQurO4 Password: nc4i
The project catalogue is as follows: (The class of code-playing no tube, just to not mislead everyone, so crossed off)
The notification annotation type is as follows:
A simple example of how AOP is implemented in spring in annotations