2025-04-29
โ ํนํ, ๋ฐ๋ณต์ ์ผ๋ก ์ํํด์ผ ํ๋ ์์ (๋ก๊น , ํธ๋์ญ์ ์์/์ข ๋ฃ, ์ธ์ฆ ๊ฒ์ฌ ๋ฑ)์ ์ฒ๋ฆฌํ ๋ ๋งค์ฐ ์ ์ฉํ๋ค!
(โ ํต์ฌ ๋น์ฆ๋์ค ๋ก์ง์ ์ค๋ณต ์ฝ๋๋ฅผ ๋ฃ์ง ์๊ณ ๊ณตํต ์์ ์ ๋ชจ๋ํํ ์ ์๋ค.)
โ 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>
โ
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());
}
}
โ ์ ์ฒด ์กฐํ API ํธ์ถ
http://localhost:8091/ex13_aop/rest/memo/getAll
[
{
"id": 7779,
"text": "7779",
"writer": null,
"createAt": null
},
{
"id": 7999,
"text": "asdf",
"writer": null,
"createAt": null
}
...
]
โ ๋จ๊ฑด ์กฐํ API ํธ์ถ
http://localhost:8091/ex13_aop/rest/memo/get/7779
{
"id": 7779,
"text": "7779",
"writer": null,
"createAt": null
}
โ ์ถ๊ฐ ํ์ธ ์ฌํญ
@After
๋ก๊น
์ด ์ ์์ ์ผ๋ก ์๋ฒ ์ฝ์์ ์ถ๋ ฅ๋จcreateAt
์ ์ผ๋ถ null, ์ผ๋ถ๋ LocalDateTime ๋ฐฐ์ด๋ก ๋ฐํ๋จ (ํ์ ์ ํฌ๋งทํฐ ์ ์ฉ ๊ฐ๋ฅ)@AfterReturning
, @Around
๋ฅผ ์ ์ฉํด๋ณด๊ณ ์ถ๋ค.