
์๋ก
๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ AOP(Aspect Oriented Programming)๋ ํต์ฌ๊ธฐ๋ฅ์์์ ๋ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ๋ถ๋ฆฌํ๊ณ ๋ถ๋ฆฌํ ๋ถ๊ฐ๊ธฐ๋ฅ์ Aspect(์์คํํธ)๋ผ๋ ๋ชจ๋ํํ๋ก ๋ง๋ค์ด ์ค๊ณํ๊ณ ๊ฐ๋ฐํ๋ ๋ฐฉ์์ ๋งํ๋ค. ๋ณธ๋ฌธ์์๋ AOP์ ๊ธฐ๋ณธ๊ฐ๋ ๊ณผ ํน์ง ๊ทธ๋ฆฌ๊ณ Aspect์ ๊ฐ๋ ๊ณผ ํน์ง์ ๋ํด์ ์์ธํ ๋ค๋ค๋ณด๊ณ ์ ํ๋ค.
AOP ๊ธฐ๋ณธ ๊ฐ๋
| Aspect | ๊ด์ฌ์ฌ๋ฅผ ๋ชจ๋ํํ ๊ฒ์ผ๋ก ๋ฐ๋ณต์ ์ธ ์ฝ๋๋ฅผ ํ๋์ ๋ชจ๋๋ก ๋ฌถ์ด ์ค๊ณํ๊ณ ๊ฐ๋ฐํ๋ ๋ฐฉ์์ ์๋ฏธํจ |
| Target | Aspect๊ฐ ์ ์ฉ๋๋ ๋์์ ์๋ฏธํจ. (Aspect๊ฐ ์ ์ฉ๋ ๊ณณ) |
| JoinPoint | Aspect๊ฐ ์ ์ฉ๋ ์คํ ์ง์ , ์ ์ฉ๋ ์์น๋ฅผ ์๋ฏธํจ |
| Advice | ์ค์ง์ ์ผ๋ก ์ด๋ ํ ์์
์ ํด์ผํ๋์ง์ ๋ํ ๋ถ๊ฐ๊ฐ๋ฅ์ ๋ด์ ๊ตฌํ์ฒด๋ฅผ ์๋ฏธํจ - Before Advice : JoinPoint ์ด์ ์ ์คํ - After Advice : JoinPoint ์ดํ์ ์คํ - Around Advice : JoinPoint๋ฅผ ๊ฐ์ธ ๋ค์ํ ์์ ์ ์ ์ฉํ์ฌ ์คํํ ์ ์์ |
| PointCut | Aspect๋ฅผ ์ด๋์ ์ ์ฉํ ์ง๋ฅผ ๊ฒฐ์ ํ๋ ํํ์, JoinPoint์ ์์ธํ ์คํ์ ์ ์ํ ๊ฒ |
| Proxy | Target ๊ฐ์ฒด๋ฅผ ๊ฐ์ธ Aspect์ ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ ์ฉํ๋ ์ค๊ฐ ๊ฐ์ฒด. Target์ ๋ค์ด์ค๋ ์์ฒญ์ ๋์ ๋ฐ์์ฃผ๋ Wrapping ์ค๋ธ์ ํธ์ |
AOP ํน์ง
- Proxy ํจํด ๊ธฐ๋ฐ์ AOP ๊ตฌํ์ฒด๋ก, Proxy ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ์ด์ ๋ ์ ๊ทผ์ ์ ์ดํ๊ณ ๋ถ๊ฐ๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ธฐ ์ํด์ ์ด๋ค.
- ์คํ๋ง bean์๋ง AOP๋ฅผ ์ ์ฉํ ์ ์๋ค.
- ๋ฉ์๋ ํธ์ถ ์ , ํ, ์์ธ ๋ฐ์ ๋ฑ ๋ค์ํ ์ง์ ์์ ์ํ๋ ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์๋ค.
- ๊ฐ์ฒด๋ค ๊ฐ ๊ด๊ณ ๋ณต์ก๋ ์ฆ๊ฐ๋ฅผ ํด์ํ๊ธฐ ์ํ ํด๊ฒฐ์ฑ ์ ์ง์ํ๋ ๊ฒ์ด AOP ์ฌ์ฉ์ ๋ชฉ์ ์ด๋ค.
AOP์ ํ์์ฑ(์ฌ์ฉํด์ผ ํ๋ ์ด์ )
AOP์ ํ์์ฑ์ ๋ณด๋ค ์ฝ๊ฒ ์ดํดํ๊ธฐ ์ํด ๋ค์์ ์์ ๋ฅผ ๊ฐ์ด ์ดํด๋ณด์.
(์ง์ ์น๊ฑฐ๋ผ ์์๊ฐ ์ ํํ์ง ์์ ์๋ ์๋ค. ์ด๋ณด๊ฐ๋ฐ์๋ผ ์ํด๋ถํ๋๋ฆผ๋๋ค,, ํ๋ฆฐ๊ฑฐ ์์ ๋งํด์ฃผ์ธ์,,)
public class Test {
private static final int A = 5;
private static final int B = 10;
public static void main(String[] args) {
calculate01();
calculate02();
}
public static void calculate01() {
int result = A+B;
System.out.println(result);
}
public static void calculate02() {
int result = A-B;
System.out.println(result);
}
}
์ ์์ ๋ ์ถ๋ ฅ๋ฌธ์ด ๋ฐ๋ณต๋๊ณ ์๋ค๋ ์ (์ง๊ธ์ ๋น๋ก ๋ ๋ฒ ๋ฐ๋ณต๋์์ง๋ง ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ฉด ๊ต์ฅํ ๋ณต์กํด ์ง ๊ฒ์ด๋ค.)์์ ๋นํจ์จ์ ์ด๊ณ ์ฌ์ฌ์ฉ์ฑ ๋ฉด์์๋ ํน์ ํด๋์ค์ ํน์ ํจ์์ ๊ธฐ๋ฅ์ ํ์ฅํ๊ธฐ๊ฐ ์ด๋ ต๋ค.
์ฌ์ฉ์๊ฐ ๋น์ง๋์ค ๋ก์ง์์ ํน์ ํ์๋ฅผ ํ๊ณ ์ถ์ ๋๊ฐ ์๋๋ฐ, ์ด๋ฌํ ํ์์ ๋์(Target)์ ํด๋์ค or ๋ฉ์๋๋ก ํน์ ์ง์ ์๊ฐ ์๋ค. ์ด๋ฌํ ํน์ ํ์๋ค์ ํ๊ตฐ๋ฐ์ ๋ชจ์ ๋ชจ๋ ํํ๋ก ๋ถ๋ฆฌํ๊ฒ ๋๋ฉด ๋ณ๋๋ก ๊ด๋ฆฌํ๊ธฐ๊ฐ ์ฉ์ดํด์ง๋ฉฐ ๊ฐ๋ ์ฑ๊ณผ ์ฌ์ฌ์ฉ์ฑ ์ธก๋ฉด์์๋ ํฐ ๋์์ด ๋๋ค.
์ฆ, AOP๋ ์ฝ๋์ ์ค๋ณต์ ๊ฐ์์ํค๊ณ , ์ฌ์ฌ์ฉ์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ๋์ด๊ธฐ ์ํด ์ฌ์ฉํด ์ฃผ๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค.
AOP ์ฌ์ฉํ๊ธฐ
Spring์์ AOP๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๋จผ์ ์์กด์ฑ์ ์ฃผ์ ํด ์ฃผ์ด์ผ ํ๋ค.
- pom.xml์ ์ถ๊ฐ
(AspectJ๋ ์์ ํ AOP๋ฅผ ์ ๊ณตํ๋ ๊ฒ์ด ๋ชฉ์ ์ธ AOP๊ธฐ์ ์ด๋ค. ๋ฐ๋ผ์ ๋ ๋ณต์กํ๋ฉฐ ๋ชจ๋ ๊ฐ์ฒด์ ์ ์ฉ์ด ๊ฐ๋ฅํ๋ค.
๋ฐ๋ฉด Spring AOP๋ ์คํ๋ง ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ Bean์๋ง ์ ์ฉ ๊ฐ๋ฅ, ์ ํ์ ์ผ๋ก ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.)
<!-- spring AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- AspectJ weaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
- root-context.xml์ ์ถ๊ฐ
<!-- @Aspect ๋ฐ ํ์ ์ด๋
ธํ
์ด์
ํ์ฑํ๋ฅผ ์ํด ์ถ๊ฐ -->
<aop:aspectj-autoproxy/>
<!-- @Aspect ์ด๋
ธํ
์ด์
scan ํ๊ธฐ์ํด ์ถ๊ฐ -->
<context:component-scan base-package="com.kh.app">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
[ AOP ์ฌ์ฉํด๋ณด๊ธฐ โผ ]
Step1. ์ธ์ ์คํ์ํฌ์ง ์ค์
- before : ํ๊ฒ์คํ ์ด์ ์ ๋์
- after : ํ๊ฒ์คํ ์ดํ์ ๋์
- afterThrowing : ํ๊ฒ ์คํ ์ดํ, ์๋ฌ ๋ฐ์ ์ ๋์
- afterReturning : ํ๊ฒ ์คํ ์ดํ, ์๋ฌ๊ฐ ์์ ๋ ๋์
- around : ํ๊ฒ ์คํ ์์ ์ง์ ๊ฐ๋ฅ
Step2. Target ์ค์ ํ๊ธฐ
@Around("execution(public void test.aop.TestAop.test())")
→ public์ ๋ฉ์๋์ ์ ๊ทผ ์ ์ด์, void๋ ๋ฉ์๋์ ๋ฐํ ํ์ , test.aop.TestAop๋ ํด๋์ค ์ด๋ฆ, test()๋ ๋ฉ์๋ ์ด๋ฆ์ ๋ํ๋ธ๋ค. ๋ฐ๋ผ์ ์ด ํํ์์ test.aop.TestAop ํด๋์ค์ test ๋ฉ์๋์ ๋ํ Pointcut์ ์ ์ํ ๊ฒ์ด๋ค.
Step3. AOP ํ์ฉํด์ ์๊ฐ ์ธก์ ํ๊ธฐ
@Around("execution(public * test.aop.TestAop.*(..))")
public Object m01(ProceedingJoinPoint jp) throws Throwable {
long start = System.currentTimeMillis();
Object o = jp.proceed();
long end = System.currentTimeMillis();
long time = end - start;
log.debug("{} ๋ฉ์๋ ์คํ์๊ฐ : {}ms", jp.getSignature(), time);
return o;
}
→ PointCut ์ค๋ช :
- public * test.aop.TestAop.*(..)์ ๋ฉ์๋ ์๊ทธ๋์ฒ๋ฅผ ๋ํ๋ด๋ฉฐ, public์ ๋ฉ์๋์ ์ ๊ทผ ์ ์ด์, *๋ ๋ฉ์๋์ ๋ฐํ ํ์ ์ ์๊ด์๋ ๋ชจ๋ ๋ฐํ ํ์ , test.aop.TestAop๋ ํด๋์ค ์ด๋ฆ, *๋ ๋ชจ๋ ๋ฉ์๋ ์ด๋ฆ์ ๋ํ๋ธ๋ค.
- (..)๋ ๋ชจ๋ ์ธ์๋ฅผ ๋ํ๋ธ๋ค. ๋ฐ๋ผ์ ํด๋น ํํ์์ test.aop.TestAop ํด๋์ค์ ๋ชจ๋ public ๋ฉ์๋์ ๋ํ Pointcut์ ์ ์ํ๋ค.
Reference
https://engkimbs.tistory.com/entry/%EC%8A%A4%ED%94%84%EB%A7%81AOP
[Spring] ์คํ๋ง AOP (Spring AOP) ์ด์ ๋ฆฌ : ๊ฐ๋ , ํ๋ก์ ๊ธฐ๋ฐ AOP, @AOP
| ์คํ๋ง AOP ( Aspect Oriented Programming ) AOP๋ Aspect Oriented Programming์ ์ฝ์๋ก ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ด๋ผ๊ณ ๋ถ๋ฆฐ๋ค. ๊ด์ ์งํฅ์ ์ฝ๊ฒ ๋งํด ์ด๋ค ๋ก์ง์ ๊ธฐ์ค์ผ๋ก ํต์ฌ์ ์ธ ๊ด์ , ๋ถ๊ฐ์ ์ธ ๊ด์ ์ผ๋ก
engkimbs.tistory.com
https://logical-code.tistory.com/118
Spring AOP์ AspectJ ๋น๊ตํ๊ธฐ
Thanks to @ใ ใ ใ ๋ ๋๋ถ์ 3-5 ์ฒซ๋ฒ์งธ ๋ฌธ์ฅ์ ์คํ๋ฅผ ์์ ํ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! (๋ ๊ฐํธํฉ๋๋ค๋ค. โข ๋ ๊ฐํธํฉ๋๋ค.) @๊น์ฑ์ ๋ ๋๋ถ์ 3-2. Weaving์ ์คํ๋ฅผ ์์ ํ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! (์ปดํ์ผ
logical-code.tistory.com
Spring - AOP ๊ธฐ๋ณธ๊ฐ๋ ๋ฐ ์ฃผ์ ๊ธฐ๋ฅ, ์์ ์ ํจ๊ป ์ดํดํ๊ธฐ
What is AOP? Spring์ ํต์ฌ๊ธฐ๋ฅ์ธ AOP(Aspect Oriented Programming)์ ์ดํดํ๊ธฐ ์ํด ๋ค์์ ์์ ๋ฅผ ์ดํด๋ณด์. (์์๋ก ๋ค์์ง๋ง ์ค์ ์ด์ํ๊ฒฝ์์ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ๋ง์ด ๋ง์ฃผ์น๊ณค ํ๋ค.) public class Foo { pub
youngwonhan-family.tistory.com
'๐ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring] 05.์คํ๋ง ORM๊ณผ JPA - Hibernate (11) | 2025.07.24 |
|---|---|
| [Spring] 04.MVC ๋? - MVC ๊ตฌ์กฐ ์ดํด (6) | 2025.07.22 |
| [Spring] 03.DI์ IoC ๊ฐ๋ ๋ฐ ์ด์ ๋ฆฌ (1) | 2024.04.15 |
| [Spring] 01.@Autowired,@Resource,@Inject ์ฐจ์ด (6) | 2023.11.20 |