Define entry point

Source: Internet
Author: User

In the previous article, four enhancements are defined in a face-cutting class, and the same entry-point expressions are specified when four enhancements are defined, this is obviously not in line with the software design principle: we repeat that entry point expression four times. If one day we need to modify this entry point expression, we need to modify four places. What if I repeat it more times? We have to modify it more times.

To solve this problem, both aspectj and spring allow defining the entry point. The so-calledDefine entry pointThe essence is to create a name for an entry point expression, so that this name can be reused in multiple enhancements.

Spring AOPOnly the execution group of spring bean method can be used as the connection point.Entry PointIt is regarded as all bean methods that can match the cut-in expression.

Entry Point DefinitionThere are two parts:

① An entry point expression:Specifies the method to match the entry point.

② A method signature containing the name and any parameters:The name of the entry.

In @ aspectj-style AOP,Entry Point signatureUse a common method definition (the method body is usually empty), and the return value of the method must be void;Entry Point expressionAnnotation using @ pointcut is required.

The following code snippet defines an entry point anyoldtransfer, which matches the execution of any method named transfer:

// Specify the entry point expression @ pointcut ("execution (* Transfer (...) When @ pointcut is used for annotation (..)) ") // use a method with the return value void and the method body being null to name the entry point private void anyoldtransfer (){}

Entry Point expression, That is, the value of the @ pointcut annotation, which is a regular aspectj5 entry point expression. For more information about the aspectj entry point syntax, see the aspectj programming guide.

Once the anyoldtransfer entry point is defined using the code snippet above, the program can reuse the entry point multiple times, you can even use this entry point in other partition classes or other package partition classes. As to whether this entry point can be accessed in the partition classes of other partition classes or other packages, it depends on the access control character before the method signature ------ for example, in this example, the anyoldtransfer method uses the private modifier, which means that the entry point can only be used in the current face-cutting class.

If you need to use the entry point in this cut-in class, you can specify the value attribute value as an existing entry point when using @ pointcut, as shown below:

@AfterReturning(pointcut="myPointcut()",returning="retVal")public void writeLog(String msg,Object retVal){   ...}

It can be seen that when specifying a starting point, it is very similar to calling the Java method syntax ------ this method only represents a starting point, and its essence is to define a starting point expression for the enhancement processing.

If you want to use the entry points in other face-cutting classes, the entry points in other face-cutting classes cannot be modified using private. Only one entry point is defined in the partition class of the following program:

@Aspectpublic class SystemArchitecture{   @Pointcut("execution(* org.crazyit.app.service.impl.Chin*.say*(..))")   public void myPointcut(){   }}

The mypointcut entry point defined above will be used directly in the following Partition class:

@ Aspectpublic class logaspect {@ afterreturning (pointcut = "systemarchitecture. mypointcut () & ARGs (MSG) ", returning =" retval ") Public void writelog (string MSG, object retval) {system. out. println (MSG); system. out. println (retval); system. out. println ("analog log... ");}}

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.