从 BundleContextAware 类访问应用程序上下文
我从现有的遗留战争中创建了一个 osgi 包。该应用程序有一个实现 spring 接口 ApplicationContextAware 的类,然后它使用上下文以编程方式获取 bean(不知道为什么,但这最终需要重构)。该应用程序现在使用 OsgiBundleXmlApplicationContext,但我认为使用此方法存在一个问题,即 setApplicationContext 方法不会在任何实现 ApplicationContextAware 的类中调用,因此现在此类中的上下文始终为 null。
因此,作为一种解决方法,我实现了 BundleContextAware,以便我可以获得对已发布上下文的引用并以这种方式访问 bean。这工作正常,但是上下文中唯一的 bean 是 warDeployer (应该提到我正在使用 spring dm 包 spring-extender 来部署 war)。上下文中存在的包是我的包,所以我不明白为什么我得到的上下文上没有我的 bean。我必须获取应用程序上下文的代码是:
ServiceReference ref = bundleContext.getServiceReference(ApplicationContext.class.getName()); applicationContext =(OsgiBundleXmlApplicationContext)bundleContext.getService(ref);
我可以在日志中看到我的大部分上下文正在创建,所以我不明白为什么它们不在我正在获取的上下文中。
任何人都可以建议出什么问题吗?我知道这种方法有点hacky,但在重构现有代码之前它是暂时的。
提前致谢。
巴里
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.
Barry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 ApplicationContext 服务是由 Spring-DM 扩展程序异步注册的。因此,您可能会遇到竞争条件,即在实际注册服务之前请求该服务。
您可以引入延迟,但随后您就会陷入令人讨厌的黑客领域。最好找出为什么未设置
ApplicationContextAware
bean 上的setApplicationContext
方法。您应该尝试提出这是针对 Spring-DM 的错误或在 Spring-DM Google Group 中询问。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 theApplicationContextAware
beans is not being set. You should try raising this is a bug against Spring-DM or asking in the Spring-DM Google Group.