A personal memorandum about Spring AOP. If you make a mistake, I would appreciate it if you could comment.
[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.
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. |
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
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