I'm trying to figure out a weird scope issue with Spring beans and bundles.
BundleA depends upon an exported service from BundleB and BundleB uses a Spring bean we'll call BeanC (annotated with @Service).
When BundleB starts up, BundleB sets some stuff in BeanC (a list of objects). When BundleA calls to BundleB to look up an entry in BeanC's list, however, the list is empty. I can see from the object hashes that BeanC has different hashes. My register/undregister BundleB events work against one BeanC, and calls from BundleA work against a different instance.
I tried setting the BundleB and BeanC classes to @Scope("singleton"), but that doesn't help. Both get BeanC the same way, through @Autowired annotations. Even if I set the inner list of BeanC to static, I have the same problem - its as if two instance are created in two separate class loaders.
Any ideas why this is happening and how to get the same instance of BeanC for register/unregister bundle events as well as from exported service calls?
I can also tell that the BeanC being accessed from BundleA isn't the original because the original has a BundleContextAware interface on it that sets the bundle context in BeanC, but the BeanC called from BundleA has no bundle context set... so they are clearly two separate instances of BeanC when they should be the same.
I might have found the issue here, still investigating, but I think some JAR files from the other bundles got into the WAR, so when component scanning ran, it picked them up and used the local beans instead.
Yep, that was the issue. My Maven POM was including JARs that weren't necessary (since they are being imported by OSGI) which caused two sets of classes to be created (one set from OSGI and one set from the WAR). I went back and did a lot of house cleaning in my POM and I was good to go.
One thing I discovered along the way was that my <osgi:...> context calls had to be in my applicationContext.xml file. They couldn't be in my foo-servlet.xml otherwise I'd get a "IllegalArgumentException: Required 'bundleContext' property was not set" error during startup of the WAR. Must be due to the order of operations during the start up of a Spring context.