【问题标题】:unable to expose osgi service bean as class not interface无法将 osgi 服务 bean 公开为类而不是接口
【发布时间】:2019-03-02 05:27:44
【问题描述】:

我想将 spring bean 从一个捆绑上下文导出到另一个。当这个 bean 没有像 MongoClient 这样的接口时,问题就开始了。第一个包上下文注册Mongoclient,但是当我想将它公开给另一个时,我得到:“在注册表中找不到类型的mongo的bean:com.mongodb.Mongo”来自参考包。有没有办法在 OSGi 注册表中按类而不是接口来定义 bean?

异常来自参考包:

线程“SpringOsgiExtenderThread-86”中的异常
org.apache.camel.RuntimeCamelException:
org.apache.camel.FailedToCreateRouteException:创建路由失败 article-author-getAll at: >>> Filter[{in ([header{operationName} ==
getAllAuthors])} -> [SetHeader[CamelMongoDbLimit, {2}],
To[mongodb:mongo?database=xxxx&collection=xxxx&operation=findAll], 日志[在 db 之后选择 getAllAuthors ${body}]]] Route(article-author-getAll)[[From[activemq:queue:backend.au...
因为无法解析端点:
mongodb://mongo?collection=xxx&database=xxxx&operation=findAll 由于:在注册表中找不到 bean:mongo 类型:
com.mongodb.Mongo

在服务包中,一切看起来都不错!

服务包中的代码如下所示:

 <bean id="mongoDatasource" class="com.mongodb.MongoClient">
    <constructor-arg name="uri" ref="mongoClientUri" />       
</bean>

<bean id="mongoClientUri" class="com.mongodb.MongoClientURI">
<constructor-arg name="uri" value="${mongo_host}" />
</bean>

来自参考包上下文的代码:

<reference id="mongoDataSourceReference" bean-name="mongoDatasource" 
context-class-loader="service-provider" 
interface="com.mongodb.MongoClient"/>

MongoClient 没有接口,osgi:reference 必须定义接口属性。

我尝试扩展 MongoClient 类并实现接口,然后将其公开给 osgi 注册表我在参考包中正确收到了它,但后来我从 camelMongo 收到异常,我必须只定义 MongoClient 类!

骆驼蒙戈路线是这样的:

from("direct:findAll")
.to("mongodb:MYMONGOCLIENTBEAN?database=flights&collection=tickets&operation=findAll")

Camel mongo 路由在连接字符串中期望 MongoClient bean。

那么有没有办法在osgi注册表中按类而不是接口来定义bean? 或者我应该在与 camelMongo 相同的包中定义 MongoClient bean?

【问题讨论】:

    标签: spring mongodb apache-camel osgi osgi-bundle


    【解决方案1】:

    获取对现有 OSGi 服务 bean 的引用之前,您首先需要将该 bean导出为 OSGi 服务:

    <osgi:service ref="beanToPublish" interface="com.xyz.MyService"/>
    

    虽然推荐,但您的服务类不需要实现接口。 查看规格: https://docs.spring.io/spring-osgi/docs/current/reference/html/service-registry.html#service-registry:export

    OSGi 服务平台核心规范定义了术语服务 表示服务公共规范的接口 方法。通常这将是一个 Java 接口,但是 规范还支持在一个类下注册服务对象 名称,因此短语服务接口可以解释为指代 到接口或类。

    所以理论上没有什么可以阻止你使用完整的类名来引用你的 Mongo bean。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多