AOP is an abbreviation for Aspect Oriented Programming, which is a programming method that designs and implements with a focus on cross-cutting concerns scattered in multiple classes.
[Thorough introduction to Spring Java application development with Spring Framework](https://www.amazon.co.jp/Spring thorough introduction-Java application development with Spring-Framework-NTT Data Co., Ltd./dp/4798142476/) I will summarize the important points about the explanation of AOP. * (The number of pages in the book is listed so that you can look back at it later) *
About typical terms of AOP (P.59)
Aspect The module itself that shows the cross-cutting interests that are the unit of AOP. For example, "output log", "handle exceptions", "manage transactions", etc. are called Aspect.
Join Point The point at which Aspect is executed (when executing a method, throwing an exception, etc.). Join Point in Spring AOP is at the time of method execution.
Advice Code that runs at a particular Join Point. Where to implement cross-cutting concerns. There are multiple types such as Around, Before, and After.
Pointcut An expression (expression) that selects the Join Point to be executed. It can be regarded as a group of Join Points. In Spring AOP, Pointcut is defined by Bean definition and annotation. For example, it decides to execute only in the class named XXController, or to execute only in the case of the method name starting with find. You can specify a wild card.
Weaving The process of inserting Aspect at the appropriate point in the application code. Weaving is performed at runtime in Spring AOP.
Target An object whose processing flow has been changed by AOP processing. Sometimes called an Adviced object.
The following 5 Advices are available in Spring AOP. (P.60)
Advice | Overview |
---|---|
Before | Advice that runs before Join Point. |
After Returning | Advice that is executed after the Join Point ends normally. If an exception is thrown, it will not be executed. |
After Throwing | Advice that is executed after an exception is thrown at the Join Point. If it ends normally, it will not be executed. |
After | Advice that runs after Join Point. It is executed regardless of normal termination or exception. |
Around | Advice that runs before and after the Join Point. |
How to write execution like the following source code and select the Join Point to be executed. (P.69)
@Aspect
@Component
public class MethodStartLogAspect {
// *Example of Advice to be executed by any method with the class name Controller
@Before("execution(* *..*Controller.*(..))")
public void startLog(JoinPoint jp) {
System.out.println("Method start:" + jp.getSignature());
}
}
When expressing the target Join Point by specifying the method name pattern, use the execution directive.
execution(* com.example.domain.*Service.find*(..))
/*
In the above example
The return value is*
Package name is com.example.domain
Type, class name*Service
Method name is find*
Arguments of any 0 or more
Represents.
*/
The wildcards that can be used in the Pointcut expression are: (P.70)
Wildcard | Description |
---|---|
* | Basically, it represents an arbitrary character string, but when expressing a package, it represents an arbitrary package 1 layer. When expressing a method argument, it represents one number of arguments. |
.. | Arbitrary when representing a package(0 or more)Represents a package. Arbitrary when expressing method arguments(0 or more)Represents a number argument. |
+ | By specifying after the class name, it represents the class and all its subclasses / implementation classes. |
[Thorough introduction to Spring Java application development using Spring Framework](https://www.amazon.co.jp/Thorough introduction to Spring-Java application development using Spring-Framework-NTT Data Co., Ltd./dp/4798142476/)
Recommended Posts