스트럿츠2에 한창 빠져있을때 이 책을 샀는데,, 사실 강컴 서평에 평점을 낮게 주었드랬다. 당시에 sturt2+spring2.5 연동하는 예제를 보고 싶었고.. 마침 이 책의 목차에 있었기에 주문했었지만 struts1이었던 것. 나는 이 책이 필요없다고 썼던 것으로 기억한다. 억울해, 괜히샀어, 괜히샀어...
하지만 aop를 공부하면서 간단명료하면서도 따라하기 쉬운 예제를 수록한 것은 필시 프로스프링2.5 , 스프링인액션2, 최볌균의 스프링2.5의 그것을 능가한 것이었다. 그리고 실무적이라는 것. 이론 설명을 차치하고 당장 써먹을 수 있는 소스를 알려주었다.

하지만 yes24서평을 보니 일본 원서를 카피해서 만든 책이라능... 카피!카피!

따로 설명 필요없이 아래 소스만 봐도 이해를 쉽게 할 수 있다. aroundAdvice!!

  public interface MessageBean {
void sayHello();
}
=======================================================================
public class MessageImplBean implements MessageBean{
private String name;
public void sayHello(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("헬로 :"+name);
}
public void setName(String name) {
this.name = name;
}
}

public class LoggingAdvice implements MethodInterceptor{
//CGLIB 라이브러리 이용. 프록시팩토리 클래스 이용. applicationContext.xml을 쓰지 않을 경우
// public static void main(String[] args){
// MessageBean target = new MessageImplBean();
//
// ProxyFactory pf = new ProxyFactory();
// pf.addAdvice(new LoggingAdvice());
// pf.setTarget(target);
//
// MessageBean proxy = (MessageBean)pf.getProxy();
//
// proxy.sayHello();
// }
public Object invoke(MethodInvocation invocation) throws Throwable{
String methodName = invocation.getMethod().getName();
StopWatch sw = new StopWatch();
sw.start(methodName);
System.out.println("로그 메소드:"+methodName+" 이즈 콜링");
Object obj = invocation.proceed();
sw.stop();
System.out.println("로그메소드:"+methodName+"불렀다");
System.out.println("처리시간:"+sw.getTotalTimeMillis()/1000+"초");
return obj;
}
}
=======================================================================
<?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-2.5.xsd">
<!--target객체 정의-->
<bean id="targetBean" class="chapter03.MessageImplBean">
<property name="name">
<value>최동환</value>
</property>
</bean>
<!--advice정의-->
<bean id="loggingAdvice" class="chapter03.LoggingAdvice" />

<!--pointcut 정의-->
<bean id="ptcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern">
<value>.*sayHello.*</value>
</property>
</bean>

<!--Advisor정의-->
<bean id="helloAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice">
<ref local="loggingAdvice"/>
</property>
<property name="pointcut">
<ref bean="ptcut" />
</property>
</bean>
<!--프록시팩토리빈을 이용한 advice적용-->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="targetBean"/>
</property>
<property name="interceptorNames">
<list>
<value>helloAdvisor</value>
</list>
</property>
</bean>
</beans>
=======================================================================
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class HelloApp {

public static void main(String[] args){
Resource res = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
MessageBean bean = (MessageBean)factory.getBean("proxy");
bean.sayHello();
}
}
=======================================================================

작성순서
 target setting -> advice -> Pointcut -> Advisor -> proxy setting

AND