简体   繁体   English

OSGi-将捆绑软件的类添加到特定的类加载器

[英]OSGi - Adding bundle's classes to specific classloader

I have bundle A.jar installed in OSGi and I can get this bundle using BundleContext 我在OSGi中安装了捆绑包A.jar ,可以使用BundleContext获得捆绑包

Bundle bundleA = context.getBundle()

And a specific classloader B 和特定的类加载器B

How can I adding all class from bundleA to my classloader B 如何将bundleA所有类添加到我的类加载器B中

You can not do this at runtime. 您无法在运行时执行此操作。 You can add a Require-Bundle header to bundle B. Then the classloader of bundle B will know all classes of bundle A. This is not really recommended though. 您可以在包B中添加Require-Bundle标头。然后,包B的类加载器将知道包A的所有类。不过,实际上并不建议这样做。

So I would rather ask you what do you want to achieve with this? 因此,我想问您,您要实现什么目标? Perhaps there is another solution to your problem. 也许您的问题还有另一种解决方案。

Let me assume you are writing your own class loader and want to use Bundle A's class loader as its parent. 让我假设您正在编写自己的类加载器,并想将Bundle A的类加载器用作其父级。 You can get the class loader for Bundle A as follows: 您可以按如下方式获取捆绑软件A的类加载器:

ClassLoader clA = bundleA.adapt(BundleWiring.class).getClassLoader();

You can then use clA as the parent of a new ClassLoader instance for loading your classes which depend upon classes from A. 然后,您可以将clA用作新ClassLoader实例的父级,以加载依赖于A中类的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM