Spring/스프링 입문

[김영한 스프링] 19. AOP

개발게발 2023. 4. 1. 23:42

시간 측정 로직을 추가할 곳이 많으면 많을수록 유지보수 불가능

 

 

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

hello.hellospring/aop/TimeTraceAop 생성

 

@Aspect

 - AOP 사용하도록 함

 

@Component

 - config파일에 Bean을 등록하지 않아도 사용 가능하도록 함

 

@Around()

 - 괄호안에 해당하는 메소드의 실행 전, 후에 동작

 

joinPoint.proceed()

 - 비지니스 메소드 호출

 

App 실행 시 시간 체크

 

회원 등록과 조회 시 시간 체크

 

AOP 적용 전 의존관계

기존에는 Controller에서 Service를 호출

 

AOP 적용 후 의존관계

프록시 Service라는 가짜 Service를 생성시켜서 Controller가 프록시 Service를 호출 후 실제 Service를 호출

 

AOP 적용 전 전체 그림
AOP 적용 후 전체 그림

 

출처 : https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%9E%85%EB%AC%B8-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8

 

[무료] 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 - 인프런 | 강의

스프링 입문자가 예제를 만들어가면서 스프링 웹 애플리케이션 개발 전반을 빠르게 학습할 수 있습니다., - 강의 소개 | 인프런

www.inflearn.com