简体   繁体   English

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

[英]How can I inject a singleton bean from one blueprint.xml into a separate blueprint.xml?

I have a Java class that serves as a registry of multiple other classes that represent entities (in my example, varieties of apples). 我有一个Java类,它作为表示实体的多个其他类的注册表(在我的例子中,各种各样的苹果)。 There are two major steps to this: 这有两个主要步骤:

  1. The entities get defined in blueprintA. 实体在blueprintA中定义。 The registry also gets populated in blueprintA. 注册表也会填充blueprintA。 My understanding is that blueprint automatically handles the right order as part its dependency injection process (and across blueprint.xml files). 我的理解是蓝图自动处理正确的顺序作为其依赖注入过程的一部分(以及跨blueprint.xml文件)。

  2. A resource class gets created and has the registry injected either as an argument of the constructor or as an argument to a property setter. 创建资源类并将注册表注入构造函数的参数或作为属性setter的参数。 In my case, I used a bean setter. 就我而言,我使用了一个bean设置器。

blueprintA 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 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>

I would expect my code to work, but I am getting errors in the log, including: 我希望我的代码可以工作,但我在日志中遇到错误,包括:

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

I am aware that I left out the bean implementations, but this question is not about them. 我知道我省略了bean实现,但这个问题不是关于它们的。 I could type up some code upon request, should those be relevant. 如果相关,我可以根据要求输入一些代码。

Because both AppleRegistry and AppleResource are top-level managers, Blueprint cannot automatically determine the dependency between them (like implicit dependencies of submanagers). 由于AppleRegistry和AppleResource都是顶级管理器,因此Blueprint无法自动确定它们之间的依赖关系(如子管理器的隐式依赖关系)。 It must be explicitly declared: 必须明确声明:

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

Example

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM