Access application context from BundleContextAware class

I have created an osgi bundle from an existing legacy war. The app has a class that implements the spring interface ApplicationContextAware, it then uses the context to programmatically get beans (not sure why but this needs refactoring eventually). The app now uses OsgiBundleXmlApplicationContext, but I believe that there is an issue with using this whereby the setApplicationContext method does not get called in any classes implementing ApplicationContextAware, so now the context in this class is always null.

So as a workaround I have implemented BundleContextAware so that I can get a reference to the published context and get access to the beans that way. This works ok however the only bean on the context is warDeployer (should mention that I am using the spring dm bundle spring-extender to deploy the war). The bundle present on the context is my bundle so I cannot see why the context that I am getting has none of my beans on it. The code I have to get the application context is:

ServiceReference ref = bundleContext.getServiceReference(ApplicationContext.class.getName()); applicationContext = (OsgiBundleXmlApplicationContext) bundleContext.getService(ref);

I can see in the logs that much of my context is getting created so I can't see why they are not on the context I am getting.

Can anyone advise as to what is wrong? I understand that this approach is a bit hacky, but it's temporary until the existing code is refactored.

Thanks in advance.



I believe that the ApplicationContext service is registered asynchronously by the Spring-DM extender. So you probably have a race condition, i.e. asking for the service just before it is actually registered.

You could introduce a delay but then you're very deep into nasty hack territory. It would be better to work out why the setApplicationContext method on the ApplicationContextAware beans is not being set. You should try raising this is a bug against Spring-DM or asking in the Spring-DM Google Group.

Need Your Help

How to enable tinyMCE rich text editor for post Comments in WordPress?

wordpress tinymce

I would love to kinda replicate WordPress post/page text editor (Admin panel) in comments section. That's how site users will be able to format text easily. A good example of what I actually need c...

Nodejs Fstream Multiple 'end' callbacks fired when piping multiple files

javascript node.js stream

Issue: I am trying to figure out a way to only fire one 'end' event when piping multiple files to fstream.Reader.