카테고리 없음

AOP 개념 정리

박수연_01 2023. 11. 20. 20:59

AOP(Aspect Oriented Programming) 정의

- 여러 비즈니스모듈에서공통으로사용되는횡단관심사를중심으로설계, 개발하는 프로그래밍 기법

 

AOP목적

- DI는 어플리케이션객체간의결합도를낮춘다

.- AOP는 횡단 관심사와이에영향받는객체간결합도를낮춘다. 

 

AOP 장점

- 전체코드에흩어져있는관심사들이 그외공통 하나의장소로응집됨

- 기타비즈니스모듈들은본질적인처리들을위한핵심기능에대한코드만기술하고 , 관심사들은공통모듈로옮겨지므로코드가깔끔해진다.

 

 Advice
실질적으로 부가기능을 담은 구현체

 

Aspect
핵심기능에 부가되어 의미를 갖는  모듈

 

PointCut
부가기능이 적용될 대상을 선정하는 방법

 

pointcut 표현식

 

JoinPoint

어드바이스가 적용될 수 있는 위치

 

@Pointcut("execution(* com.example.todo..*(..))")
private void cut(){}

대상 위치를 pointcut으로 정한다.

@Before("cut()")
public void beforeParameterLog(JoinPoint joinPoint) {
    Method method = getMethod(joinPoint);
    log.info("======= method name = {} =======", method.getName());

    Object[] args = joinPoint.getArgs();
    if (args.length <= 0) log.info("no parameter");
    for (Object arg : args) {
        log.info("parameter type = {}", arg.getClass().getSimpleName());
        log.info("parameter value = {}", arg);
    }
}

실제 실행될 함수를 작성해준다.

private Method getMethod(JoinPoint joinPoint) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    return signature.getMethod();
}

joinpoint를 이용해 해당 함수의 정보를 가지고 왔다.