tags:

views:

54

answers:

1

The basic problem I have here is that I have one xml file that is being used as a utility file and imported into other xml files. It defines a series of objects for connecting to a platform and providing an interface to it. The beans in this file are defined to be lazy-initialised so that if you do not want to connect to the platform you will not but if you start referencing the appropriate bean then everything should get up and running.

The basic problem I have is that one of the beans in this set is not explicity referenced by any of the others but it is required to be constructed as it will call a method on one of the other beans in order to "activate" it. (It is acting as a gate keeper by switching on/off the connectivity based on what it detects as the state of the platform).

Here's a dummy of the sort of XML setup I have:

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

Now basically I want to remove the lazy-inits here that are required to get this thing to work properly. The objects referenced by the client XML are the PlatformConnection and the PlatformConnectionDataBus. How can I explicity declare that I want those other beans constructed if they are referenced?

+3  A: 

You can add an explicit dependency from one bean to another by using the depends-on attribute:

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

If I'm understanding your questin correctly, then I suggest you make all of your bean definitions lazy-init="true", and use depends-on to tie them together, for example:

<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"/>

So if your client config were to express a dependency on PlatformConnectionDataBus, then that would trigger the initialisation of PlatformConnectionDataBus, which in turn would trigger the initialisation of PlatformStatusNotifier.

skaffman