我遇到过这样一种情况,我想在运行时使用蓝图 (Aries) 来解决依赖关系,并且实现是在需要它的同一个包中定义的,并且不会在任何其他包中使用。我正在抽象这个包中的实现,以便在单元测试时更容易模拟依赖项。如果我将此服务放在自己的包中,则会导致内聚性差。
在运行时,蓝图表示它正在等待依赖项。如何使用Blueprint实现bundle内的依赖注入(inject)?
<!-- 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>
日志
bundle rest-cxf-server 正在等待依赖项 [(objectClass=org.example.blog.rest.cxf.server.model.ModelEntityMapper)]
最佳答案
能不能直接引用服务的bean?如果您在同一个蓝图文件中定义服务和服务引用,那么使用 OSGi 服务就没有多大意义。
关于dependency-injection - bundle 中的蓝图依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14216300/