Spring

[Spring] 실습 : AOP 프로그램 만들기

기록하는_사람 2022. 11. 4. 18:32

AOP 프로그램 만들기

📌 AOP를 사용하려면 아래 코드를 build.gradle에 추가해야 함. 

...
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	implementation 'org.springframework.boot:spring-boot-starter-aop'  // 코드 추가. 
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
...

 

📌 AOP 클래스 생성

package com.example.demo.ch03.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

@Aspect  // Aspect : 어드바이스 정리한 클래스.
@Component  // 인스턴스 생성.
public class SampleAspect {
    // @Before : 중심적 관심사 실행 전에 실행. 
    @Before("execution(* com.example.demo.ch03.used.*Greet.*(..))")
    public void beforeAdvice(JoinPoint jp) {
        System.out.println("========== Before Advice ==========");
        System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(new java.util.Date()));
        System.out.println(String.format("Method: %s", jp.getSignature().getName()));
    }

    // @After : 중심적 관심사 실행 후에 실행. 
    @After("execution(* com.example.demo.ch03.used.*Greet.*(..))")
    public void afterAdvice(JoinPoint jp) {
        System.out.println("========== After Advice ==========");
        System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(new java.util.Date()));
        System.out.println(String.format("Method: %s", jp.getSignature().getName()));
    }

    // @AfterReturning : 중심적 관심사가 정상적으로 종료된 후에 실행. 
    @AfterReturning("execution(* com.example.demo.ch03.used.*Greet.*(..))")
    public void afterReturningAdvice(JoinPoint jp) {
        System.out.println("===== After Returning Advice =====");
        System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(new java.util.Date()));
        System.out.println(String.format("Method: %s", jp.getSignature().getName()));
    }
    
    // @AfterThrowing : 중심적 관심사로부터 예외가 던져진 후에 실행. 
    @AfterThrowing("execution(* com.example.demo.ch03.used.*Greet.*(..))")
    public void afterThrowingAdvice(JoinPoint jp) {
        System.out.println("===== After Throwing Advice =====");
        System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(new java.util.Date()));
        System.out.println(String.format("Method: %s", jp.getSignature().getName()));
    }

    // @Around : 중심적 관심사 호출 전후에 실행. 
    @Around("execution(* com.example.demo.ch03.used.*Greet.*(..))")
    public Object aroundAdvice(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("========== Around Advice ==========");
        System.out.println("--- before proceed ---");
        Object result = jp.proceed();
        System.out.println("--- after proceed ---");
        return result;
    }
}

 

참고

https://codingrecord2209.tistory.com/289

 

[Spring] AOP(관점 지향 프로그래밍)

AOP(관점 지향 프로그래밍) 📌 AOP(관점 지향 프로그래밍, Aspect Oriented Programming) : '횡단적 관심사'를 추출하고, 프로그램의 여러 곳에서 호출할 수 있게 함으로써 개발자는 실현해야 할 기능인 '중

codingrecord2209.tistory.com