【问题标题】:How to use properties from OSGI/Karaf ConfigurationAdminService with Spring dependency injection如何通过 Spring 依赖注入使用 OSGI/Karaf ConfigurationAdminService 中的属性
【发布时间】:2017-06-02 08:30:52
【问题描述】:

我正在尝试将存储在 Karaf 中的属性注入到我的 Camel/Spring-Service 中。到目前为止,我尝试按照Fuse documentationSpring 中记录的方式注入属性。 但两者似乎都过时了:osgix:cm-properties 无法被当前的 Spring 版本解析(Fuse 6.3 使用 3.2.16)。

另一方面,Apache Aries 似乎有一些现在可以使用的东西。 aries-blueprint-spring 功能包含两个捆绑包:

  • aries.blueprint.spring
  • aries.blueprint.spring.extender

我发现了一个指向此捆绑包的旧用户列表帖子。但我找不到任何使用它的文档或示例。我们只需要注入属性。

【问题讨论】:

    标签: spring apache-karaf jbossfuse


    【解决方案1】:

    使用 OSGi 服务纲要已经有一段时间了,下面是我的一个项目的摘录,希望对您有所帮助。

    重要的是 compendium 命名空间的声明及其前缀 osgix

    还要注意 persistent-id 的声明,因为它必须在要在容器实例的 etc 目录中创建的 Karaf 配置文件中定义。

    现在 Spring property-placeholder 指的是 osgix 属性声明和 CamelContext 中的 propertyPlaceholder。如果您想访问外部属性 - ${propName} 和内部属性 - {{propName}} CamelContext,则两者都是必需的。

    要访问 Camel 上下文之外的属性,语法是 ${propertyName}
    要访问 Camel Context 中的属性,语法是 {{propertyName}}


    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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.xsd     
        http://camel.apache.org/schema/spring               http://camel.apache.org/schema/spring/camel-spring.xsd
        http://www.springframework.org/schema/osgi          http://www.springframework.org/schema/osgi/spring-osgi.xsd
        http://www.springframework.org/schema/osgi-compendium       http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd
        http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd">
    
    
    
        <!-- A. Configuration Management -->
        <osgix:cm-properties id="cachingServicesProp" persistent-id="com.fsafrica.cachingservices.cm">
            <prop key="amqBrokerUrl">tcp://localhost:61616</prop>
            <prop key="amqUsername">admin</prop>
            <prop key="amqPassword">admin</prop>
            <prop key="queueName">jms/SRK_CACHE_QUEUE</prop>
        </osgix:cm-properties>
    
        <!-- Required for resolving properties for Beans outside CamelContext -->
        <ctx:property-placeholder properties-ref="cachingServicesProp" />
    
    
    
        <!-- B. ActiveMQ -->
        <bean class="org.apache.activemq.camel.component.ActiveMQComponent" id="activemq">
            <property name="brokerURL" value="${amqBrokerUrl}" />
            <property name="userName" value="${amqUsername}" />
            <property name="password" value="${amqPassword}" />
        </bean>
    
    
    
        <camelContext id="CC-CachingMain" xmlns="http://camel.apache.org/schema/spring">
    
            <!-- Required for resolving properties inside CamelContext -->
            <propertyPlaceholder id="properties" location="ref:cachingServicesProp"/>
    
    
    
            <!-- JMS INTERFACE -->
            <route id="Route-JMSMasterData">
    
                <from uri="activemq:queue:{{queueName}}?transacted=false" />
    
                <log message="#### After putting some data in the Queue (jms/SRK_CACHE_QUEUE) you should be able read this text on Karaf console" />
    
            </route>
    
        </camelContext>
    
    </beans>
    

    【讨论】:

    • 谢谢,但我确实是这样的。问题是在运行时 spring 无法解析 xml。似乎当前的春季版本放弃了对这些东西的支持?!
    猜你喜欢
    • 1970-01-01
    • 2013-11-16
    • 2013-09-17
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 2011-05-05
    • 2015-07-07
    相关资源
    最近更新 更多