This is the 8th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.
👨🎓 author: Java Academic Conference
🏦 Storage: Github, Gitee
✏️ blogs: CSDN, Nuggets, InfoQ, Cloud + Community
💌 public account: Java Academic Party
🚫 special statement: the original is not easy, shall not be reproduced or copied without authorization, if you need to reproduce can contact xiaobian authorization.
🙏 Copyright notice: part of the text or pictures in the article come from the Internet and Baidu Encyclopedia, if there is infringement, please contact xiaobian as soon as possible. Wechat search public Java academic party contact xiaobian.
☠️ Daily toxic chicken soup: smile embrace every day, be like sunflower warm woman.
👋 Hello! I’m your old friend Java Academic Party. Recently xiaobian in the whole Spring family bucket notes, notes will be issued regularly every day, like the big guys welcome to collect points like attention yo. Xiaobian will share it every day. The Spring framework is not limited to server-side development. Any Java application can benefit from Spring in terms of simplicity, testability, and loose coupling. The Spring framework is also a super glue platform, providing the ability to glue other technologies and frameworks in addition to its own capabilities.
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.
AspectJ framework
1.1 introduce AspectJ
- 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 is an excellent section-oriented framework that extends the Java language to provide powerful sectioning implementations. AspectJ is an open source project of the Eclipse Foundation
- 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
1.2 AspectJ implements AOP in two ways
- Configuration files using XML: Configure global transactions
- Use annotations, which are used in real development. AspectJ has five annotations.
1.3 How to use AspectJ framework
-
Section execution time: This execution time is called Advice in the specification. Use annotations in the aspectJ framework. You can also use tags in XML configuration files.
- @Before (Pre-notification)
- @afterreturning (Post notification)
- @around (Circular notification)
- @afterThrowing
- @After (Final notice)
The above five notes indicate when the section is executed.
-
Where the aspect is executed: Use pointcut expressions
1.4 Notification Types in AspectJ (Understanding)
There are five types commonly used in AspectJ:
- Pre-notification (@before)
- Post notification (@afterreturning)
- Circular notification (@around)
- Exception Notification (@afterThrowing)
- Final Notice (@after)
1.5 Pointcut Expression syntax (specifying the location of pointcuts)
AspectJ defines special expressions for specifying pointcuts. The prototype of the expression is:
// Use Spaces between arguments to separate execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) // Write the type of the parameter, not the parameter value. The package name returned by the execution permission method. Class name. Method name (method parameter type) Exception type.Copy the code
Explain AspectJ parameter information
- Modifiers -pattern: Access permission type
- Ret-type-pattern: indicates the return value type
- Literal -type-pattern: package name Class name
- Name-pattern (param-pattern) : method name (parameter type and parameter number)
- Throws -pattern: Throws the exception type
Note:? Represents optional parts. That is, there are no bold parameters.
The object that the pointcut expression matches is the method name of the target method. Therefore, an execution expression is clearly a signature of a method.
1.6 Pointcut expressions use co-cards
Wildcards can be used in AspectJ for the purpose of obtaining multiple target objects using a single annotation in a configuration file, and then adding uniform functionality to those target objects or supplementingother functionality.
(Only the return value type and method name (parameter) parameters can not be omitted, where the simplified tangent point expression must exist in the information of these two parameters)
- execution(public * *(..) ) : Specifies the location of the pointcut, any public method.
- execution(* set*(…) ) : Specifies the position of the pointcut, any method that begins with “set”.
- execution(* com.yunbocheng.service.* .*(..) ) : Specifies that the pointcut location is any method in any class in the service package.
- execution(* com.yunbocheng.service..* .(..) ) : Specifies that the pointcut location is any method of any class in the service package or subpackage. ..” When appearing in a class name, it must be followed by”“, represents all classes under the package, subpackage.
- execution(* ..service. * .(..) ) : Specifies the pointcut location: all methods in all classes (interfaces) under the service subpackage of the multilevel package are pointcuts.
- execution(* .service. * .(..) ) : Specifies the pointcut location: all methods in all classes (interfaces) in the service subpackage of the first-level package are pointcuts.
- Execution (* joke(String,*)) : Specifies the Joke () method in all packages, whose first argument is String and whose second argument can be of any type. Example: Joke (String s1,String s2), joke(String s1,double d)
- execution(* joke(String,..) ) : 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).
AspectJ annotation-based AOP implementation (Master)
- When AspectJ implements AOP, AOP’s constraints are introduced. The tags in the AOP constraints used in the configuration file are those used by the AspectJ framework, not by the Spring framework itself when implementing AOP.
- AspectJ implements AOP in two ways: annotations and configuration files.
Step 1: Define the business interface and implementation classes
Step 2: Define the aspect class
Class defines several common methods that will be used as different notification methods to enhance functionality.
Step 3: Declare the target object and the aspect class object in the Spring configuration file (ApplicationContext.xml)
Step 4: Register AspectJ’s automatic proxy in spring’s configuration file (ApplicationContext.xml)
- 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.
Step 5: Use the ID of the target object in the test class to locate
Several ways to use notifications
3.1 @before – The method has JoinPoint parameters
- 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.
- Pre-notification core code, the rest of the code is the same as above.
AfterReturning – There are 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.
Add the ProceedingJoinPoint parameter
- Execute before and after the target method executes. Methods annotated as surround enhanced should have a return value of type Object. The parallel 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.
### throwing – An annotation with 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.
###3.5 [understanding]@After final notification
- This enhancement is executed whether or not the target method throws an exception.
Today, I will share my bucket notes with you tomorrow.
For the source code of the above project, click planet for free accessplanet(Github address) If you don’t have a Github partner. You can follow my wechat official account:Java academic lie prone, send Spring, free to send you the project source code, the code is personally tested by xiaobian, absolutely reliable. It’s free to use.