Declaring a pronounced object dependency in Spring

The main problem that I have here is that I have one XML file that is used as a utility file and imported into other xml files. It defines a series of objects to connect to the platform and provides an interface for it. The beans in this file are defined as lazy-initialized, so if you don’t want to connect to the platform, you won’t, but if you start referencing the corresponding bean, then everything should get up and running.

The main problem I am facing is that one of the beans in this set is not an explanation of any of the others, but it needs to be constructed in such a way that it will call a method on one of the other beans in order to " activate "it. (He acts as a gatekeeper by turning on / off the connection based on what he defines as the state of the platform).

Here is the dummy view of the XML configuration that 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 elements here that are necessary for this thing to work properly. The objects referenced by client XML are PlatformConnectionand PlatformConnectionDataBus. How can I explain that I want these other beans to be created if they are referenced?

+3
1

bean depends-on:

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

+5

Source: https://habr.com/ru/post/1728425/


All Articles