Announcement Announcement Module
No announcement yet.
creation time dependecies Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • creation time dependecies

    Hi, I have a question regarding a design problem. I have 3 different main beans in my application which all run their own tasks (each starting it in its constructor) and communicate via set of interfaces. For example bean A calls B's interface methods and also registers itself for B's events. All the calls between the beans are asynchronous - logic is executed in bean private task.
    Now the thing is that each of the bean should not really start their actions without having set all references to others. There is one main bean that initiates all the actions. My worries are, that it may call the other bean's method and cause it to perform some actions, before all setXXX calls have been executed by the container.
    There are some simple workarounds, like wait(10s) right after starting the task, but I would like to avoid that for obvious reasons. There are also some more complicated options, like adding some pointcuts to check if all setXXX calls have been made and if yes, than start the tasks manually. I don't think this is a good design either.

    Could anyone give me some tips how to solve this issue in a well-designed manner?



  • #2
    Have you considered using the init-method property of the bean to execute a method after the setters have been called?


    • #3

      that is exactly what I was looking for! must have missed this while reading the reference docs.

      thanks a lot


      • #4
        ...And make sure you don't start any tasks in class constructors! Let the object to be fully constructed first. Then call the runTask() method, or whatever. I see people putting the class's business logic into the constructor quite often. They make the class do all its work before the object instance creation is completed. That's insane! Kids, don't ever do that!!!


        • #5
          Is there a way to configure Spring to invoke the "init-method" methods after the entire context has been processed. I'm running into a case where a service is started and fires events before all its listeners have been added. The listeners are added like so:

          <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                  <property name="targetObject" ref="MyService"/>
                  <property name="targetMethod" value="addListener"/>
                  <property name="arguments">
                      <bean class="mypackage.MyListener"/>
          I could add them to the service via constructor or setter injection, but that would make them dependencies of my service, which doesn't seem right. Although the system depends on them being listeners of the service, the service itself doesn't depend on them.

          I solved this by manually starting my service after I construct the context. It's actually more complicated than that, as there are several running services that could cause MyService to fire events. I have to ensure they are all started manually after context construction.


          • #6
            I don't know about cause it to execute after the entire context has been constructed, but you can cause the bean to be instantiated after another bean by adding the "depends" property. For example:

            <bean id="someBean" class="..." init-method="init" depends="anotherBean">
              <!-- Finish this -->
            <bean id="anotherBean" class="..." />
            In this case, since "someBean" will not be instantiated until after "anotherBean" is instantiated, its init-method will by extension not be called until "anotherBean" is instantiated.


            • #7
              Thanks. Yes, I thought about that. But then I'd have beans with depends-ons to beans they have no business knowing. For example, if A1, A2 and A3 are all services that call methods on and receive events from B, and I don't want any Ax to start processing until all Axs are wired to B, then A1 would require depends-on="A2,A3", etc. Note also that A1 has no business knowing about A2 or A3.

              It just seems cleaner and simpler to me to construct the entire context, THEN kick off processing (i.e., threads). That way you don't have to worry about accessing of half-built services. It would be nice if Spring provided a way to do this.