JAVA/Spring Boot
(Spring Boot) AOP 동작원리
ri5
2021. 6. 25. 14:34
1. AOP란
어떤 서비스나 메서드가 실행되기전에 공통적으로 들어 가야할 이벤트, 메서드 등이 필요 할때 사전에 정의해둔 클래스를 작동하고 나서 실제 서비스가 작동하게 하는 것.
2. AOP의 장점
불필요한 비지니스 로직이 줄어서 좀 더 직관적인 비지니스 로직을 짤 수 있음.
3. 사용 예시
@Aspect
@Component
public class TimeTraceAop {
@Around("execution(* hello.hellospring..*(..))")
public Object execute(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");
}
}
}
이렇게 컴포넌트와 Aspect를 명시 해놓으면 @Around 안에 있는 hello.hellospring 아래에 있는 모든 클래스 들은 동작하기전에 TimeTarce를 거쳐 간다.
3. 동작 구조
- AOP 적용전
- AOP 적용후