본문 바로가기
대외활동/UMC 8기

4주차. AOP란

by 피스타0204 2025. 4. 7.

 AOP란

AOP는 관점 지향 프로그래밍으로, OOP(Object-Oriented Programming, 객체 지향 프로그래밍)를 보완하는 프로그래밍 패러다임입니다.Spring IoC 컨테이너는 AOP에 의존하지 않지만 AOP는 Spring IoC를 보완하여 매우 유능한 미들웨어 솔루션을 제공합니다. 애플리케이션의 공통적으로 적용되는 코드(cross-cutting concern)를 분리 하고 관리할 수 있게 해줍니다.

 

-용어

#Aspect:  여러 클래스에 공통적으로 적용되는 코드(cross-cutting concern)를 분리해서 모듈화한 것입니다.

#Join point: 프로그램 실행 중 aspect가 끼어들 수 있는 특정한 지점을 말합니다. 해당 시점에서는 주로 메소드 실행이나 예외 처리가 일어납니다. 아래 예시에서는 getUser가 호출되는 시점입니다.

@Before("execution(* com.example.service.UserService.getUser(..))")
public void logBefore() {
    System.out.println("getUser 메서드 호출 전 로깅");
}

 

#advice: 특정 join point에서수행될 구체적인 동작입니다. advice type으로는 "around", "before", "after" 등이 있습니다. 대부분의 AOP 프레임워크(스프링 포함)는 어드바이스를 인터셉터로 모델링하고, 조인 포인트 주위에 인터셉터 체인을 유지합니다.

 

# point cut: 어떤 join point를 골라서 advice를 실행할지 지시해주는 부분을 말합니다.

@Before("execution(* com.example.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
    System.out.println("Before method: " + joinPoint.getSignature());
}

위 코드를 보면 @Before 어노테이션 안에 "execution(...)"이라는 표현식이 들어가 있죠?

이게 포인트컷 표현식이고,
그 의미는 "com.example.service 패키지 안에 있는 모든 클래스의 모든 메서드 실행 전에 이걸 실행해줘!" 라는 뜻입니다.

 

# Introduction: 기존 클래스가 전혀 구현하지 않았던 인터페이스를 강제로 구현하게 만들어서, 마치 원래부터 그 기능을 가진 것처럼 행동하게 만드는 것입니다.

📌 예시: IsModified 인터페이스 도입

public interface IsModified {
    boolean isModified();
}

 

-도입할 Aspect

@Aspect
public class IntroductionAspect {

    @DeclareParents(value = "com.example.service.*+", defaultImpl = IsModifiedImpl.class)
    public static IsModified mixin;
}

 

-기본 구현 클래스

public class IsModifiedImpl implements IsModified {
    @Override
    public boolean isModified() {
        // 변경 여부 판단 로직
        return true;
    }
}

 

-이렇게 하면 com.example.service 내의 모든 Bean이 IsModified 인터페이스를 자동으로 구현한 것처럼 사용할 수 있습니다!

MyService service = context.getBean(MyService.class);
if (service instanceof IsModified) {
    boolean modified = ((IsModified) service).isModified();
}

 

# Target object: advice가 적용되는 실제 객체입니다. 스프링에서는 이 타깃 객체가 항상 프록시로 감싸져 있습니다.

# proxy: 프록시는 어떤 객체를 대신해서 행동하는 객체입니다.즉, 원래 객체에 대한 접근을 제어하거나, 추가 동작을 넣기 위한 대리인입니다.

# AOP proxy: Spring AOP는 개발가 만든 Bean을 직접 사용하는 게 아니라, 프록시를 사용하게 합니다.

📌 예시 코드로 보면

클래스 정의)

public class UserService {
    public void createUser() {
        System.out.println("유저 생성");
    }
}

호출시)

UserService userService = context.getBean(UserService.class);
userService.createUser();  // ← 이 userService는 실제 객체가 아님!

userService는 사실 프록시입니다. Spring이 만든 프록시 객체가 대신 호출을 가로채고, 필요한 어드바이스(예: 로그 찍기)를 실행한 뒤 진짜 createUser()를 호출합니다.

 

#weaving: Weaving은 Aspect를 애플리케이션의 실제 코드(타입 또는 객체)에 연결하여 Advice가 지정된 Join point에서 실행되도록 만드는 과정입니다. 이 과정은 컴파일 시, 클래스 로딩 시, 또는 런타임에 수행될 수 있으며, Spring AOP는 순수 Java 기반이기 때문에 런타임에 Weaving을 수행합니다.

 

https://docs.spring.io/spring-framework/reference/core/aop.html

 

Aspect Oriented Programming with Spring :: Spring Framework

Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enabl

docs.spring.io