Can singleton be implemented in OSGi environment

2019-07-26 23:42发布

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?

标签: java osgi
1条回答
冷血范
2楼-- · 2019-07-27 00:15

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.

查看更多
登录 后发表回答