首页 文章

无法将osgi服务bean暴露为类而不是接口

提问于
浏览
2

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

异常来自参考包:

线程“SpringOsgiExtenderThread-86”中的异常org.apache.camel.RuntimeCamelException:org.apache.camel.FailedToCreateRouteException:无法在以下位置创建路径article-author-getAll:>>> Filter [{in([header = = getAllAuthors])} - > [SetHeader [CamelMongoDbLimit,{2}],To [mongodb:mongo?database = xxxx&collection = xxxx&operation = findAll],Log [after db select getAllAuthors $]]] <<< in route :Route(article-author-getAll)[[来自[activemq:queue:backend.au ...因为无法解析 endpoints :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:引用必须具有已定义的接口属性 .

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

Camel Mongo路线看起来像这样:

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

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

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

1 回答

  • 0

    getting 对现有OSGi服务bean的引用之前,首先需要将此bean作为OSGi服务 export

    <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的引用 .

相关问题