结合OSGi蓝图和弹簧配置

jen*_*ens 9 spring osgi spring-dm blueprint-osgi

关于Spring配置和OSGi Blueprint(例如Gemini Blueprint)的组合是否有任何良好/最佳实践?您使用哪些XML文件?你把它们放在OSGi包中的哪个位置(META-INF/spring,OSGi-INF)?以下哪种做法可以让您将捆绑包与蓝图的非Gemini实施结合使用?

背景:我们正在从Spring/Spring DM切换到Spring/Blueprint.我知道定义<bean>元素的蓝图.但是,我们偶尔会遇到蓝图规范的有限bean定义功能无法满足我们所有需求的情况.因此,在我们的捆绑包中使用Spring配置和通过OSGi服务连接捆绑包的Blueprint似乎是一个不错的选择.

Emi*_*erg 12

您使用哪些XML文件?你把它们放在OSGi包中的哪个位置(META-INF/spring,OSGi-INF)?以下哪种做法可以让您将捆绑包与蓝图的非Gemini实施结合使用?

Gemini Blueprint平等地处理这两个目录,但是是通用OSGi Blueprint规范中唯一指定的目录OSGI-INF/blueprint/*.xml.

Gemini Blueprint文档中的建议做法是:

[...]建议的做法是将应用程序上下文配置拆分为至少两个文件,这些文件由约定modulename-context.xml和modulename-osgi-context.xml命名.modulename-context.xml文件包含独立于OSGi知识的常规bean定义.modulename-osgi-context.xml文件包含用于导入和导出OSGi服务的bean定义.它可能(但不是必须)使用Gemini Blueprint OSGi架构作为顶级命名空间而不是Spring'bean'命名空间.

我试过这个,效果很好.我使用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>
Run Code Online (Sandbox Code Playgroud)

并且是一个普通的普通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>
Run Code Online (Sandbox Code Playgroud)

当然,你也可以<beans>在这里使用命名空间和根元素,但是你必须xmlns:osgi像这样定义一个和前缀服务:<osgi:service .../>为了工作.在我的情况下,我不需要Gemini特定Blueprint的东西,所以我很满意这个通用的Blueprint配置.同样,我也可以使用<blueprint>命名空间context.xml,但是这个特定的应用程序是一个移植到OSGi的旧应用程序,所以我更喜欢将该配置保持为Spring特定的.

另一个应用程序依次有自己的osgi-context.xml喜欢

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
  <reference id="myOrdinarySpringBeanImportedFromOsgi" interface="com.acme.Foo" availability="mandatory"/>
</blueprint>
Run Code Online (Sandbox Code Playgroud)

而且此时不会,但可以,有自己的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>
Run Code Online (Sandbox Code Playgroud)

并且无法真正关心myOrdinarySpringBeanImportedFromOsgi是从OSGi服务导入还是在同一应用程序上下文中定义为常规普通Spring bean.

如果我想将自己从Gemini Blueprint实现中META-INF/osgi-context.xml解脱出来,这些配置可以简单地移动到OSGI-INF/blueprint/,但是暂时我更喜欢将两半放在同一个地方以避免弄乱目录结构.


小智 5

Blueprint文件应该在OSGI-INF/blueprint /下,并且命名为*.xml(通常为blueprint.xml).该位置符合OSGi 4.2蓝图规范,可与Aries或Gemini配合使用.

Spring-DM文件(你可能知道)在META-INF/spring /下,并且也被命名为*.xml(通常是beans.xml)

这两个文件应该能够和平共存.但是,如果您支持安装的每个容器,它们只能工作.

应通过OSGi服务注册表进行接线.

至于迁移,我们一直在Spring-DM上寻找我们在Blueprint中无法做到的功能.其他一切都已迁移到蓝图.