如何将一个blueprint.xml中的单个bean注入一个单独的blueprint.xml?

问题描述 投票:0回答:1

我有一个Java类,它作为表示实体的多个其他类的注册表(在我的例子中,各种各样的苹果)。这有两个主要步骤:

  1. 实体在blueprintA中定义。注册表也会填充blueprintA。我的理解是蓝图自动处理正确的顺序作为其依赖注入过程的一部分(以及跨blueprint.xml文件)。
  2. 创建资源类并将注册表注入构造函数的参数或作为属性setter的参数。就我而言,我使用了一个bean设置器。

blueprintA

<?xml version="1.0" encoding="UTF-8" ?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <bean id="AppleRegistry" class="com.domain.AppleRegistry" activation="lazy" scope="singleton">
        <property name="apples">
            <set value-type="com.domain.apple.IApple">
                <ref component-id="macintosh" />
                <ref component-id="baldwin" />
                <ref component-id="stayman" />
                <ref component-id="crispin" />
            </set>
        </property>
    </bean>

    <bean id="macintosh" class="com.domain.apple.IApple"/>
    <bean id="baldwin" class="com.domain.apple.IApple"/>
    <bean id="stayman" class="com.domain.apple.IApple"/>
    <bean id="crispin" class="com.domain.apple.IApple"/>

</blueprint>

blueprintB

<?xml version="1.0" encoding="UTF-8" ?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <bean id="AppleResource" class="com.domain.api.AppleResource" scope="singleton">
        <property name="appleRegistry">
            <ref component-id="AppleRegistry" />
        </property>
    </bean>

</blueprint>

我希望我的代码可以工作,但我在日志中遇到错误,包括:

Unable to load com.domain.api.AppleResource from recipe BeanRecipe[name='AppleResource'], trying to load a nested class com.domain.api$AppleResource

我知道我省略了bean实现,但这个问题不是关于它们的。如果相关,我可以根据要求输入一些代码。

java dependency-injection blueprint-osgi
1个回答
0
投票

由于AppleRegistry和AppleResource都是顶级管理器,因此Blueprint无法自动确定它们之间的依赖关系(如子管理器的隐式依赖关系)。必须明确声明:

  • depends-on="com.domain.OtherTopLevelManager"

<bean id="AppleResource" class="com.domain.api.AppleResource" scope="singleton" depends-on="AppleRegistry">
        <property name="appleRegistry">
            <ref component-id="AppleRegistry" />
        </property>
</bean>
© www.soinside.com 2019 - 2024. All rights reserved.