首页 文章

在Apache Servicemix 4中的OSGi包之间共享配置文件?

提问于
浏览
2

有没有人能够在SMX4中成功共享两个或更多捆绑包之间的配置?我正在寻找的是:

  • $SMX_HOME/etc/myconfiguration.cfg 中有一个文件

  • 使用Spring dm通过OSGi配置管理将此配置"available"注入我的软件包,即

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:osgi="http://www.springframework.org/schema/osgi"
        xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
        xmlns:ctx="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
                            http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium-1.2.xsd">

        <osgix:cm-properties id="cfg"
            persistent-id="myconfiguration">
            <prop key="db.driverClassName">org.postgresql.Driver</prop>
            <prop key="db.url">jdbc:postgresql://localhost/db</prop>
            <prop key="db.username">someuser</prop>
            <prop key="db.password">somepassword</prop>
            <prop key="amq.brokerURL">vm://default</prop>
        </osgix:cm-properties>

        <ctx:property-placeholder properties-ref="cfg" />

然后,我可以将这样的东西注入我的bean:

.
    .
    .
        <bean id="activeMqConnectionFactory"
            class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="${amq.brokerURL}" />
        </bean>
    .
    .
    .

对于一个捆绑包,所有这些只是极好的 . 我正在寻找的是一种定义此方法的方法,然后能够重用相同的配置文件作为一组包的属性 . 现在,我有多个bundle,每个bundle都有自己的配置实例(持久id),因此,每个需要数据库连接,Java JMS等的bundle必须在每个文件中重复配置 .

目前,我正在使用Apache Servicemix 4,它使用Apache Felix作为OSGi容器 .

1 回答

  • 3

    我分享了我的配置

    $SMX_HOME/etc/my.config.cfg

    使用此声明

    <!-- get properties as bean from OSGi Configuration Admin Service -->
    <osgix:cm-properties id="myConfig" persistent-id="my.config" />
    
    <!-- activate ${...} placeholder -->
    <ctx:property-placeholder properties-ref="myConfig" />
    

    在我的每个包中 . 看起来非常类似于您的解决方案,工作完美!您可以共享您的配置(不需要为每个包提供配置文件),但您必须在每个spring-application-context文件中声明对此配置的引用 .

    我正在使用基于apache-servicemix-4.3.0-fuse-01-00的FUSE 4.3 .

相关问题