프로 스프링2.5에 있는 책의 예제다.
이제 막 aop를 공부하는 시점에서 기초를 탄탄히 하기 위해 예제를 하나씩 따라하고 있다.
헌데 프록시에러 발생. 

Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interface

CGLIB를 패스에 추가하라는 것이다. 물론 간단하게 jar파일 구해서 추가해 주면 되지만, 이거 없이도 돌아갈 수 있나를 찾아보았다. 
결론은 jdk proxy를 쓰면 된다는 것. 하지만 "JDK Proxy는 인터페이스에 대한 Proxy만을 지원하며, 클래스에 대한 Proxy를 지원할 수 없다는 것이 큰 단점이다"라고 박재성님의 위키에서 jdk proxy에 대한 단점들을 읽고 일단 테스트용도로 jdk proxy를 써서 구현하기로 하였다.

public interface Message {
public void writeMessage();
}
==========================================================
public class MessageWriter implements Message{

public void writeMessage(){
System.out.println("World!!");
}
}
===========================================================
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

public class SimpleBeforeAdvice implements MethodBeforeAdvice{
public static void main(String args[]){
Message target = new MessageWriter();
//make proxy
ProxyFactory pf = new ProxyFactory();
pf.addAdvice(new SimpleBeforeAdvice());
pf.setTarget(target);
pf.setInterfaces(new Class[]{Message.class});
Message proxy = (Message) pf.getProxy();
proxy.writeMessage();
}
public void before(Method method, Object[] args, Object target) throws Throwable{
System.out.println("before method:"+method.getName());
}

}
===============================================================

jdk proxy를 쓰면 프록시팩토리 객체에 setInterfaces()를 추가해 주어야 한다. 그리고 반드시 타겟클래스의 인터페이를 만들어 해당 인터페이스를 인자로 넘겨주어야 한다는 것!!!
성능 저하도 단점이라고 한다.

CGLIB를 추가하자..
AND