I use the asm to implements a proxy patterns.
for example:
the original class is test.service.Service
public class ProxyFactory{
public static Object generateProxy(Class<?> argument){
//do generate use asm.
//generate a proxyClass that extends argument
//and override the sayHello() method
//and than invoke System.out.println("anything") before we call super.sayHello()
Class<?> class = proxyClass;
return class.newInstance();
}
}
public class Service implements IService{
@Override
public List<String> sayHello(){
List<String> list = new ArrayList<String>();
list.add("stackoverflow user");
return list;
}
}
public class Action extends ActionSupport{
private IService service = ProxyFactory.generateProxy(Service.class);
}
When I request this action than will be an Exception:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
…
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoClassDefFoundError: test.service.Service
at java.lang.ClassLoader.defineClass1(Native Method)
…
… 40 more
Caused by: java.lang.ClassNotFoundException: test.service.Service
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
…
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
… 43 more
It’s seem like tomcat classloader problem because all above code can work if I run it as application could you anybody help me to solve it.
Thank
Solve by myself, actually , As I expected, the problem is caused by classloader.
this is before code:
and now i use Thread.currentThread().getContextClassLoader() to instead of
ClassLoader.getSystemClassLoader();
it mean we can use tomcat classloader to defined our class so than can found our web
application class “test.service.Service”