0

Is there any proper way to share objects between components in a single bundle?

Objects in this case are somewhat similar to Spring beans. So they are created outside and independently of components.

I've managed to do this by creating an additional "internal" component that is somewhat like a DI root. Unfortunately, that way, this "internal" component is exposed to other bundles. That is undesirable.

Oleg_Andreych
  • 73
  • 1
  • 9
  • This _problem_ is not related to OSGi at all. You can always create an object in one method and pass i to another - JVM will see/have references to this object only inside methods and method argument. However once you reference it from some field, you can always navigate to this object. If field is private, you can still use reflection - you can deny reflection access using security manager (with some tricks I'm not even aware of). – Grzegorz Grzybek Oct 10 '22 at 04:31
  • @GrzegorzGrzybek, to call an object's method one needs an object reference. I want to know how to share an object (reference) between two OSGi SCR components. Shared object doesn't "belong" to any of components. If I mark this shared object as an SCR component itself (to be able to inject it in other components) object would become exposed to another bundles as a service, which is unwanted. Without OSGi there is usually a common root under programmer's control (e.g. main method) or some kind of shared context, so that programmer can create a shared object in it and get access to it later. – Oleg_Andreych Oct 13 '22 at 18:32
  • In OSGi such _root_ is usually one of the activators - it's not very good practice, but I've seen it many times. These SCR components of yours may simply use static field in some well known class - but still, this _exposes_ the field to others. So there's no perfect solution here. – Grzegorz Grzybek Oct 14 '22 at 05:13
  • @GrzegorzGrzybek, yeah and that makes unit testing more problematic. Thanks for the advice anyway! – Oleg_Andreych Oct 15 '22 at 07:42

0 Answers0