Can singleton be implemented in OSGi environment

2019-07-26 23:53发布

问题:

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?

回答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.



标签: java osgi