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

Laravel : functions in model

php laravel laravel-4 laravel-5

I'm asking myself if i'm doing things in the right way. Here is my concern :

How to get value of EditText on tap of it?

android android-layout android-intent android-listview android-emulator

I've TextView and EditText in my layout. TextView represents price and EditText represents quantity. When user enters some number in EditText i want to multiply price by that entered number and dis...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.