Spring AOP 정리
2024. 3. 12. 08:57ㆍSpring
목차
Spring AOP(Aspect Oriented Programming) 란?
관점 지향 프로그래밍dmfh, 관심 사항에 대해서 공통적인 핵심 관점, 부가적인 관점 등으로 나누어서 그 관점을 모듈화 하는 것이다.
해당 모듈은 @Aspect 데코레이터로 적용하여 사용한다. 보통은 Config에서 Bean으로 등록하여 사용하지만, ComponentScan을 이용해 등록하기도 한다. 내부동작은 SpringCGLIB를 사용해 프록시 매서드를 만들어 적용.
AOP 종류
- 공통 관심 사항(cross-cutting concern)
- 핵심 관심 사항(core concern)
AOP가 필요한 상황
- ex) 모든 메소드의 호출 시간을 측정하고 싶다면?
- 공통 관심 사항(cross-cutting concern) vs 핵심 관심 사항(core concern)
- 회원 가입 시간, 회원 조회 시간을 측정하고 싶다면?
예시
- 로직 시간 실행시간 출력 AOP
- 해당 예시는 ComponentScan을 사용해 bean 등록
- @Around 를 사용해 범위 적용
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;
@Component
@Aspect
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");
}
}
}
참고
'Spring' 카테고리의 다른 글
FeignClient를 이용한 API 호출(HttpClient) (0) | 2024.03.13 |
---|---|
Spring Security 의인증 관리 (0) | 2024.03.13 |
@Resource, @Autowired, @Inject 차이점(feat. @Qualifier) (0) | 2021.10.18 |
Intellij 자동주석 템플릿 (0) | 2021.10.06 |
[Intellij] Import 시 cannot resolve symbol 에러 (0) | 2021.08.24 |