在osgi环境中(我用的是felix),不同的bundle都有自己独立的classloader,当在一个bundle中复制另外一个bundle加载的对象时,会报类型不匹配的错误。这一问题的解决,需要利用反射的方法,进行处理。
transloader是一个在不同的classloader间进行对象复制和对象转换的工具包,对于对象的复制,实现如下:
ObjectWrapper wrap = new ObjectWrapper(ObjectInstance,CloningStrategy.MAXIMAL);
Object newChromosome = (Object) wrap.cloneWith(ObjectInstance.getClass().getClassLoader());
但是,在osgi的标准设置下,会报一个找不到sun/reflect/ReflectionFactory的错误,这是因为,在osgi默认的jre导入package中,不包括sun.reflect包。需要在osgi的配置文件中,增加:
# To append packages to the default set of exported system packages,
# set this value.
org.osgi.framework.system.packages.extra=sun.reflect.*
# The following property makes specified packages from the class path
# available to all bundles. You should avoid using this property.
org.osgi.framework.bootdelegation=sun.*,com.sun.*,sun.reflect.*
把sun.reflect包导入到osgi的环境中。
在使用OSGi的felix环境中,通过引入transloader工具包解决不同bundle加载的对象复制时出现类型不匹配的问题。需在配置文件中添加额外的系统包导入,如sun.reflect包,确保所有bundle间对象的正常复制。
86万+

被折叠的 条评论
为什么被折叠?



