용어가 매우 중요함(반드시 기억)

※ AOP 구현하는 과정
1. Advice 클래스를 작성
2. 설정 파일에 Pointcut를 설정
3. 설정 파일에 Advice와 Pointcut을 묶어 놓은 Advisor를 설정
4. 설정 파일에 ProxyFactoryBean 클래스를 이용하여 대상 객체에 Advisor를 적용
5. 사용

MessageBean.java

package aop;

public interface MessageBean {
 void sayHello();
}


MessageBeanImpl.java

package aop;

public class MessageBeanImpl implements MessageBean {
 private String name;
 
 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public void sayHello() {
  // TODO Auto-generated method stub
  try{
   Thread.sleep(5000);
  }catch(InterruptedException ie){
   ie.printStackTrace();
  }
  
  System.out.println("Hello " + name + "!");
 }

}


LoggingAdvice.java

package aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;

public class LoggingAdvice implements MethodInterceptor {

 @Override
 public Object invoke(MethodInvocation invocation) throws Throwable {
  // TODO Auto-generated method stub
  
  String methodName = invocation.getMethod().getName();
  
  StopWatch sw = new StopWatch();
  sw.start(methodName);
  
  System.out.println("[LOG] METHOD : " + methodName + " is calling...");
  Object obj = invocation.proceed();
  
  sw.stop();
  System.out.println("[LOG] METHOD : " + methodName + " was called.");
  System.out.println("[LOG] 처리시간 : " + sw.getTotalTimeSeconds() + "초");
  
  return obj;
 }

}


applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="loggingAdvice" class="aop.LoggingAdvice"></bean>
 
 <bean id="helloAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
  <property name="advice" ref="loggingAdvice"/>
  <property name="pointcut">
   <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="pattern">
     <value>.*sayHello.*</value>
    </property>
   </bean>
  </property>
 </bean>
 
 <bean id="messageBean" class="aop.MessageBeanImpl">
  <property name="name" value="SPRING AOP"></property>
 </bean>
 
 <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="target" ref="messageBean"></property>
  <property name="interceptorNames" value="helloAdvisor"></property>
 </bean>

</beans>

AopTest.java

package aop;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class AopTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Resource resource = new ClassPathResource("applicationContext.xml");
  XmlBeanFactory factory = new XmlBeanFactory(resource);
  MessageBean messageBean = (MessageBean)factory.getBean("proxy");
  messageBean.sayHello();
 }

}


결과
[LOG] METHOD : sayHello is calling...
Hello SPRING AOP!
[LOG] METHOD : sayHello was called.
[LOG] 처리시간 : 5.0초



[참고자료] Spring 2.5 실무 프로그래밍(성윤정)

'프로그램 > Spring 2.5' 카테고리의 다른 글

HandlerMapping - 예제  (0) 2012.03.22
HandlerMapping  (0) 2012.03.22
AOP(Aspect Oriented Programming) - 관점 지향 프로그래밍  (0) 2012.03.19
MultiActionController  (0) 2012.03.10
SimpleFormController  (0) 2012.03.10

+ Recent posts