Light Red Pointer [Spring] 02.AOP๊ฐœ๋…, AspectํŠน์ง• ์ด์ •๋ฆฌ
 

[Spring] 02.AOP๊ฐœ๋…, AspectํŠน์ง• ์ด์ •๋ฆฌ


 

 

 

์„œ๋ก 

๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” 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

https://youngwonhan-family.tistory.com/entry/AOP-%EA%B8%B0%EB%B3%B8-%EB%B0%8F-%EC%A3%BC%EC%9A%94-%EA%B8%B0%EB%8A%A5-%EC%98%88%EC%A0%9C%EC%99%80-%ED%95%A8%EA%BB%98-%EC%9D%B4%ED%95%B4%ED%95%98%EA%B8%B0

 

Spring - AOP ๊ธฐ๋ณธ๊ฐœ๋… ๋ฐ ์ฃผ์š” ๊ธฐ๋Šฅ, ์˜ˆ์ œ์™€ ํ•จ๊ป˜ ์ดํ•ดํ•˜๊ธฐ

What is AOP? Spring์˜ ํ•ต์‹ฌ๊ธฐ๋Šฅ์ธ AOP(Aspect Oriented Programming)์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์˜ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž. (์˜ˆ์‹œ๋กœ ๋“ค์—ˆ์ง€๋งŒ ์‹ค์ œ์šด์˜ํ™˜๊ฒฝ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ๋งˆ์ฃผ์น˜๊ณค ํ•œ๋‹ค.) public class Foo { pub

youngwonhan-family.tistory.com