This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

AspectJ’s implementation of AOP

AOP is a programming idea that has been implemented in many frameworks. Spring, for example, can do faceted programming. However, AspectJ also implements the functionality of AOP in a simpler, more user-friendly way, and supports annotated development. As a result, Spring has incorporated AspectJ’s implementation of AOP into its framework.

When developing with AOP in Spring, AspectJ’s implementation is generally used.

AspectJ profile

AspectJ is a section-oriented framework that extends the Java language. AspectJ defines the AOP syntax, which has a specialized compiler for generating Class files that comply with the Java byte-encoding specification.

Official website: www.eclipse.org/aspectj/

AspetJ is an open source project of Eclipse.

A Seamless aspect-oriented extension to the Javatm Programming Language

Java Platform Compatible (Compatible with the Java platform and can be seamlessly extended)

Easy to learn and use

The notification type of AspectJ

There are five types of advice commonly used in AspectJ:

● Advance notification

● Post notification

● Surround notification

● Exception notification

● Final notice

AspectJ pointcut expression

AspectJ defines special expressions for specifying pointcuts. The prototype of the expression is:

Execution ([modifiers-pattern] Access type REt-type-pattern Return value type [variable type-pattern] Full name of the class literal Name-pattern (param-pattern) Method name (parameter name) [throws an exception type)Copy the code

The object that the pointcut expression matches is the method name of the target method. So, an execution expression is clearly a signature of a method. Note that the part with [] in the expression indicates the part that can be omitted, separated by Spaces. Here you can use the following symbols:

For example:

Low execution (public * * (..) )

Specify the pointcut as: any public method.

Low execution (* set * (..) )

Specify pointcuts as: any method that begins with “set”.

Low execution (* com. Xyz. Service.. (..) )

Specify pointcuts as: any method defined in any class in the service package.

Low execution (* com. Xyz. Service… (..) )

Specify pointcuts that define any method of any class in a service package or subpackage. “..” When it appears in the class name, it must be followed by an asterisk (*) to indicate all classes under the package or subpackage.

Low execution (*. Service.. * (..) )

Specify that only all methods in all classes (interfaces) in serivce subpackages under a level 1 package are pointcuts

Low execution (*.. service.. * (..) )

Specify all methods in all classes (interfaces) under serivce subpackages under all packages as pointcuts

Low execution (*. ISomeService. (..) )

Specify that only all methods in the ISomeSerivce interface under a level 1 package are pointcuts

Low execution (*.. ISomeService.(..) )

Specify all methods in the ISomeSerivce interface under all packages as pointcuts

Low execution (* com. Xyz. Service. IAccountService. * (..) )

Specify the pointcut as any method in the IAccountService interface.

Low execution (* com. Xyz. Service. IAccountService +. * (..) )

If IAccountService is an interface, it is any method in the interface and any method in all its implementation classes. Class, any method in the class and its subclasses.

Low execution (* joke (String, int)))

Specify pointcuts for all Joke (String,int) methods, with joke() taking the first argument to String and the second argument to int. If the parameter type in the method is a class under the java.lang package, you can use the class name directly, otherwise you must use the fully qualified class name, such as Joke (java.util.list, int).

Low execution (* joke (String, *)))

Specify pointcuts as: All joke() methods, whose first parameter is String and whose second parameter can be of any type, such as Joke (String s1,String s2) and Joke (String s1,double d2), But Joke (String S1,double d2,Strings3) is not.

Low execution (* joke (String) that… ))

Specify pointcuts as: All joke() methods, which take String as their first argument and can be followed by any parameter of any type, Examples include Joke (String s1), Joke (String s1,String S2) and Joke (String s1,double d2,String S3).

Low execution (* joke (Object))

Specify the pointcut as: all joke() methods, which have one parameter of type Object. Joke (Object OB) is, but Joke (String s) and Joke (User U) are not.

Low execution (* joke (Object +)))

Specify the pointcut as: all joke() methods, which take one parameter that is of type Object or a subclass of the class. Not only joke(Object OB), but also Joke (String S) and Joke (User U).

AspectJ development environment

1. Import three Jar packages

● Spring framework AOP implementation JAR package: spring-aop.jar

● AspectJ framework for AOP implementation jar package: AspectJrt. Jar, AspectJweaver. Jar

● Spring’s implementation of AOP is available in the Spring framework package: spring-aOP-4.3.9.release.jar

AspectJ jars can be downloaded from the AspectJ website.

www.eclipse.org/aspectj/dow…

Download aspectj-1.9.0.jar, unzip it to:

2. Introduce AOP constraints

In the header of the configuration file, you introduce constraints about AOP. In the xsD-configuration. HTML file under \docs\spring-framework-reference\ HTML in the unzip directory of the Spring framework.

AOP’s constraints were not introduced earlier when Spring implemented AOP, and were only introduced when AspectJ implemented AOP. Note that the tags in the AOP constraints used in the configuration file are those used by the AspectJ framework, not those used by the Spring framework itself when implementing AOP.

AspectJ implements AOP in two ways: annotations and configuration files.

AspectJ annotation-based AOP implementation

AspectJ provides an annotated implementation of AOP.

1. Implementation steps

Step1: define business interfaces and implementation classes

Step2: define the section POJO class

This is a POJO class that will appear as an aspect. There are several common methods defined that will serve as different notification methods.

Step3: add the @aspect annotation to the Aspect class

Add the @aspect annotation to the defined POJO class to specify that the current POJO class will be the Aspect.

 

Step4: add notification annotations to the normal methods of the POJO class

The aspect class is used to define the enhancement code, that is, the enhancement method used to define the target method in the enhancement target class. These enhancements use different “notification” annotations that finish weaving at different points in time. Of course, for enhanced code, an execution expression also specifies the target classes and target methods, known as pointcuts, for a specific application.

Step5: register the target object and POJO section class

Step6: register AspectJ’s automatic proxy

Once the Aspect has been defined, you need to tell the Spring container to generate a proxy object for the “target class + Aspect”. This proxy is automatically generated by the container. All you need to do is register an AspectJ-based automatic proxy generator in your Spring configuration file, and it will automatically scan for the @Aspect annotation, weave it in by notification type and pointcut, and generate the proxy.

The underlying is implemented by AnnotationAwareAspectJAutoProxyCreator.

As the name of its class indicates, it is an AspectJ-based annotation adaptation automatic proxy generator.

It works by scanning to find the Aspect class defined by @Aspect, which then finds the target method of the target class based on the pointcut, and then finds the point in time of the cut based on the notification type.

Step7: use the id of the target object in the test class

Example: annotation package for the aop_AspectJ project

2. @before – Method has JoinPoint parameter

Execute before the target method executes. Methods annotated as pre-notification can contain a JoinPoint type parameter. Objects of this type are themselves pointcut expressions. With this parameter, you can obtain pointcut expressions, method signatures, target objects, and so on.

Not only the pre-notification method can contain a JoinPoint type parameter, but all notification methods can contain this parameter.

3. @afterRETURNING – Annotations have RETURNING attributes

Execute after the target method executes. Since it is executed after the target method, the return value of the target method can be retrieved. The RETURNING attribute of the annotation is the name of the variable used to specify the value returned by the receiving method. So, a method annotated as a post-notification can contain, in addition to the JoinPoint argument, a variable used to receive the return value. This variable is best of type Object, because the return value of the target method can be of any type.

4, @around surround notification – enhancement methods ProceedingJoinPoint parameter

Execute before and after the target method executes. Methods annotated as surround enhanced should have a return value of type Object. And the method can contain a parameter of type ProceedingJoinPoint. The ProceedingJoinPoint interface has a proceed() method that executes the target method. If the target method has a return value, that method’s return value is the target method’s return value. Finally, the wrap enhancement method returns its return value. The enhanced method actually intercepts the execution of the target method.

 

5. @afterThrowing Exception notification – An annotation has the Throwing attribute

Executes after the target method throws an exception. The Throwing attribute of the annotation is used to specify the exception class object that occurred.

Of course, methods annotated as exception notifications can include a parameter Throwable, named as Throwing, which represents the exception object that occurred.

6. @After Final notice

This enhancement is executed whether or not the target method throws an exception.

7. @pointcut defines pointcuts

When more notification enhancement methods use the same execution pointcut expression, it can be cumbersome to write and maintain. AspectJ provides the @Pointcut annotation to define an execution Pointcut expression.

This is done by annotating @pointcut over a method that can be used as a Pointcut for all future executeion value property values. Represents the Pointcut defined by @pointcut. This method annotated with @pointcute generally uses the private identification method, which has no real effect.