※ AOP 구현하는 과정
1. Advice 클래스를 작성
2. 설정 파일에 Pointcut를 설정
3. 설정 파일에 Advice와 Pointcut을 묶어 놓은 Advisor를 설정
4. 설정 파일에 ProxyFactoryBean 클래스를 이용하여 대상 객체에 Advisor를 적용
5. 사용
MessageBean.java
package aop; public interface MessageBean { |
MessageBeanImpl.java
package aop; public class MessageBeanImpl implements MessageBean { } |
LoggingAdvice.java
package aop; import org.aopalliance.intercept.MethodInterceptor; public class LoggingAdvice implements MethodInterceptor { @Override } |
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; public class AopTest { /** } |
결과
[LOG] METHOD : sayHello is calling... Hello SPRING AOP! [LOG] METHOD : sayHello was called. [LOG] 처리시간 : 5.0초 |
'프로그램 > 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 |