➡️ 공통 관심 사항을 원하는 곳에 적용할 수 있는 기술
🔸핵심 관심사항과 시간을 측정하는 공통 관심 사항 분리
🔸시간을 측정하는 로직을 별도의 공통 로직으로 구현 >> 변경시 이 로직만 변경하면 됨
🔸핵심 관심사항은 깔끔하게 유지 가능
🔸원하는 적용 대상 선택 가능

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 // AOP 사용 가능 어노테이션
@Component // Bean에 등록
public class TimeTraceAop {
@Around("execution(* hello.hellospring..*(..))")
//패키지명.클래스.파라미터타입 등 원하는 적용 부분 지정 (지정하는 문법 사용)
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable{
// 호출이 될 때마다 joinPoint에 걸림
long start = System.currentTimeMillis();
System.out.println("START : " + joinPoint.toString()); //걸렸을 때 start지점 찍고
try {
return joinPoint.proceed(); //계속 실행하다가
} finally{ //마지막에 finish지점 찍음
long finish = System.currentTimeMillis();
long timeMs = finish-start;
System.out.println("END : " + joinPoint.toString() + " " + timeMs + "ms");
}
}
}
➡️ 스프링의 AOP 작용 방식
🔸프록시라는 기술로 생성된 가짜 클래스가 먼저 호출됨

'프로그래밍 > - Spring' 카테고리의 다른 글
| [ 스프링 핵심 원리 이해1 ] 예제 만들기(순수 Java) : 프로젝트 생성~회원도메인 실행과 테스트 (0) | 2025.02.20 |
|---|---|
| [ 객체 지향 설계와 스프링 ] 스프링이란? / 좋은 객체 지향 프로그래밍이란? / 좋은 객체 지향 설계의 5가지 원칙(SOLID) / 객체 지향 설계와 스프링 (0) | 2025.02.19 |
| [ AOP ] AOP가 필요한 상황 (0) | 2025.02.13 |
| [ 스프링 DB 접근 기술 ] 스프링 데이터 JPA (0) | 2025.02.13 |
| [ 스프링 DB 접근 기술 ] JPA (0) | 2025.02.12 |