可以在OSGi环境中实现单例 [英] Can singleton be implemented in OSGi environment
问题描述
据我了解,osgi中的每个捆绑软件都有自己的类加载器.假设我们有三个束:1、2、3.捆绑软件1中有单例类.捆绑软件2加载单例并对其进行一些更改.据我了解,如果bundle 3加载了singleton,则其singleton的版本也会有所不同.因此,捆绑2中的Singleton!=捆绑3中的Singleton.
As far as I understand every bundle in osgi has its own classloader. Let's suppose we have three bundles:1,2,3. There is singleton class in bundle 1. Bundle 2 loads singleton and makes some changes in it. And as far as I understand, if bundle 3 loads singleton too its version of singleton is different. So Singleton in bundle 2 != singleton in bundle 3.
我的陈述正确吗?如果它们是正确的,那么它不是单例,是吗?
Are my statements right? If they are right, so it is not singleton, is it?
推荐答案
每个捆绑包确实都有自己的类加载器,但是这些类加载器是共享的",具体取决于清单import-package
声明.每个捆绑包类加载器维护一个私有映射表,该表告诉它哪个类加载器负责提供特定程序包中的类.
在您的情况下,当捆绑软件2加载单例时,它不会自行加载单例,而是向捆绑软件1类加载器询问该类.
It is true that each bundle has its own classloader, but the classloaders are "shared", depending on the Manifest import-package
declaration. Each bundle classloader maintain a private mapping table that tells it which classloader is responsible for providing classes from a particular package.
In your case, when bundle 2 loads the singleton, it does not load the Singleton by itself, it asks the bundle 1 classloader for the class.
这篇关于可以在OSGi环境中实现单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!