Spring

[Spring] AOP ์ •๋ฆฌ

obin01 2023. 7. 24. 00:34

๐Ÿ“Œ ์šฉ์–ด ์ •๋ฆฌ

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