gorootde
gorootde

Reputation: 4073

Missing an event in spring

I am using Spring application events to send information to other beans. There's a bean A that publishes an event as soon as A is initialised. And there is a bean B that listens for the events sent by A.

Depending on the time A is initialised among the other beans, B gets initialised to late, and misses the event sent by A.

How is it intended in Spring to circumvent such issues? Is there any other way than changing the bean initialisation order?

Upvotes: 0

Views: 450

Answers (2)

mhasan
mhasan

Reputation: 3709

One probable way is to use

depends-on

attribute.You may define the dependency in case you are using

Xml configuration:

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

Annotation based:

@DependsOn("B")
public class A {}

This forces spring to intialize B before A so that it will not miss the event published by A.

Upvotes: 1

theDima
theDima

Reputation: 756

You can create init() method in your beans, and then in your main function make calls to init() methods of each bean. You can order the calls as you like there, and particularly call to A's init() before B's one. Then you'll have to put the publishing of event of bean A into A's init() method.

Upvotes: 0

Related Questions