스프링 입문-03 AOP

junkyu lee·2022년 5월 25일
0

Spring

목록 보기
4/6

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/lecture/49601?tab=curriculum&volume=1.00&speed=1.25


AOP(Aspect Oriented Programming)

  • 관점 지향 프로그래밍
  • 공통관심사항과 핵심관심사항을 분리한다.
    ex) 핵심 관심 사항 : 회원 관리, 회원 목록 조회 등
    공통 관심 사항 : 서비스 메소드들의 수행시간 측정, 각 메소드의 사용 메모리 등
  • AOP 적용 전
  • AOP 적용 후

AOP 적용 방법

aop패키지 생성

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;

@Component // spring bean 과의 연결
@Aspect // AOP 어노테이션
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");
        }
    }
}

@Around("execution( hello.hellospring..(..))")

  • * hello.hellospring..*(..) : 해당 패키지의 모든 클래스 내부의 메소드를 대상으로 AOP 로직을 진행한다.

프록시 : 가짜 Serviec를 생성, 생성된 프록시가 실제 메서드를 실행시키는 방식

profile
가끔 기록하는 velog

0개의 댓글