반응형

[Spring] AOP (Aspect Oriented Programming)

AOP는 관점 지향 프로그래밍이라고 불린다. 관점 지향은 쉽게 말해 어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각각 모듈화하겠다는 것이다. 여기서 모듈화란 어떤 공통된 로직이나 기능을 하나의 단위로 묶는 것을 말한다.

 

@Aspect
@Component
public class TimeTraceAop{

	@Around("execution(* hello.hellospring..*(..))")
	// @Around 어노테이션을 통해 어느 위치 하위(파라미터)에 적용할지 적용
	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");
		}
	}
}

 

스프링의 AOP 동작 방식
  • AOP 적용 시, Controller 이후 프록시 Service + joinPoint.proceed() 이후 실제 Service가 동작 하는 방식으로 적용
반응형

+ Recent posts