๋กœ์ผ“๐Ÿพ
article thumbnail

 

์Šคํ”„๋ง์˜ ํŠน์ง•์„ 5๊ฐ€์ง€๋กœ ๋‚˜๋ˆˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

  • ๊ฒฝ๋Ÿ‰ ์ปจํ…Œ์ด๋„ˆ
  • DI(Dependency Injection, ์˜์กด์„ฑ ์ฃผ์ž…)
  • IoC(์ œ์–ด์˜ ์—ญํ๋ฆ„, Inversion of Control)
  • AOP(๊ด€์ ์ง€ํ–ฅํ”„๋กœ๊ทธ๋ž˜๋ฐ, Aspect oriented Programming)
  • POJO ๋ฐฉ์‹์˜ ํ”„๋ ˆ์ž„์›Œํฌ

๊ทธ ์ค‘ ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” AOP(๊ด€์ ์ง€ํ–ฅํ”„๋กœ๊ทธ๋ž˜๋ฐ)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 


l AOP๋Š” ์™œ ํ•„์š”ํ• ๊นŒ?

 

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๋‹ค ๋ณด๋ฉด ํ•ดํ‚น์„ ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•œ ๋ณด์•ˆ ๊ธฐ๋Šฅ, ์‚ฌ์šฉ์ž์˜ ์ ‘์† ๋‚ด์—ญ์„ ์œ„ํ•œ ๋กœ๊น… ๊ธฐ๋Šฅ ๊ทธ์™ธ์— ํŠธ๋žœ์žญ์…˜, ์ด๋ฉ”์ผ ๋ฐœ์†ก ๊ธฐ๋Šฅ ๋“ฑ ์ˆ˜๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์˜ ๊ณตํ†ต์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? ๋ชจ๋‘ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์ถ•ํ•  ๋•Œ ๊ณตํ†ต์ ์œผ๋กœ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋“ค ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ํด๋ž˜์Šค๋“ค์ด ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. 

 

ํ•˜์ง€๋งŒ ๊ฐ ํด๋ž˜์Šค๋งˆ๋‹ค ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ์—๋Š” ์‚ฌ์‹ค ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ–‰์—ฌ ๋ณด์•ˆ ๊ธฐ๋Šฅ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ๋ณด์•ˆ ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ์ฐพ์•„ ๋ณด์•ˆ ๊ธฐ๋Šฅ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

 

์ƒ๊ฐ๋งŒ ํ•ด๋„ ๋”์ฐํ•ฉ๋‹ˆ๋‹ค

 

์ด๋Ÿฌํ•œ ๋ถˆํŽธํ•จ์„ ์—†์• ๊ธฐ ์œ„ํ•ด ์Šคํ”„๋ง์€ AOP ๋ผ๋Š” ๊ฐœ๋…์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ DI์™€ IoC์ฒ˜๋Ÿผ AOP๋„ ๊ฐœ๋ฐœ์ž๋“ค์˜ ๋ถˆํŽธํ•จ์„ ํ•ด์†Œํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋“ฑ์žฅํ•œ ๊ฒƒ์ด์ง€์š”.

 


l AOP๋Š” ๋ฌด์—‡์ผ๊นŒ?

 

AOP๋Š” ๋ฉ”์„œ๋“œ ์•ˆ์˜ ์ฃผ๊ธฐ๋Šฅ๊ณผ ๋ณด์กฐ ๊ธฐ๋Šฅ์„ ๋ถ„๋ฆฌํ•œ ํ›„ ์„ ํƒ์ ์œผ๋กœ ๋ฉ”์„œ๋“œ์— ์ ์šฉํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฐœ๋…์œผ๋กœ ์ •์˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. AOP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํด๋ž˜์Šค์— ํฉ์–ด์ ธ ์žˆ๋Š” ๊ณตํ†ต๊ธฐ๋Šฅ๋“ค์„ ํ•˜๋‚˜์˜ ์žฅ์†Œ์— ๋ชจ์•„์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

AOP๋ฅผ ์ ์šฉํ•œ LMS ์‹œ์Šคํ…œ์˜ ๊ตฌ์กฐ๋„

 

 

์šฉ์–ด ์„ค๋ช…
aspect ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ณด์กฐ ๊ธฐ๋Šฅ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
โ˜…adviceโ˜… aspect์˜ ์‹ค์ œ ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ณด์กฐ ๊ธฐ๋Šฅ์˜ ์‹ค์ฒด ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
joinpoint advice๋ฅผ ์ ์šฉํ•˜๋Š” ์ง€์ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์Šคํ”„๋ง์€ method ๊ฒฐํ•ฉ์ ๋งŒ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
pointcut advice๊ฐ€ ์ ์šฉ๋˜๋Š” ๋Œ€์ƒ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€์ด๋ฆ„/ํด๋ž˜์Šค์ด๋ฆ„/๋ฉ”์„œ๋“œ์ด๋ฆ„์„ ์ •๊ทœ์‹์œผ๋กœ ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
โ˜…targetโ˜… advcie๊ฐ€ ์ ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
weaving advice๋ฅผ ์ฃผ๊ธฐ๋Šฅ์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 


l AOP๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹ค์Šต

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
   "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
   <bean id="calcTarget" class="com.spring.ex03.Caculator">
   	<property name="add">
   		<value>๋”ํ•˜๊ธฐ</value>
   	</property>
   </bean>
   <bean id="logAdvice" class="com.spring.ex03.LoggingAdvice" />
   
   <bean id="proxyCal"
          class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="target" ref="calcTarget"/>
      <property name="interceptorNames">
         <list>
            <value>logAdvice</value>
         </list>
      </property>
   </bean>
</beans>

 

๋นˆ์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์„ค๋ช…ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

(๋นˆ์ด๋ž€? ์Šคํ”„๋ง์—์„œ DI๋ฅผ ์œ„ํ•˜์—ฌ ํด๋ž˜์Šค์— ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…์‹œ์ผœ์ฃผ๋Š”๋ฐ ์ด ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒด๋ฅผ ๋นˆ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.)

 

calTarget ๋นˆ

proxyCal ๋นˆ์—์„œ name="target" ํ•จ์œผ๋กœ์จ advice๊ฐ€ ์ ์šฉ๋  ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ ์šฉ๋  ํด๋ž˜์Šค๋Š” bean class์ธ com.spring.ex03.Caculator์ž…๋‹ˆ๋‹ค.

 

property๋ฅผ ์„ค์ •ํ•ด์ฃผ์–ด Caculator์˜ setter์—์„œ ๋นˆ ๊ฐ์ฒด๋ฅผ ๋ฐ›์„์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

logAdvice ๋นˆ

๋กœ๊น… ๊ธฐ๋Šฅ์„ ํ•˜๋Š” advice ๋นˆ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

 

proxyCal ๋นˆ

์ฒซ๋ฒˆ์งธ property๋Š” ref="calcTarget"๋ฅผ ํ•˜์—ฌ Caculator ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ์—ˆ๊ณ , ์ด๋ฅผ ํƒ€๊นƒ ํด๋ž˜์Šค๋กœ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

(ํƒ€๊นƒ ํด๋ž˜์Šค๋Š” advice๋ฅผ ์ ์šฉํ•ด์ค„ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.)

 

๋‘๋ฒˆ์งธ property๋Š” logAdvice๋ฅผ advice ๋นˆ์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ํƒ€๊นƒ ํด๋ž˜์Šค ํ˜ธ์ถœ์‹œ logAdvice๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

package com.spring.ex03;

public class Caculator {
	private String add;
	
	public void setAdd(String add) {
		this.add = add;
	}
	
	public void add(int x, int y) {
		int result=x+y;
		System.out.println("bean์˜ Value : " + add);
		System.out.println("๊ฒฐ๊ณผ:"+ result);
	}

	public void subtract(int x, int y) {
		int result=x - y;
		System.out.println("๊ฒฐ๊ณผ:"+ result);
	}

	public void multiply(int x, int y) {
		int result=x * y;
		System.out.println("๊ฒฐ๊ณผ:"+ result);
	}

	public void divide(int x, int y) {
		int result=x / y;
		System.out.println("๊ฒฐ๊ณผ:"+ result);
	}
}

 

setter ํ•จ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ bean์˜ property ์ด๋ฆ„์œผ๋กœ ํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ name="test" ์˜€๋‹ค๋ฉด setter ํ•จ์ˆ˜์˜ ํ•จ์ˆ˜๋ช…์€ setTest๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

package com.spring.ex03;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LoggingAdvice implements MethodInterceptor {
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("[๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „ : LogginAdvice");
		System.out.println(invocation.getMethod() + "๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „");

		Object object = invocation.proceed();

		System.out.println("[๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ํ›„ : loggingAdvice");
		System.out.println(invocation.getMethod() + "๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ํ›„");
		return object;
	}
}

 

invoke ํ•จ์ˆ˜๋Š” invocation.proceed ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „์— ์ˆ˜ํ–‰ํ•˜๋Š” ๊ตฌ๋ฌธ๊ณผ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ํ›ˆ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ตฌ๋ฌธ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ฝ˜์†”์— ๋กœ๊ทธ๋ฅผ ์ฐ์–ด์ค๋‹ˆ๋‹ค. ์•„๋งˆ ๋ฐ‘์—์„œ ์‚ฌ์ง„์œผ๋กœ ๋ณด์‹œ๋ฉด ์ดํ•ด๊ฐ€ ๋” ๋น ๋ฅด์‹ค ๊ฒ๋‹ˆ๋‹ค.

 

package com.spring.ex03;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CalcTest {
	   public static void main(String[] args){
	      ApplicationContext context=new ClassPathXmlApplicationContext("AOPTest.xml");
	      Caculator cal=(Caculator)context.getBean("proxyCal");

	      cal.add(100,20);
	 
	   }
	}

 

ApplicationContext ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋นˆ ์„ค์ •์ด ๋˜์–ด์žˆ๋Š” xml ํŒŒ์ผ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  getBean์„ ์ด์šฉํ•˜์—ฌ ๋นˆ ๊ฐ์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. 

 

cal.add(100,20)๋ฅผ ํ•ด์คŒ์œผ๋กœ์จ ๋นˆ ๊ฐ์ฒด๋ฅผ ์ž˜ ๋ถˆ๋Ÿฌ์™”๋Š”์ง€ ํ™•์ธํ•ด์ค๋‹ˆ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

์ฝ˜์†” ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์ „, ํด๋ž˜์Šค ๋‚ด์šฉ, ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ›„ ์ด๋ ‡๊ฒŒ ๋กœ๊ทธ๋‚ด์—ญ์ด ์žˆ๋Š” ๊ฑธ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


l ์š”์•ฝ

AOP ๋˜ํ•œ DI, IoC ์ฒ˜๋Ÿผ ๊ฒฐ๊ตญ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ๋ณด๋‹ค ํŽธํ•˜๊ฒŒ ํ•ด์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ ์ƒ๊ธด ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. AOP๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด๋‹ค ์‰ฝ๊ฒŒ ๊ณตํ†ต ๊ธฐ๋Šฅ๋“ค์„ ํ•œ ๊ณณ์— ๋ชจ์•„ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • target : advice๋ฅผ ์ ์šฉํ•ด์ค„ ํด๋ž˜์Šค
  • advice : ๋ณด์กฐ ๊ธฐ๋Šฅ(aspect)์„ ๊ตฌํ˜„ํ•œ ์‹ค์ œ ํด๋ž˜์Šค

 

 

์ฐธ๊ณ ๋„์„œ

[์ž๋ฐ” ์›น์„ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ (๊ธธ๋ฒ—)]

[๋ฐฐ์›Œ์„œ ๋ฐ”๋กœ ์“ฐ๋Š” ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ(ํ•œ๋น›๋ฏธ๋””์–ด)]

 

 

 

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์งˆ๋ฌธ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.

 

"๋‚œ ๋ฐ˜๋“œ์‹œ ๋ฐฑ์—”๋“œ ์™•์ด ๋ ๊ฑฐ์•ผ"

profile on loading

Loading...