[Spring] AOP ์ ๋ฆฌ
๐ ์ฉ์ด ์ ๋ฆฌ
1) Aspect: ์์ฃผ์ฐ๋ ์ฝ๋๋ค์ ๋ชจ๋ํํ๊ฒ
2) Target: Aspect๋ฅผ ์ ์ฉํ๋๊ณณ (class, method...)
3) Advice: ์ค์ง์ ์ธ ๋ถ๊ฐ๊ธฐ๋ฅ์ ๋ด์ ๊ตฌํ์ฒด
4) JointPoint: Advice๊ฐ ์ ์ฉ๋ ์์น
4-1) ProceedingJoinPoint: joinPoint๋ฅผ ์์๋ฐ๋ ๊ฐ์ฒด, Around Advice์์๋ง ์ง์๋จ
5) PointCut: JointPoint์ ์์ธํ ๋ด์ฉ์ ์ ์ํ๊ฒ
AOP๋
- ์ด๋ค ๋ก์ง์ ๊ธฐ์ค์ผ๋ก ๊ด์ ์ ๋๋์ด์ ๋ณด๊ณ ๊ทธ ๊ด์ ์ ๋ชจ๋ํ ์ํค๋ ํ๋ก๊ทธ๋๋ฐ
- ์์ค์ฝ๋์์์ ์์ฃผ ๋ฐ๋ณต๋๋ ์ฝ๋๋ค์ Aspect๋ก ๋ชจ๋ํํ์ฌ ์ฌ์ฌ์ฉํ๋๊ฒ ๋ชฉ์
Spring AOP
- ํ๋ก์ ํจํด ๊ธฐ๋ฐ์ AOP ์ ๊ทผ์ ์ด ๋ฐ ๋ถ๊ฐ๊ธฐ๋ฅ ์ถ๊ฐํ๊ธฐ ์ํด ์ฌ์ฉ
- ์คํ๋ง ๋น์๋ง AOP ์ ์ฉ๊ฐ๋ฅ
์ฌ์ฉ์์
1. dependency ์ถ๊ฐ
-- maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
-- gradle
implementation 'org.springframework.boot:spring-boot-starter-aop'
2. ์ด๋ ธํ ์ด์ ์ถ๊ฐ
@Aspect
@Component
@Order(200)
public class LoggingAop {
@Before("execution (* com.test.web..*Controller.*(..)) && @annotation(AccessLogging)")
public void webControllerBeforeLoggingAop(final JoinPoint joinPoint) {
final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
Optional.ofNullable(requestAttributes).ifPresent(ra -> { //์ ๊ทผ๋ก๊ทธ ์ ์ฌ .... });
}
}
๐ ์ถ๊ฐ ์ ๋ฆฌ
@Aspect: ์ด ํด๋์ค๊ฐ Aspect๋ฅผ ๋ํ๋ด๋ ํด๋์ค๋ผ๋ ๊ฒ์ ๋ช ์
@Component: ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก
@Order: AOP ์์ ์ค์ , ๊ฐ์ด ์์์๋ก ์ฐ์ ์์๊ฐ ๋์ ์์ชฝ์ผ๋ก ์ ๋ ฌ๋๋ค
@Before: ์ง์ ํ ํจํด์ ํด๋นํ๋ ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ์ ์ interceptor์ ๊ฐ์ด ๋์ (๋ค๋ฅธ ์๋ก @After, @Around ๋ฑ์ด ์๋ค)
execution: ํน์ ๋ฉ์๋๋ฅผ ์ง์ ํ๋ ํจํด๋ฐฉ์
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface AccessLogging {
}
๐ ์ถ๊ฐ ์ ๋ฆฌ
@Target : ์ด๋ ธํ ์ด์ ์ด ๋ถ๋ ํน์ ํ์ ์ ๊ฐ์ฒด๋ฅผ ์ง์
- ๋ฐํ์๋ ๊ฐ์ฒด๊ฐ ์ผ์นํ๋์ง ํ์ธํ๋ฏ๋ก @Retension์ RUNTIME ์ด์ด์ผ ํจ
@Retention : ์ด๋ ์์ ๊น์ง ์ด๋ ธํ ์ด์ ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ์ ธ๊ฐ์ง ์ค์ (SOURCE, CLASS, RUNTIME)
@AfterReturning("processAop()")
public void checkAccessLog(final JoinPoint joinPoint) throws RuntimeException
{
//ํด๋์ค๋ช
final String classNm = joinPoint.getTarget()
.getClass()
.getSimpleName();
//๋ฉ์๋๋ช
final String methodNm = joinPoint.getSignature()
.getName();
//arguments
final Object[] args = joinPoint.getArgs();
}
๐ ์ถ๊ฐ ์ ๋ฆฌ
@AfterReturning: ํ๊ฒ ๋ฉ์๋๊ฐ ์ฑ๊ณต์ ์ผ๋ก ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ์ ์ด๋๋ฐ์ด์ค ๊ธฐ๋ฅ์ ์ํํ๋ค
์ฐธ์กฐ: https://engkimbs.tistory.com/746
[Spring] ์คํ๋ง AOP (Spring AOP) ์ด์ ๋ฆฌ : ๊ฐ๋ , ํ๋ก์ ๊ธฐ๋ฐ AOP, @AOP
| ์คํ๋ง AOP ( Aspect Oriented Programming ) AOP๋ Aspect Oriented Programming์ ์ฝ์๋ก ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ด๋ผ๊ณ ๋ถ๋ฆฐ๋ค. ๊ด์ ์งํฅ์ ์ฝ๊ฒ ๋งํด ์ด๋ค ๋ก์ง์ ๊ธฐ์ค์ผ๋ก ํต์ฌ์ ์ธ ๊ด์ , ๋ถ๊ฐ์ ์ธ ๊ด์ ์ผ๋ก
engkimbs.tistory.com
https://sas-study.tistory.com/329
[Java] @Retention ์ด๋ ธํ ์ด์ ๊น๋ณด๊ธฐ(RetentionPolicy / Source, Class, Runtime)
์๋ ํ์ธ์. ์ค๋์ ์ปค์คํ ์ด๋ ธํ ์ด์ ์ ์์ฑํ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ ์ด๋ ธํ ์ด์ ์ธ @Retention์ด๋ ธํ ์ด์ ์ ๋ํด์ ๋ค๋ฃจ์ด๋ณด๊ฒ ์ต๋๋ค. ๋ณดํต ์ด๋ ธํ ์ด์ ์ ๋ค์๊ณผ ๊ฐ์ด ์ ์ธ๋ฉ๋๋ค. @Retention(RetentionPo
sas-study.tistory.com
https://seeminglyjs.tistory.com/249
[Java] ์๋ฐ ์ด๋ ธํ ์ด์ Annotation @Target ์์๋ณด๊ธฐ.
2021-02-05 ์ด๋ ธํ ์ด์ ์ ์ ์ธ ์ค @Target์ด๋ผ๋ ๊ธฐ๋ฅ์ด ์๋ค. ์ด๋ ํด๋น ์ฌ์ฉ์๊ฐ ๋ง๋ ์ด๋ ธํ ์ด์ ์ด ๋ถ์ฐฉ๋ ์ ์๋ ํ์ ์ ์ง์ ํ๋ ๊ฒ์ด๋ค. (ํ์ ์ด๋? -> ํด๋์ค / ์์ฑ์ / ๋ฉ์๋ ๋ฑ๋ฑ...) ์ค๋์
seeminglyjs.tistory.com