ASPECTJ Learning one: Tangent plane various implementations

Source: Internet
Author: User
Tags throwable


650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= " Https://s2.51cto.com/wyfs02/M01/A7/05/wKioL1nfMMLx0MpEAAAairfUIkU831.png-wh_500x0-wm_3-wmp_4-s_1939634227.png " Title= "2@c0) 5@86sbbcjca%m[100c.png" alt= "Wkiol1nfmmlx0mpeaaaairfuiku831.png-wh_50"/>

One: Custom annotation type

1: Custom annotation class:

@Retention (retentionpolicy.runtime) public @interface Pointcuttest {}

2:

@Aspect @component@order (0) public class pointcut {//here two ways//one://    @ Pointcut ("@annotation (application.anntation.pointcutTest)")//    public void  Pointcut () {}//II: Open the comment above and replace the following @annotation (Application.anntation.pointcutTest) with pointcut ()       @Before ("@annotation (application.anntation.pointcutTest)")     public void  Beforerunning ()  {        system.out.println ("pointcutTest1 @ The Before method starts before executing ");    }     @Around (" @annotation ( Application.anntation.pointcutTest)     //Here Note that there is a return value for  around to be added when returning the object, Otherwise it will cause the original return value not to return     public object beforeexecute (final proceedingjoinpoint  joinpoint)  throws Throwable {        Object  Obj = null;       &nbSp;try {            system.out.println (" pointcuttest1  @around method before starting execution 1 ");             System.out.println (Json.tojsonstring (Joinpoint.getargs ()));             //.proceed () Here for notification processing, will first execute the Before method, the Pointcut will enter the around method first              obj = joinpoint.proceed ();             system.out.println ("pointcuttest1   @before method executes after execution");         } catch  (exception e)  {//             logger.error ("Exception: ",  e);             system.out.println ("Method execution error");         }        return obj;    }     @After (" @annotation (application.anntation.pointcutTest) ")     public void after (final  joinpoint joinpoint)  {        try {             system.out.println ("pointcutTest1 @ After method Execution ");        } catch  (exception e)  {//             logger.error ("Abnormal:  occurred",  e);             system.out.println ("Method execution error");         }    }     @AfterReturning ("@ Annotation (application.anntation.pointcutTest) ")     public JoinPoint  AfterExecute (FINAL JOINPOINT JOINPoint)  {        try {             system.out.println ("pointcuttest1  @AfterReturning method executes after execution");         } catch  (exception e)  {//             logger.error ("Exception: ",  e);             system.out.println ("Method execution error");         }        return joinPoint;     }

Above, all methods with @pointcuttest annotations will be cut to enhance processing


Two: No annotated logo, active cutting

@Aspect @component@order (2) public class pointcuttest2 {     @Pointcut (" Execution (* application.controller.test.test ()) ")     public void pointcut () {}     @Before ("Pointcut ()")     public void beforeexecute ( Final joinpoint joinpoint)  {        try {             system.out.println ("pointcutTest2 @ before  method Start Execution ");        } catch  (Exception e)  {//            logger.error ("Exception:  occurred",  e);             system.out.println ("Method execution Error");         }    }     @Around (" Pointcut () ")     Public object beforeexecute (Final proceedingjoinpoint joinpoint)  throws  throwable {        object obj=null;         try {             System.out.println ("pointcuttest2  @around method before starting");             system.out.println ( json.tojsonstring (Joinpoint.getargs ()));             obj= joinpoint.proceed ();             system.out.println ("pointcuttest2  @Before method executes after execution");         } catch  (exception e)  {//             logger.error ("Exception: ",  e);              system.out.println ("Method execution error");        }         return obj;    }     @AfterReturning (pointcut =  "pointcut ()",returning =  "FileURL")     public  Joinpoint afterexecute (Final joinpoint joinpoint ,string fileurl)  {         try {             system.out.println (FileURL);             system.out.println (Json.tojsonstring (Joinpoint.getargs ()));             system.out.println ("pointcuttest2 afterreturning Method execution after execution");         } catch  (exception e)  {//           &nbsP; logger.error ("Exception: ",  e);             system.out.println ("Method execution error");        }         return joinpoint;    }}

Custom method, add note @pointcut ("Execution (* application.controller.test.test ())"), this annotation represents the test method inside the cut test class, when the test method is executed, It's going to be enhanced.



Three: You can use a configuration file to implement a pointcut for a project that can use a configured file
This method, like the first one above, is the configuration file form for the custom annotation method
1, <bean id= "Pointcuttest" class= "Application.anntation.pointcut"/>
specifying the Processing class
2, <aop:pointcut id= "pointcut" expression= "@annotation (application.anntation.pointcutTest)"/>
To specify custom annotations

3, <aop:aspect id= "brokeraction" order= "0" ref= "pointcuttest" >
<aop:around pointcut-ref= "Pointcut" method= "BeforeExecute"/>
</aop:aspect>
Specify the method of execution and the timing of processing

Specifies the time of execution before, around, after
Order when the same method is executed by multiple tangent cuts, the smaller the number, the first execution
METHOD specifies which one to execute


Four: Execution detailed
Example one:
<aop:pointcut id= "Servicemethod" expression= "Execution (* *). *service.* (..)) " >
The first * denotes any type of return value
The second * represents a package that starts with any name, such as com.xx
The third * denotes class names that begin with any name, such as Testservice
The fourth * means any class below the wildcard *service
The last of the ... Indicates that a wildcard method can have 0 or more parameters

Example two:
Execution (* com.aptech.jb.epet.dao.hibimpl.*.* (..))
This is all the methods that match all the classes under the Hibimpl package
The first * denotes any type of return value
The second * indicates that all of the classes
The fourth * represents all methods
The last of the ... Indicates that a wildcard method can have 0 or more parameters


When using execution, you can cut all the methods inside a class, or all the methods inside a package, or you can cut a single method, use @annotation, you can freely give the method you want to cut to identify the annotation, the two methods according to the actual scene needs to choose.


ASPECTJ Learning one: Tangent plane various implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.