dependency-injection - bundle 中的蓝图依赖注入(inject)

标签 dependency-injection osgi blueprint-osgi aries

我遇到过这样一种情况,我想在运行时使用蓝图 (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/

相关文章:

c# - 使用 Ninject 2 构造函数注入(inject)

javascript - 访问angularjs中另一个模块中定义的工厂

angular - 如何在 Angular 6 中用 spy 对象替换组件范围的服务?

java - Bundlor 无法与 Java 8 lambda 正常工作

java - OSGI Blueprint jaxrs 服务器端点配置 - 如何配置地址?

python - 如何模拟 python (flask) webapp 中的服务层进行单元测试?

java - 使用哪个富客户端平台

java - 在 OSGi 包中使用 Spring AOP

java - servicemix中如何使用多个jar版本

osgi blueprint如何读取bundle中的资源文件