【问题标题】:Declaring an explict object dependency in Spring在 Spring 中声明一个显式的对象依赖
【发布时间】:2010-01-14 07:42:48
【问题描述】:

我在这里遇到的基本问题是我有一个 xml 文件被用作实用程序文件并导入到其他 xml 文件中。它定义了一系列用于连接到平台并为其提供接口的对象。此文件中的 bean 被定义为延迟初始化,因此如果您不想连接到平台,则不会,但如果您开始引用适当的 bean,那么一切都应该启动并运行。

我遇到的基本问题是,这个集合中的一个 bean 没有被其他任何一个明确引用,但它需要被构造,因为它将调用其他 bean 之一的方法以“激活” “ 它。 (它通过根据检测到的平台状态打开/关闭连接来充当看门人。

这是我拥有的那种 XML 设置的虚拟模型:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
    default-lazy-init="true">

    <!-- Provides the connection to the platform -->
    <bean id="PlatformConnection">
        <constructor-arg ref="PlatformConnectionProperties" />
    </bean>

    <!-- This bean would be overriden in file importing this XML -->
    <bean id="PlatformConnectionProperties"/>

    <!-- Controls the databus to be on/off by listening to status on the Platform
         (disconnections/reconnections etc...) -->
    <bean lazy-init="false" class="PlatformStatusNotifier">
        <constructor-arg ref="PlatformConnection" />
        <constructor-arg ref="PlatformConnectionDataBus" />
    </bean>

    <!-- A non platform specific databus for client code to drop objects into -
         this is the thing that client XML would reference in order to send objects out -->
    <bean id="PlatformConnectionDataBus" class="DataBus"/>

    <!-- Connects the DataBus to the Platform using the specific adaptor to manage the java object conversion -->
    <bean lazy-init="false" class="DataBusConnector">
        <constructor-arg>
            <bean class="PlatformSpecificDataBusObjectSender">
                <constructor-arg ref="PlatformConnection" />
            </bean>
        </constructor-arg>
        <constructor-arg ref="PlatformConnectionDataBus" />
    </bean>

</beans>

现在基本上我想在这里删除让这个东西正常工作所需的惰性初始化。客户端 XML 引用的对象是 PlatformConnectionPlatformConnectionDataBus。如果引用了其他 bean,我如何明确声明我希望构建它们?

【问题讨论】:

    标签: java spring


    【解决方案1】:

    您可以使用depends-on 属性将一个bean 的显式依赖添加到另一个bean:

    <bean id="a" class="A"/>
    <bean id="b" class="B" depends-on="a"/>
    

    如果我正确理解了您的问题,那么我建议您将所有 bean 定义 lazy-init="true",并使用 depends-on 将它们绑定在一起,例如:

    <bean id="PlatformStatusNotifier" lazy-init="false" class="PlatformStatusNotifier">
        <constructor-arg ref="PlatformConnection" />
        <constructor-arg ref="PlatformConnectionDataBus" />
    </bean>
    
    <bean id="PlatformConnectionDataBus" lazy-init="false" class="DataBus" depends-on="PlatformStatusNotifier"/>
    

    因此,如果您的客户端配置要表达对PlatformConnectionDataBus 的依赖关系,那么这将触发PlatformConnectionDataBus 的初始化,进而触发PlatformStatusNotifier 的初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-22
      • 2011-01-16
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2012-02-03
      • 2018-12-20
      相关资源
      最近更新 更多