您使用哪些 XML 文件?你把它们放在你的 OSGi 包中的什么位置
(META-INF/弹簧,OSGi-INF)?这些做法中的哪一个可以让您
结合非双子座实现重用你的包
蓝图?
Gemini Blueprint 同等对待这两个目录,但 OSGI-INF/blueprint/*.xml 是通用 OSGi 蓝图规范中唯一指定的目录。
the Gemini Blueprint documentation 的建议做法是:
[...] 一个
建议的做法是拆分应用程序上下文配置
到至少两个文件中,按照约定 modulename-context.xml 命名
和模块名-osgi-context.xml。 modulename-context.xml 文件
包含独立于任何知识的常规 bean 定义
操作系统吉。 modulename-osgi-context.xml 文件包含 bean
用于导入和导出 OSGi 服务的定义。它可能(但
不需要)使用 Gemini Blueprint OSGi 模式作为顶层
命名空间而不是 Spring 'beans' 命名空间。
我试过了,效果很好。我将 Gemini Blueprint 用于我的一个项目,其中包含文件 META-INF/spring/context.xml,它定义了我的 bean 及其关系,以及 META-INF/spring/osgi-context.xml,它定义了哪些 bean 作为/从 OSGi 服务中公开/导入以及如何公开。 context.xml 看起来像
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="myOrdinarySpringBean" class="com.acme.impl.Foo"/>
</beans>
并且是一个普通的普通 Spring 应用程序上下文,根本没有 Blueprint/OSGi 配置。 osgi-context.xml 看起来像
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<service id="myOsgiService" ref="myOrdinarySpringBean" interface="com.acme.Foo"/>
</blueprint>
当然,您也可以在此处使用 <beans> 命名空间和根元素,但您必须定义一个 xmlns:osgi 并像这样为服务添加前缀:<osgi:service .../> 才能工作。就我而言,我不需要 Gemini 特定蓝图的东西,所以我对这个通用蓝图配置很满意。同样,我也可以在 context.xml 中使用 <blueprint> 命名空间,但是这个特定的应用程序是一个旧的,正在移植到 OSGi,所以我现在更愿意保留 Spring 特定的配置。
另一个应用程序又拥有自己的osgi-context.xmllike
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<reference id="myOrdinarySpringBeanImportedFromOsgi" interface="com.acme.Foo" availability="mandatory"/>
</blueprint>
目前没有,但可以拥有自己的 context.xml 喜欢
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="myOrdinaryOtherSpringBean" class="com.acme.impl.Bar">
<property name="foo" ref="myOrdinarySpringBeanImportedFromOsgi"/>
</bean>
</beans>
并且真的不在乎myOrdinarySpringBeanImportedFromOsgi 是从 OSGi 服务导入还是定义为同一应用程序上下文中的常规普通 Spring bean。
如果我想将自己与 Gemini 蓝图实现分离,这些 META-INF/osgi-context.xml 配置可以轻松地移动到 OSGI-INF/blueprint/,但目前我更喜欢将这两部分放在同一个地方以避免弄乱目录结构。