Spring slicing aspect

Source: Internet
Author: User

1.controller Method:

@Aspect @Component Public classTimeaspect { @Around ( "Execution (* com.sea.web.controller.usercontroller.* (..))")     PublicObject Handlecontrollermethod (proceedingjoinpoint pjp) throws Throwable {System. out. println ("Time aspect start"); //Description: Proceedingjoinpoint can get the value of the passed-in parameter of the called Method//such as: HTTP:LOCALHOST:8080/USER/1//Comtroller Method: Public User getInfo (@PathVarable String ID)       object[] args = Pjp.getargs ();  for(Object Arg:args) {System. out. println ("Arg is"+ arg);//ID 1        }        LongStart =NewDate (). GetTime (); //This object is the return value of the bar method, such as: User user= usercontroller.finduser (), // The method is equivalent to the filter in the Dofilter () method of the object Object =        pjp.proceed (); System. out. println ("time aspect:"+ (NewDate (). GetTime ()-start)); System. out. println ("Time aspect End"); return Object; }}

@Around: Contains the following three kinds of

before Advice

Treatment prior to substitution

After Advice

Processing after a call

Throw Advice

Exception handling thrown at the time of the call

Execution com.sea.web.controller.UserController. * (..))
execution : Indicates execution
First *: return value ; This means that whatever the return value is.
Com.sea.web.controller.UserController: denotes that class
Second *: Presentation method: All Methods under Class
*(..) : All methods, methods of any parameters are executed

anotation Annotation Rules:

@Aspect the class that represents the annotation is an abstract service method module;

@Pointcut define the usage rules for the service, which methods the service methods are applied to in the target class.

@Pointcut ("Execution (*add* (..))") The first * indicates whether or not there are return values, all methods that begin with add, regardless of the parameters.

Private Voidaddaddmethod () {}; This method is only an empty method body in the annotation pattern, is a modal method definition structure, the method cannot have a return value, cannot have any parameters, and cannot implement it. The identity of the method name to use in advice.

Advice annotation checksecurity () method, indicating that the method is a specific service method, using annotations, advice does not appear, but using the above mentioned in the theoretical part of the use of @after, @Befor, @Throw to represent, The rules defined in the Pointcut are also associated in advice.

Spring slicing aspect

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.