๐Ÿ“Œ Spring AOP ๊ฐœ๋… ๋ฐ ์ ์šฉ ์‹ค์Šต

My Pale Blue Dotยท2025๋…„ 4์›” 29์ผ
0

SPRING

๋ชฉ๋ก ๋ณด๊ธฐ
29/36
post-thumbnail

๐Ÿ“… ๋‚ ์งœ

2025-04-29


๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ

1๏ธโƒฃ AOP๋ž€?

  • AOP (Aspect Oriented Programming, ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ) ํ•ต์‹ฌ ๋กœ์ง๊ณผ ๊ณตํ†ต ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ๋ฒ•๋ก ์ด๋‹ค.
  • ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ: Aspect, JoinPoint, Advice, Pointcut, Weaving
  • ์ฃผ ์‚ฌ์šฉ ์‚ฌ๋ก€: ๋กœ๊น…, ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ, ๋ณด์•ˆ ๊ฒ€์‚ฌ

โœ… ํŠนํžˆ, ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…(๋กœ๊น…, ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘/์ข…๋ฃŒ, ์ธ์ฆ ๊ฒ€์‚ฌ ๋“ฑ)์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค!

(โ†’ ํ•ต์‹ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ์ค‘๋ณต ์ฝ”๋“œ๋ฅผ ๋„ฃ์ง€ ์•Š๊ณ  ๊ณตํ†ต ์ž‘์—…์„ ๋ชจ๋“ˆํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.)


2๏ธโƒฃ Spring AOP ์„ค์ •

โœ… Maven ์˜์กด์„ฑ ์ถ”๊ฐ€

<!-- spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

<!-- aspectjrt -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${org.aspectj-version}</version>
</dependency>

<!-- aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${org.aspectj-version}</version>
    <scope>runtime</scope>
</dependency>

โœ… root-context.xml ์„ค์ •

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy />
</beans>

3๏ธโƒฃ AOP ํด๋ž˜์Šค ์ž‘์„ฑ

โœ… LoggingAdvice ํด๋ž˜์Šค

package com.example.app.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

@Aspect
@Component
@Slf4j
public class LoggingAdvice {

    @Before("execution(boolean com.example.app.domain.service.MemoServiceImpl.registraionMemo(..))")
    public void loggingBefore(JoinPoint joinPoint) {
        log.info("[AOP] BEFORE..." + joinPoint);
    }

    @After("execution(* com.example.app.domain.service.MemoServiceImpl.getAllMemo())")
    public void loggingAfter(JoinPoint joinPoint) {
        log.info("[AOP] AFTER..." + joinPoint);
        log.info("[AOP] AFTER TARGET..." + joinPoint.getTarget());
        log.info("[AOP] AFTER SIGNATURE..." + joinPoint.getSignature());
        log.info("[AOP] AFTER METHOD..." + joinPoint.getSignature().getName());
    }
}

4๏ธโƒฃ Postman ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ

โœ… ์ „์ฒด ์กฐํšŒ API ํ˜ธ์ถœ

  • URL: http://localhost:8091/ex13_aop/rest/memo/getAll
  • ์‘๋‹ต ์˜ˆ์‹œ (JSON):
[
  {
    "id": 7779,
    "text": "7779",
    "writer": null,
    "createAt": null
  },
  {
    "id": 7999,
    "text": "asdf",
    "writer": null,
    "createAt": null
  }
  ...
]

โœ… ๋‹จ๊ฑด ์กฐํšŒ API ํ˜ธ์ถœ

  • URL: http://localhost:8091/ex13_aop/rest/memo/get/7779
  • ์‘๋‹ต ์˜ˆ์‹œ (JSON):
{
  "id": 7779,
  "text": "7779",
  "writer": null,
  "createAt": null
}

โœ… ์ถ”๊ฐ€ ํ™•์ธ ์‚ฌํ•ญ

  • ์ „์ฒด ๋ฆฌ์ŠคํŠธ ๋ฐ ๋‹จ๊ฑด ์กฐํšŒ ๋ชจ๋‘ ์ •์ƒ ๋™์ž‘
  • AOP @After ๋กœ๊น…์ด ์ •์ƒ์ ์œผ๋กœ ์„œ๋ฒ„ ์ฝ˜์†”์— ์ถœ๋ ฅ๋จ
  • createAt์€ ์ผ๋ถ€ null, ์ผ๋ถ€๋Š” LocalDateTime ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜๋จ (ํ•„์š” ์‹œ ํฌ๋งทํ„ฐ ์ ์šฉ ๊ฐ€๋Šฅ)

๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ


๋А๋‚€ ์ 

  • AOP๋ฅผ ์ด์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์ •๋ง ํŽธ๋ฆฌํ–ˆ๋‹ค.
  • ํŠนํžˆ, ๋ฐ˜๋ณต์ ์ธ ๋กœ๊น…์ด๋‚˜ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•ต์‹ฌ ๋กœ์ง๊ณผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๋งค์šฐ ์‰ฌ์›Œ์งˆ ๊ฒƒ ๊ฐ™๋‹ค.
  • ์„ค์ •๋งŒ ์ž˜ ํ•ด๋‘๋ฉด ํ”„๋กœ์ ํŠธ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ๊ทธ ์ง„๊ฐ€๋ฅผ ๋ฐœํœ˜ํ•  ๊ฒƒ ๊ฐ™๋‹ค.
  • ๋Œ€๊ทœ๋ชจ ์‹œ์Šคํ…œ์ด๋‚˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜(MSA) ํ™˜๊ฒฝ์—์„œ๋Š” AOP๋ฅผ ํ†ตํ•œ ๊ณตํ†ต ๋ชจ๋“ˆ ๊ด€๋ฆฌ๊ฐ€ ํ•„์ˆ˜์ ์ด๋ผ๊ณ  ๋А๊ผˆ๋‹ค.
  • ๋‹ค์Œ์—๋Š” @AfterReturning, @Around๋ฅผ ์ ์šฉํ•ด๋ณด๊ณ  ์‹ถ๋‹ค.

profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€