OSGi 包从另一个包访问 Spring 上下文文件 [英] OSGi bundle access Spring context file from another bundle

查看:27
本文介绍了OSGi 包从另一个包访问 Spring 上下文文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作为多个 Spring 项目存在的现有应用程序.项目 A 的 Spring 上下文 XML 文件使用

I have an existing application that exists as multiple Spring projects. Project A's Spring context XML file improts B's Spring context XML file using

<import resource="classpath*:/META-INF/spring/BContext.xml" />

但是,我得到一个 FileNotFoundException.我认为这是由于项目 B 的包没有公开资源造成的.我可以访问类,但不能访问文件.

However, it I get a FileNotFoundException. I assume this is caused by the fact that the resource is not exposed by project B's bundle. I can access the classes, but not the file.

在研究这个问题时,常见的评论是使用 OSGi 服务并注入服务,而不是尝试直接注入 bean.然而,由于这是一个现有的应用程序,我想避免重新布线.

When researching this issue the common comment was to use OSGi services and inject the services instead of trying to inject the beans directly. However, since this is an existing application, I would like to avoid rewiring the entire thing.

有没有办法告诉 OSGi 导出资源?我在 Karaf 上运行 ServiceMix.

Is there any way to tell OSGi to export the resource? I'm running ServiceMix on Karaf.

推荐答案

它只是一个类路径资源,所以我假设添加一个适当的 Export-Package 指令可以解决问题.不过,这绝对不是正确的做法.该上下文文件的路径表明,包含 BContext.xml 的项目可能已经设置为使用 Spring Dynamic Modules.如果是这样,那么当您启动该包时,Spring ApplicationContext 将作为服务导出.在您的 OSGi 控制台中查找它.

It's just a classpath resource, so I would assume adding an appropriate Export-Package directive would do the trick. That's definitely not the right way to do it, though. The path of that context file suggests that perhaps the project that contains BContext.xml is already set up to work with Spring Dynamic Modules. If so, then when you start that bundle, the Spring ApplicationContext is exported as a service. Look for it in your OSGi console.

回应评论中的讨论:

我自己从未尝试过,但理论上应该可以使用 Spring DM 的 osgi 命名空间 以创建 bean 对 OSGi 服务的引用,它是项目 B 的 ApplicationContext.然后,拥有一个作为 ApplicationContext 的 bean,您可以使用 普通 Spring 配置 使用 one of getBean() 方法.请注意,您可以使用 <constructor-arg .../> 为 Spring 配置中的工厂方法指定参数,如 本示例部分的底部.

I've never tried this myself, but theoretically it should be possible to use Spring DM's osgi namespace to make a bean reference to the OSGi service which is project B's ApplicationContext. Then, having a bean which is the ApplicationContext, you can use normal Spring configuration to extract beans from it using one of the getBean() methods. Note that you can use <constructor-arg ... /> to specify arguments to a factory method in a Spring config, as shown toward the bottom of this examples section.

这篇关于OSGi 包从另一个包访问 Spring 上下文文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆