Springboot/lecture

[springBoot] AOP

Alchemists 2022. 8. 17. 08:24
728x90

๐Ÿฅ‘  AOP๊ฐ€ ํ•„์š”ํ•œ ์ƒํ™ฉ?

 

- ๋ชจ๋“  ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

- ๊ณตํ†ต ๊ด€์‹ฌ์‚ฌํ•ญ(ํ˜ธ์ถœ ์‹œ๊ฐ„) vs ํ•ต์‹ฌ ๊ด€์‹ฌ์‚ฌํ•ญ (๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง)

- ํšŒ์› ๊ฐ€์ž… ์‹œ๊ฐ„, ํšŒ์› ์กฐํšŒ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด? 

 

์‹œ๊ฐ„ ์ธก์ •ํ•˜๋Š” ๋กœ์ง๊ณผ ํ•ต์‹ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์„ž์ด๋ฉด ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ต๋‹ค.  > ๋‘๊ฐœ๋ฅผ ๋ถ„๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. > AOP (Aspect Oriented Programming)

 

 

๐Ÿฅ‘  AOP

 

์‹œ๊ฐ„ ์ธก์ • ๋กœ์ง์„ ์งœ๋†“๊ณ  ์›ํ•˜๋Š”  ๊ณณ์— ๊ณตํ†ต ๊ด€์‹ฌ์‚ฌํ•ญ(์‹œ๊ฐ„ ์ธก์ • ๋กœ์ง) ์ ์šฉ ํ•˜๋ฉด ๋˜๊ฒ ๋‹ค.

 

TimeTraceAop ๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์œ„์— Aspect ์–ด๋…ธํ…Œ์ด์…˜์„ ์“ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์Šคํ”„๋ง bean์— ๋“ฑ๋กํ•ด์ค€๋‹ค.

๋นŒ๋“œํ• ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค๋ฉด SpringConfig์—์„œ ๋นˆ์„ ๋“ฑ๋กํ•˜์ง€ ๋ง๊ณ  TimeTraceAop์—์„œ ์ƒ๋‹จ์— Component ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

 

 

Around ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด์„œ ์–ด๋””์— ๊ณตํ†ต ๊ด€์‹ฌ ์‚ฌํ•ญ์„ ์ ์šฉํ•  ์ง€ ์ •ํ•ด์ค€๋‹ค. 

 

hello ํŒจํ‚ค์ง€ ์•ˆ์— hellospring ํŒจํ‚ค์ง€ ์•ˆ์— ์žˆ๋Š” ํด๋ž˜์Šค๋“ค์— ์ ์šฉํ•ด์ค€๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. 

 

์ตœ์ข… ์ฝ”๋“œ

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package hello.hellospring.aop;
 
 
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 TimeTraceAop {
 
    @Around("execution(* hello.hellospring..*(..))")
 
    public Object execut(ProceedingJoinPoint joinPoint) throws Throwable{
 
        long start=System.currentTimeMillis();
        System.out.println("start: "+joinPoint.toString());
        try{
            return joinPoint.proceed();
        }finally{
            long finish=System.currentTimeMillis();
            long timeMs=finish-start;
            System.out.println("end: "+joinPoint.toString()+" "+timeMs+"ms");
        }
    }
 
 
 
}
 
cs

 

 

728x90