wildcatsの日記

赤羽在住でIT関係の会社の社長やってます。

SpringのAOPでJavassistを使えるようにしてみる。

このエントリに触発されてやっちまった。
Springは2.0.2でJavassistは3.4


public class Hoge implements IHoge {

public void getMessage() {
System.out.println("Hoge");
}

}


public class HogeInterceptor implements MethodInterceptor {

public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("before");
Object result = invocation.proceed();
System.out.println("after");
return result;
}
}


ProxyFactory factory = new ProxyFactory(new Hoge());
factory.addAdvice(new HogeInterceptor());
factory.setAopProxyFactory(new JavassistAopProxyFactory());
factory.setOptimize(true);
factory.setProxyTargetClass(true);
IHoge hoge = (IHoge) factory.getProxy();
hoge.getMessage();
System.out.println(hoge.getClass().getName());
実行結果

2007/03/19 23:08:29 org.springframework.aop.framework.DefaultAopProxyFactory
情報: CGLIB2 not available: proxyTargetClass feature disabled
2007/03/19 23:08:29 org.springframework.core.CollectionFactory
情報: JDK 1.4+ collections available
before
Hoge
after
com.pragmaticengine.spring.aop.framework.Hoge_$$_javassist_0
公開しようか考え中