Spring AOP

Stella·2022년 4월 16일
0

Java

목록 보기
4/18

AOP

AOP가 필요한 상황

  • 모든 Method의 호출 시간을 측정.
  • 공통 관심 사항(cross-cutting concern) vs 핵심 관심 사항(core concern)
  • 회원 가입 시간, 회원 조회 시간을 측정.

AOP 적용

  • AOP: Aspect Oriented Programming
  • 공통 관심 사항(cross-cutting concern) vs 핵심 관심 사항(core concern) 분리

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component // 쓰거나 아니면 bean으로 등록
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(); // 다음 method로 진행
        } finally {
            long finish = System.currentTimeMillis();
            long timeMs = finish-start;
            System.out.println("End"+joinPoint.toString()+" "+timeMs+"ms");

        }

    }
}

aop가 있으면 가짜memberService(프록시)를 만들어서 실행하고, joinPoint.proceed()가 실행되면 실제 MemberSerivce가 실행된다.

프록시가 실행되는 걸 확인 할 수 있다.

@Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
        System.out.println("memberService = "+ memberService.getClass());
    }
결과
STARTexecution(MemberService hello.hellospring.SpringConfig.memberService())
Endexecution(MemberService hello.hellospring.SpringConfig.memberService()) 13ms
memberService = class hello.hellospring.service.MemberService$$EnhancerBySpringCGLIB$$71033579

memberService = class hello.hellospring.service.MemberServiceEnhancerBySpringCGLIBEnhancerBySpringCGLIB71033579 -> memberSerivce를 복제해서 코드 조작.

Ref:
*김영한님의 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 강의

profile
Hello!

0개의 댓글