使用Cglib动态dialing开发运行时抛异常org.objectweb.asm.ClassWriter

开始时使用的jar包:

asm-2.2.3.jar

cglib-2.2.jar

代码如下:

package proxy.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;






/**
 * Cglib动态代理:可以不用像JDK的对象使用动态代理必须要实现一个或多个接口
 * @author frank
 * 2015-4-28上午11:47:58
 */
public class CglibDemo {
	public static void main(String[] args) {
		Test tset=(Test) new MyCglib().getInstance(new Test());
		tset.start();
	}
}	

class  Test{
	public void start(){
		System.out.println("start...");
	}
	public void stop(){
		System.out.println("stop...");
	}
}
 class MyCglib implements MethodInterceptor{
	 public Object getInstance(Object o){
		 Enhancer e=new Enhancer();
		 e.setSuperclass(o.getClass());
		 e.setCallback(this);
		 return e.create();
	 }
	@Override
	public Object intercept(Object object, Method method, Object[] objects,MethodProxy proxy) throws Throwable {
		System.out.println("->");
		proxy.invokeSuper(object, objects);
		System.out.println("-<");
		return null;
	}

	
	
}
一运行:

Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
	at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47)
	at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)
	at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
	at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
	at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
	at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
	at proxy.cglib.MyCglib.getInstance(CglibDemo.java:36)
	at proxy.cglib.CglibDemo.main(CglibDemo.java:21)

经过资料查找发现是jar包问题:

把cglib-2.2.jar换成cglib-nodep-2.1_3.jar完美解决问题。

运行结果:

->
start...
-<


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。