我遇到了一个案例,我想在运行时使用 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>
日志
Bundle rest-cxf-server is waiting for dependencies [(objectClass=org.example.blog.rest.cxf.server.model.ModelEntityMapper)]