Access the application context from the BundleContextAware class

I created an osgi package from an existing old war. The application has a class that implements the spring ApplicationContextAware interface, then uses the context to obtain beans programmatically (not sure why, but ultimately it is necessary for refactoring). Now the application uses OsgiBundleXmlApplicationContext, but I believe that there is a problem with this, as a result of which the setApplicationContext method is not called in any classes that implement ApplicationContextAware, so now the context in this class is always zero.

So, as a workaround, I implemented BundleContextAware to get a link to the published context and access the beans this way. This works fine, however the only bean in the context is warDeployer (should mention that I use spring dm bundle spring -extender to deploy the war). The package present in the context is my package, so I cannot understand why in the context that I receive, there is not one of my beans on it. The code I should get in the context of the application:

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

In the logs, I see that most of my context is being created, so I don’t understand why they are not in the context that I get.

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

Thanks in advance.

Barry

+3
source share
1 answer

I believe that the ApplicationContext service is registered asynchronously with the Spring -DM Extender. Thus, you probably have a race condition, that is, request a service immediately before its actual registration.

You can enter a delay, but then you go very deep into the dirty hacking area. It would be better to understand why the method setApplicationContexton ApplicationContextAwarebeans is not defined. You should try to get up, is this a bug with Spring -DM or a request in the Google Spring -DM group.

+1
source

Source: https://habr.com/ru/post/1777697/


All Articles