Dependency injection bundle中的蓝图依赖注入

Dependency injection bundle中的蓝图依赖注入,dependency-injection,osgi,blueprint-osgi,aries,Dependency Injection,Osgi,Blueprint Osgi,Aries,我遇到了一个案例,我想使用Blueprint(Aries)在运行时解决依赖关系,而实现是在需要它的同一个包中定义的,不会在任何其他包中使用。我正在抽象这个包中的实现,以便在进行单元测试时更容易模拟依赖关系。如果我把这个服务放在它自己的包中,就会导致凝聚力差 在运行时,蓝图表示它正在等待依赖项。如何使用Blueprint在捆绑包中实现依赖项注入 <!-- Interface --> <reference id="modelEntityMapper" interface="org.

我遇到了一个案例,我想使用Blueprint(Aries)在运行时解决依赖关系,而实现是在需要它的同一个包中定义的,不会在任何其他包中使用。我正在抽象这个包中的实现,以便在进行单元测试时更容易模拟依赖关系。如果我把这个服务放在它自己的包中,就会导致凝聚力差

在运行时,蓝图表示它正在等待依赖项。如何使用Blueprint在捆绑包中实现依赖项注入

<!-- Interface -->
<reference id="modelEntityMapper" interface="org.example.blog.rest.cxf.server.model.ModelEntityMapper" />
<!-- Implementation defined within same bundle -->
<bean id="modelEntityMapperImpl" class="org.example.blog.rest.cxf.server.model.impl.ModelEntityMapperImpl" />
<service ref="modelEntityMapperImpl" interface="org.example.blog.rest.cxf.server.model.ModelEntityMapper" />

<!-- Object which has dependency -->
<bean id="posts" class="org.example.blog.rest.cxf.server.BlogResourceImpl">
        <property name="modelEntityMapper" ref="modelEntityMapper" />
</bean>

编辑 我刚刚尝试了@christian scheider的建议,Blueprint仍在等待一些服务来满足ModelEntityMapper的要求

XML

<!-- Interface -->
<reference id="modelEntityMapper" interface="org.example.blog.rest.cxf.server.model.ModelEntityMapper" />
<!-- Implementation defined within same bundle -->
<bean id="modelEntityMapperImpl" class="org.example.blog.rest.cxf.server.model.impl.ModelEntityMapperImpl" />

<!-- Object which has dependency -->
<bean id="posts" class="org.example.blog.rest.cxf.server.BlogResourceImpl">
        <property name="modelEntityMapper" ref="modelEntityMapperImpl" />
</bean>

日志


捆绑rest cxf服务器正在等待依赖项[(objectClass=org.example.blog.rest.cxf.server.model.ModelEntityMapper)]

您可以直接引用服务的bean吗?如果在同一个蓝图文件中定义服务和服务引用,那么使用OSGi服务就没有多大意义。

我在Aries站点上找不到与捆绑包中引用相关的详细文档,因此我将参考Eclipse Gemini蓝图实现文档(以前是Spring动态模块)。请参阅中的警告。是的,从技术上讲,这与它们的实现有关,但我相信在白羊座可能也有类似的情况

声明对也由同一捆绑包导出的服务的强制引用是错误的,此行为可能导致应用程序上下文创建因死锁或超时而失败

简而言之,您通常要么导入(引用)一个OSGi服务,要么在同一个捆绑包中导出一个OSGi服务,通常不尝试在单个捆绑包中同时执行这两项操作

如果希望此捆绑包导出类型为
ModelEntityMapper
的服务,则需要使用
service
元素将其导出。当其他bean需要同一捆绑包中的引用时,您可以像使用它一样使用
ref
属性。在这种情况下,您不需要
reference
元素t,而是使用
服务
元素


如果您不打算在这个捆绑包之外使用
ModelEntityMapper
bean,那么您根本不需要在配置中使用
reference
service
元素。您应该能够在
ref
属性中使用它,而无需将其导出为OSGi服务-它基本上是该bu内部的beanndle.在这种情况下,您应该能够完全删除
reference
元素:
这只是一个猜测,但是
blogRepositoryApi
引用是在哪里定义的?我还要检查
org.example.blog.rest.cxf.server.model
包是否被导出和导入(不确定是否相关,但从头顶上写下来)。同意。捆绑包中的Blueprint可以正常工作,但前提是您不使用中间服务。我只是尝试了一下,但运气不好。我应该能够像XML I一样直接引用bean?您没有删除引用定义。因此,它会等待一个永远不存在的服务。尝试删除引用元素,它就会出现uld工作。@Holly:我说的对吗?最初的方法的问题是blueprint会等到服务引用出现,直到它启动上下文。所以它永远不会到达安装自己的服务的位置?@Christian,是的,这是blueprint无法满足的一个循环。我不会在这个包之外使用这个ModelEntityMapper这是正确的安排,谢谢