8

I am using Spring 3.0.2. I have two relatively simple bean definitions. One has a @PostConstruct (bean 'A') which triggers a chain of events that the @DependsOn bean (bean 'B') needs to be prepared for. However, it seems even though that I stated that the bean 'A' is dependent on bean 'B', the events (the lifecycle methods) of bean 'A' are running before bean 'B' is fully initialized.

Does stating that a bean is "dependent" via @DependsOn (or for that matter, depends-on in a bean definition) mean that the dependent bean's lifecycle methods will be completed before the bean that is dependent on said bean?

Will bean 'B' lifecycle methods be completed before bean 'A'?

UPDATE

Bean A is a custom class that is using a JMS Template to send a message announcing that he has initialized.

The recipient of said message processes it and forwards it's configuration to a MessageListeningContainer (Bean B).

The first part is happening all before Bean B has been started 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 in my init method the injection of bean b plus two logging statements:

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

I looked at the spring source and isActive is set to true after the Initialization method (doInitialized is completed). The isRunning is set after the doStart is completed. The doStart is triggered by the DefaultLifecycleProcessor which is occurring after the @PostConstruct annotated methods are called.

How can I guarantee that my Postconstruct method is called AFTER bean b has been initialized AND started?

4

1 回答 1

1

在您的特定情况下@PostConstruct,在 B 完全初始化之前,不会调用 bean A 的方法,即。它的所有依赖项都被注入并@PostConstruct完成执行。

更新:由于您在这里依赖 Spring 生命周期功能,您可以Lifecycle在 A 中实现并将您的 JMS 调用移动到那里的start()方法吗?

于 2012-01-11T16:47:51.970 回答