当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

对于不同级别classloader define的类的问题处理


问题描述:程序代码中,执行下列语句:Object mapperObj = Class.forName(mapperClassName).newInstance();MapperInterface mapper = (MapperInterface)mapperObj; 报ClassCastException。

bug fix:  1.分别取得运行环境下mapperObj和MapperInerface.class的 classloader:   mapperObj.getClass().getClassLoader() : sun.misc.Launcher$AppClassLoader    MapperInterface.class.getClass().getClassLoader() : WebContainerClassLoader2.由jvm classload 机制可知,jvm load class 分四个层次:  第一层为bootstrapclassloader : 主要负责load rt.jar等jvm必须的jar包中的类。  第二层为extclassloader : 主要负责load 被置于java.ext.dirs属性值所指路径(默认%JAVA_HOME%/lib/ext) 中所有的class.其实现类为sun.misc.Launcher$ExtClassLoader   第三层为systemclassloader : 负责load 被置于CLASSPATH路径中的类。 其实现类一般为sun.misc.Launcher$AppClassLoader   第四层为appclassloader : 由应用程序设计者继承ClassLoader并实现完成相应user-defined ClassLoader。用于根据应用程序需要加载并不是设计时就知道的类。

详细的load策略偶就不多写了,很多文章上都有,总之两句话: 当define一个类的时候,低层classloader会向上层询问是否已经define,有则直接拿来用;当load一个类的时候,同样低层向高层询问是否能find到,能就直接拿来用。

由此可知,由于原有系统原因, mapperObj被 systemclassloader define;而这里使用的接口是被appclassloader WebContainerClassLoader define的。所以会造成 ClassCastException错误。用instanceof也可发现mapperObj 确实不是 MapperInterface的实例。

由于原系统原因,无法通过改动其他代码完成更换mapperObj classload的动作,而运行到当前代码时,mapperObj 已经被define,所以无法通过forName方法的参数更改其class loader,后面的代码就无法调用其方法。

解决方法:采用类反射,换有Object定义的属性接 mapperObj;在下面的代码中,利用mapperObj.getClass().getInterface()方法判断是否其继承了 MapperInterface。 然后用反射调用其方法。例:

mapper = Class.forName(mapperClassName).newInstance();

Class[] tmpInterface = mapper.getClass().getInterfaces();   for(int i=0 ;i<tmpInterface.length ;i++){ if(tmpInterface[i].getName().equals("MapperInterface.class.getName()"))    flag = true;}

if(flag){  Class[] tc = new Class[2] ;  tc[0] = String.class;  tc[1] = HttpServletRequest.class;  Method mapperFunc = mapper.getClass().getMethod( "mapFunction", tc );  Object res = mapperFunc.invoke( mapper, new Object[] { event.getServletClassName(),req } );}


相关内容
赞助商链接