Is the annotation for bean @DependsOn meaning that the dependent bean will be created or initialized?

I am using Spring 3.0.2. I have two relatively simple definitions of bean. One has @PostConstruct (bean 'A'), which fires up an event chain for which you need to prepare @DependsOn bean (bean 'B'). However, it seems, although I stated that the bean 'A' depends on the bean 'B', the events (lifecycle methods) of the bean 'A' work until the bean 'B' is fully initialized.

Does a bean say "depends" on @DependsOn (or, if it depends, depends on the definition of a bean), does it mean that dependent w810 lifecycle methods will complete before a bean that depends on the bean mentioned?

Will the bean 'B' life cycle methods be executed before the bean 'A'?

UPDATE

Bean A is a custom class that uses the JMS template to send a message about its initialization.

The receiver of this message processes it and redirects it to MessageListeningContainer (bean B).

The first part is executed before the bean B was launched by the DefaultLifecycleProcessor.

@Component @DependsOn("beanB") public class BeanA { @PostConstruct public void init() { // do stuff } } <bean id="beanB" class="org.springframework.jms.listener.DefaultMessageListenerContainr"> <!-- other configuration --> </bean> 

I added bean b injection to my init method, plus two registration statements:

 container.isRunning(); container.isActive(); 

I looked at the Spring source, and isActive is true after the Initialization method (doInitialized is complete). IsRunning is set after doStart completes. DoStart is launched using the DefaultLifecycleProcessor, which occurs after calling the annotated @PostConstruct methods.

How can I guarantee that my Postconstruct method is called AFTER the bean b is initialized and started?

+7
source share
1 answer

In your specific case, the @PostConstruct bean A method will not be called until B is fully initialized, i.e. all its dependencies are entered and @PostConstruct execution @PostConstruct .

update: Since you rely on Spring Lifecycle functionality here, can you implement Lifecycle in and move your JMS call to the start() method?

+2
source

All Articles