public class MessageWriter {
public void writerMessage() {
System.out.println("i'm working");
}
}
public class MessageDecorator implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("before...");
Object retVal = methodInvocation.proceed();
System.out.println("after...");
return retVal;
}
}
public class MessageWeaver {
public static void main(String[] args) {
// joint point
MessageWriter target = new MessageWriter();
// create the proxy
ProxyFactory pf = new ProxyFactory();
// add advice
pf.addAdvice(new MessageDecorator());
pf.setTarget(target);
MessageWriter proxy = (MessageWriter) pf.getProxy();
target.writerMessage();
System.out.println("------------");
proxy.writerMessage();
}
}
在测试就项目时使用上述测试代码如下jars⑩
spring2.5.6.jar
commons-logging-1.2.jar
cglib-full-2.0.2.jar
aspectjweaver-1.8.0.RELEASE.jar
测试类报如下错误
Exception in thread "main" java.lang.NoSuchMethodError: net.sf.cglib.proxy.Enhancer.setInterceptDuringConstruction(Z)V
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:182)
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:145)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:97)
at com.ycx.ch5.section3.MessageWeaver.main(MessageWeaver.java:17)
经过分析使用cglib-2.1_3.jar和asm-1.5.3.jar代替cglib-full-2.0.2.jar
问题解决