SpringAopImplement_4_6

์Šคํ”„๋ง AOP ๊ตฌํ˜„ - V4

ํฌ์ธํŠธ์ปท ์ฐธ์กฐ

ํฌ์ธํŠธ์ปท์„ ๊ณต์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ณ„๋„์˜ ์™ธ๋ถ€ ํด๋ž˜์Šค์— ๋ชจ์•„๋‘์–ด๋„ ๋œ๋‹ค.

public class Pointcuts {

    @Pointcut("execution(* hello.aop.order..*(..))")
    public void allOrder(){} //ํฌ์ธํŠธ์ปท ์‹œ๊ทธ๋‹ˆ์ฒ˜

    //ํด๋ž˜์Šค ์ด๋ฆ„ ํŒจํ„ด์ด *Service
    @Pointcut("execution(* *..*Service.*(..))")
    public void allService(){}

    @Pointcut("allOrder() && allService()")
    public void orderAndService(){}
}
  • ์™ธ๋ถ€์—์„œ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด public์œผ๋กœ ์—ด์–ด๋‘์–ด์•ผ ํ•œ๋‹ค.

@Slf4j
@Aspect
public class AspectV4Pointcut {

    @Around("Pointcuts.allOrder()")
    public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("[log] {}", joinPoint.getSignature());
        return joinPoint.proceed();
    }

    //hello.aop.order ํŒจํ‚ค์ง€์™€ ํ•˜์œ„ ํŒจํ‚ค์ง€ ์ด๋ฉด์„œ ๋™์‹œ์— ํด๋ž˜์Šค ์ด๋ฆ„ ํŒจํ„ด์ด *Service
    @Around("Pointcuts.orderAndService()")
    public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            log.info("[ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘] {}", joinPoint.getSignature());
            Object result = joinPoint.proceed();
            log.info("[ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹] {}", joinPoint.getSignature());
            return result;
        } catch (Exception e) {
            log.info("[ํŠธ๋žœ์žญ์…˜ ๋กค๋ฐฑ] {}", joinPoint.getSignature());
            throw e;
        } finally {
            log.info("[๋ฆฌ์†Œ์Šค ๋ฆด๋ฆฌ์ฆˆ] {}", joinPoint.getSignature());
        }
    }
}
  • ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ํฌ์ธํŠธ์ปท ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.

  • ํฌ์ธํŠธ์ปท์„ ๋ชจ์•„๋‘” ํด๋ž˜์Šค์™€ ํฌ์ธํŠธ์ปท์„ ์‚ฌ์šฉํ•˜๋ ค๋Š” ํด๋ž˜์Šค์˜ ํŒจํ‚ค์ง€๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ํฌ์ธํŠธ์ปท์„ ์‚ฌ์šฉํ•˜๋ ค๋Š” ํด๋ž˜์Šค์—์„œ ํŒจํ‚ค์ง€๋ช…๊นŒ์ง€ ํฌํ•จํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

@Slf4j
@SpringBootTest
@Import(AspectV4Pointcut.class)
public class AopTest { ... }
  • ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๊ธฐ์กด๊ณผ ๋˜‘๊ฐ™๋‹ค.

์Šคํ”„๋ง AOP ๊ตฌํ˜„ - V5

์–ด๋“œ๋ฐ”์ด์Šค ์ˆœ์„œ

์–ด๋“œ๋ฐ”์ด์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๊ณ  ์‹ถ์œผ๋ฉด @Aspect ์ง€์ • ๋‹จ์œ„๋กœ @Order ์–ด๋…ธํ…Œ์ด์…˜์„ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋ฌธ์ œ๋Š” ์ด๊ฒƒ์„ ์–ด๋“œ๋ฐ”์ด์Šค ๋‹จ์œ„๊ฐ€ ์•„๋‹ˆ๋ผ ํด๋ž˜์Šค ๋‹จ์œ„๋กœ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ํ•˜๋‚˜์˜ ์• ์ŠคํŽ™ํŠธ์— ์—ฌ๋Ÿฌ ์–ด๋“œ๋ฐ”์ด์Šค๊ฐ€ ์žˆ์œผ๋ฉด ์ˆœ์„œ๋ฅผ ๋ณด์žฅ ๋ฐ›์„ ์ˆ˜ ์—†๊ณ  ์• ์ŠคํŽ™ํŠธ๋ฅผ ๋ณ„๋„์˜ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.

@Slf4j
public class AspectV5Order {

    @Aspect
    @Order(2)
    public static class LogAspect{
        @Around("Pointcuts.allOrder()")
        public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
            log.info("[log] {}", joinPoint.getSignature());
            return joinPoint.proceed();
        }
    }

    @Aspect
    @Order(1)
    public static class TxAspect{
        //hello.aop.order ํŒจํ‚ค์ง€์™€ ํ•˜์œ„ ํŒจํ‚ค์ง€ ์ด๋ฉด์„œ ๋™์‹œ์— ํด๋ž˜์Šค ์ด๋ฆ„ ํŒจํ„ด์ด *Service
        @Around("Pointcuts.orderAndService()")
        public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable {
            try {
                log.info("[around][ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘] {}", joinPoint.getSignature());
                Object result = joinPoint.proceed();
                log.info("[around][ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹] {}", joinPoint.getSignature());
                return result;
            } catch (Exception e) {
                log.info("[around][ํŠธ๋žœ์žญ์…˜ ๋กค๋ฐฑ] {}", joinPoint.getSignature());
                throw e;
            } finally {
                log.info("[around][๋ฆฌ์†Œ์Šค ๋ฆด๋ฆฌ์ฆˆ] {}", joinPoint.getSignature());
            }
        }
    }
}
  • ํ•˜๋‚˜์˜ ์• ์ŠคํŽ™ํŠธ ์•ˆ์— ์žˆ๋–ค ์–ด๋“œ๋ฐ”์ด์Šค๋ฅผ ๊ฐ๊ฐ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ–ˆ๋‹ค. (๊ฐ๊ฐ ๋ณ„๋„์˜ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด๋„ ๋œ๋‹ค.)

  • ๊ฐ ์• ์ŠคํŽ™ํŠธ์— @Order ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์ ์šฉํ–ˆ๋‹ค.

@Slf4j
@SpringBootTest
@Import({AspectV5Order.TxAspect.class, AspectV5Order.LogAspect.class})
public class AopTest { ... }
  • ๋‘ ๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด์•ผ ํ•œ๋‹ค.

## success() ์‹คํ–‰ ๊ฒฐ๊ณผ ์ฝ˜์†”
[ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘] void hello.aop.order.OrderService.orderItem(String)
[log] void hello.aop.order.OrderService.orderItem(String)
[orderService] ์‹คํ–‰
[log] String hello.aop.order.OrderRepository.save(String)
[orderRepository] ์‹คํ–‰
[ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹] void hello.aop.order.OrderService.orderItem(String)
[๋ฆฌ์†Œ์Šค ๋ฆด๋ฆฌ์ฆˆ] void hello.aop.order.OrderService.orderItem(String)
  • ํŠธ๋žœ์žญ์…˜ ์–ด๋“œ๋ฐ”์ด์Šค๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜๊ณ  ์ดํ›„์— ๋กœ๊ทธ ์–ด๋“œ๋ฐ”์ด์Šค๊ฐ€ ์ ์šฉ๋๋‹ค.

img_3.png

์Šคํ”„๋ง AOP ๊ตฌํ˜„ - V6

์–ด๋“œ๋ฐ”์ด์Šค ์ข…๋ฅ˜

  • @Around : ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „ํ›„์— ์ˆ˜ํ–‰์œผ๋กœ ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ์–ด๋“œ๋ฐ”์ด์Šค๋‹ค. ์กฐ์ธ ํฌ์ธํŠธ ์‹คํ–‰ ์—ฌ๋ถ€ ์„ ํƒ, ๋ฐ˜ํ™˜ ๊ฐ’ ๋ณ€ํ™˜, ์˜ˆ์™ธ ๋ณ€ํ™˜ ๋“ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

  • @Before : ์กฐ์ธ ํฌ์ธํŠธ ์‹คํ–‰ ์ด์ „์— ์‹คํ–‰

  • @AfterReturning : ์กฐ์ธ ํฌ์ธํŠธ๊ฐ€ ์ •์ƒ ์™„๋ฃŒํ›„ ์‹คํ–‰

  • @AfterThrowing : ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ๊ฒฝ์šฐ ์‹คํ–‰

  • @After : ์กฐ์ธ ํฌ์ธํŠธ๊ฐ€ ์ •์ƒ ๋˜๋Š” ์˜ˆ์™ธ์— ๊ด€๊ณ„์—†์ด ์‹คํ–‰(finally์™€ ๋น„์Šท)

@Slf4j
@Aspect
public class AspectV6Advice {

    @Around("Pointcuts.orderAndService()")
    public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            //@Before
            log.info("[ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘] {}", joinPoint.getSignature());
            Object result = joinPoint.proceed();
            //@AfterReturning
            log.info("[ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹] {}", joinPoint.getSignature());
            return result;
        } catch (Exception e) {
            //@AfterThrowing
            log.info("[ํŠธ๋žœ์žญ์…˜ ๋กค๋ฐฑ] {}", joinPoint.getSignature());
            throw e;
        } finally {
            //@After
            log.info("[๋ฆฌ์†Œ์Šค ๋ฆด๋ฆฌ์ฆˆ] {}", joinPoint.getSignature());
        }
    }

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

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

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

    @After(value = "Pointcuts.orderAndService()")
    public void doAfter(JoinPoint joinPoint) {
        log.info("[after] {}", joinPoint.getSignature());
    }
}
  • ์‚ฌ์‹ค @Around๋ฅผ ์ œ์™ธํ•œ ์–ด๋“œ๋ฐ”์ด์Šค๋“ค์€ @Around๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์˜ ์ผ๋ถ€๋งŒ ์ œ๊ณตํ•  ๋ฟ์ด๋‹ค. ๋”ฐ๋ผ์„œ @Around ์–ด๋“œ๋ฐ”์ด์Šค๋งŒ ์‚ฌ์šฉํ•ด๋„ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋ชจ๋“  ์–ด๋“œ๋ฐ”์ด์Šค๋Š” JoinPoint๋ฅผ ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.(์ƒ๋žต ๊ฐ€๋Šฅ)

  • @Around๋Š” ProceedingJoinPoint๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • ProceedingJoinPoint๋Š” JoinPoint์˜ ํ•˜์œ„ ํƒ€์ž…์ด๋‹ค.

JoinPoint ์ธํ„ฐํŽ˜์ด์Šค ์ฃผ์š” ๊ธฐ๋Šฅ

  • getArgs() : ๋ฉ”์„œ๋“œ ์ธ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

  • getThis() : ํ”„๋ก์‹œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

  • getTarget() : ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

  • getSignature() : ์กฐ์–ธ๋˜๋Š” ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ฐ˜ํ™˜

  • toString() : ์กฐ์–ธ๋˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ์„ค๋ช…์„ ์ธ์‡„

ProceedingJointPoint ์ธํ„ฐํŽ˜์ด์Šค ์ฃผ์š” ๊ธฐ๋Šฅ

  • proceed() : ๋‹ค์Œ ์–ด๋“œ๋ฐ”์ด์Šค๋‚˜ ํƒ€๊ฒŸ์„ ํ˜ธ์ถœ

@Before

์กฐ์ธ ํฌ์ธํŠธ ์‹คํ–‰ ์ „

@Before("Pointcuts.orderAndService()")
public void deBefore(JoinPoint joinPoint) {
    log.info("[before] {}", joinPoint.getSignature());
}
  • @Around์™€ ๋‹ค๋ฅด๊ฒŒ ์ž‘์—… ํ๋ฆ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†๋‹ค.

  • @Around๋Š” ProceedingJoinPoint.proceed()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ๋‹ค์Œ ๋Œ€์ƒ์ด ํ˜ธ์ถœ๋œ๋‹ค. ๋งŒ์•ฝ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ ๋Œ€์ƒ์ด ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.

  • @Befores๋Š” ProceedingJoinPoint.proceed() ์ž์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ฉ”์„œ๋“œ ์ข…๋ฃŒ ์‹œ ์ž๋™์œผ๋กœ ๋‹ค์Œ ํƒ€๊ฒŸ์ด ํ˜ธ์ถœ๋œ๋‹ค.

  • ๋ฌผ๋ก  ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค.

@AfterReturning

๋ฉ”์„œ๋“œ ์‹คํ–‰์ด ์ •์ƒ์ ์œผ๋กœ ๋ฐ˜ํ™˜๋  ๋•Œ ์‚ฌ์šฉ

@AfterReturning(value = "Pointcuts.orderAndService()", returning = "result")
public void doReturn(JoinPoint joinPoint, Object result){
    log.info("[return] {} return={}", joinPoint.getSignature(), result);
}
  • returning ์†์„ฑ์— ์‚ฌ์šฉ๋œ ์ด๋ฆ„์€ ์–ด๋“œ๋ฐ”์ด์Šค ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.

  • returning์ ˆ์— ์ง€์ •๋œ ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋งŒ ๋Œ€์ƒ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค.(๋ถ€๋ชจ ํƒ€์ž…์„ ์ง€์ •ํ•˜๋ฉด ์ž์‹ ํƒ€์ž…๋„ ์ธ์ •)

  • @Around์™€ ๋‹ค๋ฅด๊ฒŒ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†๋‹ค. ๋ฐ˜ํ™˜ ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด @Around๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • ๋ฐ˜ํ™˜ ๊ฐ์ฒด๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜๋Š” ์žˆ๋‹ค.

@AfterThrowing

๋ฉ”์„œ๋“œ ์‹คํ–‰์ด ์˜ˆ์™ธ๋ฅผ ๋˜์ ธ์„œ ์ข…๋ฃŒ๋  ๋•Œ ์‹คํ–‰

@AfterThrowing(value = "Pointcuts.orderAndService()", throwing = "ex")
public void doThrowing(JoinPoint joinPoint, Exception ex) {
    log.info("[ex] {} message={}", joinPoint.getSignature(), ex.getMessage());
}
  • throwing ์†์„ฑ์— ์‚ฌ์šฉ๋œ ์ด๋ฆ„์€ ์–ด๋“œ๋ฐ”์ด์Šค ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.

  • throwing์ ˆ์— ์ง€์ •๋œ ํƒ€์ž…๊ณผ ๋งž๋Š” ์˜ˆ์™ธ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค.(๋ถ€๋ชจ ํƒ€์ž…์„ ์ง€์ •ํ•˜๋ฉด ์ž์‹ ํƒ€์ž…๋„ ์ธ์ •)

@After

๋ฉ”์„œ๋“œ ์‹คํ–‰์ด ์ข…๋ฃŒ๋˜๋ฉด ์‹คํ–‰๋œ๋‹ค.(finally์™€ ๋น„์Šท)

@After(value = "Pointcuts.orderAndService()")
public void doAfter(JoinPoint joinPoint) {
    log.info("[after] {}", joinPoint.getSignature());
}
  • ์ •์ƒ ๋ฐ ์˜ˆ์™ธ ๋ฐ˜ํ™˜ ์กฐ๊ฑด์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•œ๋‹ค.

  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.

@Around

  • ๋ฉ”์„œ๋“œ ์‹คํ–‰์˜ ์ฃผ๋ณ€์—์„œ ์‹คํ–‰๋œ๋‹ค. ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์ „ํ›„์— ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  • ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ์–ด๋“œ๋ฐ”์ด์Šค๋‹ค.

    • ์กฐ์ธ ํฌ์ธํŠธ ์‹คํ–‰ ์—ฌ๋ถ€ ์„ ํƒ(joinPoint.proceed() ํ˜ธ์ถœ ์—ฌ๋ถ€ ์„ ํƒ)

    • ์ „๋‹ฌ ๊ฐ’ ๋ณ€ํ™˜(joinPoint.proceed(args[]))

    • ๋ฐ˜ํ™˜ ๊ฐ’ ๋ณ€ํ™˜

    • ์˜ˆ์™ธ ๋ณ€ํ™˜

    • ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋Ÿผ try~catch~finally ๋ชจ๋‘ ๋“ค์–ด๊ฐ€๋Š” ๊ตฌ๋ฌธ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

  • ์–ด๋“œ๋ฐ”์ด์Šค์˜ ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ProceedingJoinPoint๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • proceed()๋ฅผ ํ†ตํ•ด ๋Œ€์ƒ์„ ์‹คํ–‰ํ•œ๋‹ค.

    • ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.(์žฌ์‹œ๋„)

## success() ์‹คํ–‰ ๊ฒฐ๊ณผ ์ฝ˜์†”
[around][ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘] void hello.aop.order.OrderService.orderItem(String)
[before] void hello.aop.order.OrderService.orderItem(String)
[orderService] ์‹คํ–‰
[orderRepository] ์‹คํ–‰
[return] void hello.aop.order.OrderService.orderItem(String) return=null
[after] void hello.aop.order.OrderService.orderItem(String)
[around][ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹] void hello.aop.order.OrderService.orderItem(String)
[around][๋ฆฌ์†Œ์Šค ๋ฆด๋ฆฌ์ฆˆ] void hello.aop.order.OrderService.orderItem(String)
img_4.png
  • ์‹คํ–‰ ์ˆœ์„œ: @Around, @Before, @After, @AfterReturning, @AfterThrowing

  • ์–ด๋“œ๋ฐ”์ด์Šค๊ฐ€ ์ ์šฉ๋˜๋Š” ์ˆœ์„œ๋Š” ์ด๋ ‡์ง€๋งŒ ํ˜ธ์ถœ ์ˆœ์„œ์™€ ๋ฆฌํ„ด ์ˆœ์„œ๋Š” ๋ฐ˜๋Œ€์ด๋‹ค.

  • @Aspect์•ˆ์— ๋™์ผํ•œ ์ข…๋ฅ˜์˜ ์–ด๋“œ๋ฐ”์ด์Šค๊ฐ€ 2๊ฐœ ์ด์ƒ ์žˆ์œผ๋ฉด ์ˆœ์„œ๋Š” ๋ณด์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๊ฒฝ์šฐ @Aspect๋ฅผ ๋ณ„๋„ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  @Order๋ฅผ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค.

@Around ์™ธ์— ๋‹ค๋ฅธ ์–ด๋“œ๋ฐ”์ด์Šค๊ฐ€ ์กด์žฌํ•˜๋Š” ์ด์œ 

@Aroundํ•˜๋‚˜๋งŒ ์žˆ์–ด๋„ ๋‹ค๋ฅธ ๋ชจ๋“  ์–ด๋“œ๋ฐ”์ด์Šค์˜ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹ค๋ฅธ ์–ด๋“œ๋ฐ”์ด์Šค๋“ค์ด ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ? @Around๋Š” ํ•ญ์ƒ joinPoint.proceed()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ ํƒ€์ผ“์ด ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ @Before๋Š” joinPoint.proceed()๋ฅผ ์ž๋™์œผ๋กœ ํ˜ธ์ถœํ•ด ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

@Around๊ฐ€ ๊ฐ€์žฅ ๋„“์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€๋งŒ ์‹ค์ˆ˜ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค. ๋ฐ˜๋ฉด์— @Before, @After ๊ฐ™์€ ์–ด๋“œ๋ฐ”์ด์Šค๋Š” ๊ธฐ๋Šฅ์€ ์ ์ง€๋งŒ ์‹ค์ˆ˜ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋‚ฎ๊ณ  ์ฝ”๋“œ๋„ ๋‹จ์ˆœํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ ์€ ์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ์˜๋„๊ฐ€ ๋ช…ํ™•ํ•˜๊ฒŒ ๋“œ๋Ÿฌ๋‚œ๋‹ค๋Š” ์ ์ด๋‹ค. @Before๋ผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ณด๋ฉด ์ด ์ฝ”๋“œ๋Š” ํƒ€๊ฒŸ ์‹คํ–‰ ์ „์— ํ•œ์ •ํ•ด์„œ ์–ด๋–ค ์ผ์„ ํ•˜๋Š” ์ฝ”๋“œ๋ผ๋Š” ๊ฒƒ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์ข‹์€ ์„ค๊ณ„๋Š” ์ œ์•ฝ์ด ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. @Around๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์‹ค์ˆ˜๋กœ ๋‹ค์Œ ํƒ€๊ฒŸ์„ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํฐ ์žฅ์• ๊ฐ€ ์ผ์–ด๋‚  ๊ฒƒ์ด๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ @Before๊ฐ™์€ ํ•œ์ •๋œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด ์ด๋Ÿฐ ๋ฌธ์ œ ์ž์ฒด๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์„ ๊ฒƒ์ด๋‹ค.

์ œ์•ฝ ๋•๋ถ„์— ์—ญํ• ์ด ๋ช…ํ™•ํ•ด์ง€๊ณ  ์˜๋„๋„ ํŒŒ์•…ํ•˜๊ธฐ ์‰ฌ์›Œ์ง„๋‹ค.

Last updated