[JAVA] About Spring AOP

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) *

AOP terminology

About typical terms of AOP (P.59)

Advice available in Spring

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.

Pointcut formula

How to write execution like the following source code and select the Join Point to be executed. (P.69)

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());

Select the target Join Point by method name

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


Wildcards available in Pointcut expressions

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.

Reference source


[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

About Spring AOP
About spring AOP
About Spring AOP Pointcut
About Spring ③
About binding of Spring AOP Annotation
About Spring Security authentication
About DI of Spring ①
About DI of Spring ②
Overview of Spring AOP
[Personal memo] About Spring framework
About Spring Framework context error
Introduction to Spring Boot ② ~ AOP ~
[Java] Spring AOP execution order
About =
About Spring DI related annotations
A story about BeanNotOfRequiredTypeException occurring after applying AOP in Spring
How to unit test Spring AOP
spring framework Simple study memo (2): AOP
Spring AOP for the first time
About error when implementing spring validation
About Kotlin
About attr_accessor
About Hinemos
Output system log by Spring AOP technology
spring × docker
About params
About Spring Dependency Injection using Java, Kotlin
javascript AOP
About Rails 6
About form_for
About enum
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About the initial display of Spring Framework
About encapsulation
About Docker
About active_hash
About static
About exceptions
Spring Java
How to write Spring AOP pointcut specifier
About scope
[Maven] About Maven
About designing Spring Boot and unit test environment
Summary of what I learned about Spring Boot
About the official start guide of Spring Framework
Matches annotations on the interface with Spring AOP
Spring Boot + Spring Data JPA About multiple table joins