본문 바로가기
프레임워크/스프링

[Spring] 스프링 AOP

by soso-shs 2022. 9. 30.

김영한의 스프링 강의 공부후 복습하기 위해서

 

AOP는 Aspect Oriented Programming의 약자로 관점 지향 프로그래밍이라고 한다고 합니다.

어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각각 모듈화하겠다는 것다.

여기서 모듈화란 어떤 공통된 로직이나 기능을 하나의 단위로 묶는 것을 말한다. 

각각의 색들은 Concern이라고 보면된다. 여기서 Concern이란 여러 클래스, 메서드에 걸쳐서 나타나는 비슷한 코드들을 의미한다.

각 클래스에 있는 Crosscutting Concerns, 흩어진 관심사를 Aspect로 모듈화하고 핵심적인 비즈니스 로직에서 분리하여 재사용하겠다는 것이 AOP의 취지이다.

 

 

 

@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");
    	}
    }
}