[JAVA] About spring AOP

A personal memorandum about Spring AOP. If you make a mistake, I would appreciate it if you could comment.

reference

[Thorough introduction to Spring](https://www.amazon.co.jp/Spring%E5%BE%B9%E5%BA%95%E5%85%A5%E9%96%80-Spring-Framework%E3% 81% AB% E3% 82% 88% E3% 82% 8BJava% E3% 82% A2% E3% 83% 97% E3% 83% AA% E3% 82% B1% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3% E9% 96% 8B% E7% 99% BA-% E6% A0% AA% E5% BC% 8F% E4% BC% 9A% E7% A4% BENTT % E3% 83% 87% E3% 83% BC% E3% 82% BF / dp / 4798142476 / ref = sr_1_3? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & keywords = spring & qid = 1566716142 & s = gateway & sr = 8-3) Spring AOP For JoinPoint (timing before or after execution of the specified method), you can insert the process implemented in the class with @Aspect annotation.

Available Advice annotations

Annotation Description
@Before Processing is inserted before execution of the target method.
@AfterReturning Processing is inserted after the target method ends normally. You can also get the return value of the method at the time of normal termination.
@AfterThrowing Target method terminates abnormally(When an exception is thrown)Processing is inserted in. You can also get an exception at the time of abnormal termination.
@After Processing is inserted at the end regardless of whether the target method ends normally or throws an exception.
@Around The strongest advice. Processing is inserted before and after the target method.

Implementation

Before

SampleAspect.java


package com.example.demo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SampleAspect {
    @Before("execution(public * com.example.demo.controller.*.*(..))")
    public void startLog(JoinPoint jp) {
        System.out.println("Method start:" + jp.getSignature());
    }
}

After Returning

SampleAspect.java


package com.example.demo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import com.example.demo.controller.resorces.UserResponse;

@Aspect
@Component
public class SampleAspect {
    @AfterReturning(value = "execution(public * com.example.demo.controller.*.*(..))", returning = "user")
    public void endLog(JoinPoint jp, UserResponse user) {
        System.out.println("Method completed successfully:" + jp.getSignature());
        System.out.println("Return value:" + user);
    }
}

After Throwing

SampleAspect.java


package com.example.demo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SampleAspect {
    @AfterThrowing(value = "execution(public * com.example.demo.controller.*.*(..))", throwing = "e")
    public void endLog(JoinPoint jp, RuntimeException e) {
        System.out.println("Method abend:" + jp.getSignature());
        e.printStackTrace();
    }
}

After

SampleAspect.java


package com.example.demo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SampleAspect {
    @After("execution(public * com.example.demo.controller.*.*(..))")
    public void endLog(JoinPoint jp) {
        System.out.println("Method end:" + jp.getSignature());
    }
}

Around

SampleAspect.java


package com.example.demo;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SampleAspect {
    @Around("execution(public * com.example.demo.controller.*.*(..))")
    public Object log(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("Method start:" + jp.getSignature());
        Object result;

        try {
            //Target method execution
            result = jp.proceed();
            System.out.println("Method end:" + jp.getSignature());
            System.out.println("Return value:" + result);
            return result;
        } catch (Throwable e) {
            System.out.println("Method abend:" + jp.getSignature());
            e.printStackTrace();
            throw e;
        }
    }
}

Pointcut

Named Pointcut

You can also name the pointcut and reuse it.

SampleAspect.java


package com.example.demo;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SampleAspect {
    @Pointcut("execution(public * com.example.demo.controller.*.*(..))")
    public void demoController() {}

    @Before("demoController()")
    public void startLog(JoinPoint jp) {
        System.out.println("Method start:" + jp.getSignature());
    }
}

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 ①
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 method.invoke
About Kotlin
About attr_accessor
About Hinemos
Output system log by Spring AOP technology
About inheritance
spring × docker
About params
About Docker
About Spring Dependency Injection using Java, Kotlin
javascript AOP
About form_for
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 JAVA_HOME
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