๊น€์˜ํ•œ๋‹˜ ์Šคํ”„๋ง ๊ณ ๊ธ‰ํŽธ ๊ฐ•์˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

AOP(Aspect Oriented Programming) ์šฉ์–ด ์ •๋ฆฌ

  • Join Point
    • Advice๊ฐ€ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜, ๋ฉ”์„œ๋“œ ์‹คํ–‰, ์ƒ์„ฑ์ž ํ˜ธ์ถœ, ํ•„๋“œ ๊ฐ’ ์ ‘๊ทผ, static ๋ฉ”์„œ๋“œ ์ ‘๊ทผ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘ ์ง€์ 
    • Joint Point๋Š” ์ถ”์ƒ์ ์ธ ๊ฐœ๋…. AOP๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ง€์ ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
    • Spring AOP๋Š” ํ”„๋ก์‹œ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ Join Point๋Š” ์ƒ์ƒ ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์ง€์ ์œผ๋กœ ์ œํ•œ๋œ๋‹ค.
  • Pointcut
    • Join Point ์ค‘์— Advice๊ฐ€ ์ ์šฉ๋  ์œ„์น˜๋ฅผ ์„ ๋ณ„ํ•˜๋Š” ๊ธฐ๋Šฅ
    • ์ฃผ๋กœ AspectJ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์„œ ์ง€์ •
    • ํ”„๋ก์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” Spring AOP๋Š” ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์ง€์ ๋งŒ Pointcur์œผ๋กœ ์„ ๋ณ„ ๊ฐ€๋Šฅ.
  • Target
    • Advice๋ฅผ ๋ฐ›๋Š” ๊ฐ์ฒด, ์ฆ‰ Advice(๋ถ€๊ฐ€ ๊ธฐ๋Šฅ ๋กœ์ง)๋ฅผ ์ ์šฉ๋ฐ›๋Š” ๊ฐ์ฒด. Pointcut์œผ๋กœ ๊ฒฐ์ •
  • Advice
    • ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ
    • ํŠน์ • Join Point์—์„œ Aspect์— ์˜ํ•ด ์ทจํ•ด์ง€๋Š” ์กฐ์น˜ (e.g. ๋กœ๊ทธ ๋‚จ๊ธฐ๊ธฐ, ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์‹œ๊ฐ„ ์ถ”๊ฐ€)
    • Around(์ฃผ๋ณ€), Before(์ „), After(ํ›„)์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ Advice๊ฐ€ ์žˆ์Œ.
  • Aspect

๊ป˜ ์กด์žฌ

  • Advisor
    • ํ•˜๋‚˜์˜ Advice์™€ ํ•˜๋‚˜์˜ PointCut์œผ๋กœ ๊ตฌ์„ฑ๋œ ํŠน๋ณ„ํ•  Advisor
    • Spring AOP์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ํŠน๋ณ„ํ•œ ์šฉ์–ด
  • Weaving
    • Pointcut์œผ๋กœ ๊ฒฐ์ •ํ•œ Target์˜ Join Point์— Advice๋ฅผ ์ ์šฉ
    • Weaving์„ ํ†ตํ•ด ํ•ต์‹ฌ ๊ธฐ๋Šฅ ์ฝ”๋“œ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ณ  ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ
    • AOP ์ ์šฉ์„ ์œ„ํ•ด Aspect๋ฅผ ๊ฐ์ฒด์— ์—ฐ๊ฒฐํ•œ ์ƒํƒœ
      • Compile Time(AspectJ compiler)
      • Load Time
      • Run Time, Spring AOP, Proxy ๋ฐฉ์‹

Advice ์ข…๋ฅ˜

  • @Around: ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „ํ›„์— ์ˆ˜ํ–‰, ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ์–ด๋“œ๋ฐ”์ด์Šค, ์กฐ์ธ ํฌ์ธํŠธ ์‹คํ–‰ ์—ฌ๋ถ€ ์„ ํƒ, ๋ฐ˜ํ™˜ ๊ฐ’ ๋ณ€ํ™˜, ์˜ˆ์™ธ ๋ณ€ํ™˜ ๋“ฑ์ด ๊ฐ€๋Šฅ
  • @Before: ์กฐ์ธ ํฌ์ธํŠธ ์‹คํ–‰ ์ด์ „์— ์‹คํ–‰
  • @AfterReturning: ์กฐ์ธ ํฌ์ธํŠธ๊ฐ€ ์ •์ƒ ์™„๋ฃŒํ›„ ์‹คํ–‰
  • @AfterThrowing: ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ๊ฒฝ์šฐ ์‹คํ–‰
  • @After: ์กฐ์ธ ํฌ์ธํŠธ๊ฐ€ ์ •์ƒ ๋˜๋Š” ์˜ˆ์™ธ์— ๊ด€๊ณ„์—†์ด ์‹คํ–‰(finally)
@Slf4j
@Aspect
public class AspectV6Advice {

    @Around("hello.aop.order.aop.Pointcuts.orderAndService()")
    public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            //@Before
            log.info("[around][ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘] {}", joinPoint.getSignature());
            Object result = joinPoint.proceed();
            //@AfterReturning
            log.info("[around][ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹] {}", joinPoint.getSignature());\
            return result;

        } catch (Exception e) {

            //@AfterThrowing
            log.info("[around][ํŠธ๋žœ์žญ์…˜ ๋กค๋ฐฑ] {}", joinPoint.getSignature());
            throw e;
        } finally {
            //@After
            log.info("[around][๋ฆฌ์†Œ์Šค ๋ฆด๋ฆฌ์ฆˆ] {}", joinPoint.getSignature());
        }
    }

    @Before("hello.aop.order.aop.Pointcuts.orderAndService()")
    public void doBefore(JoinPoint joinPoint) {
        log.info("[before] {}", joinPoint.getSignature());
    }

    @AfterReturning(value = "hello.aop.order.aop.Pointcuts.orderAndService()",
        returning = "result")
    public void doReturn(JoinPoint joinPoint, Object result) {
        log.info("[return] {} return={}", joinPoint.getSignature(), result);
    }

    @AfterThrowing(value = "hello.aop.order.aop.Pointcuts.orderAndService()",
        throwing = "ex")
    public void doThrowing(JoinPoint joinPoint, Exception ex) {
        log.info("[ex] {} message={}", joinPoint.getSignature(),
            ex.getMessage());
    }

    @After(value = "hello.aop.order.aop.Pointcuts.orderAndService()")
    public void doAfter(JoinPoint joinPoint) {
        log.info("[after] {}", joinPoint.getSignature());
    }
}

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ