注释 bean @DependsOn 是否意味着依赖 bean 将被实例化或初始化?
我正在使用 Spring 3.0.2。我有两个相对简单的 bean 定义。其中一个有一个 @PostConstruct(bean 'A'),它触发 @DependsOn bean(bean 'B')需要准备的一系列事件。然而,即使我声明bean“A”依赖于bean“B”,bean“A”的事件(生命周期方法)也在bean“B”完全初始化之前运行。
通过 @DependsOn 声明一个 bean 是“依赖”的(或者就此而言,在 bean 定义中依赖)是否意味着依赖 bean 的生命周期方法将在依赖于所述 bean 的 bean 之前完成?
bean 'B' 生命周期方法会在 bean 'A' 之前完成吗?
更新
Bean A 是一个自定义类,它使用 JMS 模板发送一条消息,宣布其已初始化。
所述消息的接收者处理它并将其配置转发到 MessageListeningContainer (Bean B)。
第一部分发生在 Bean B 被 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>
我在 init 方法中添加了 bean b 的注入以及两个日志记录语句:
container.isRunning();
container.isActive();
我查看了 spring 源代码,在初始化方法(doInitialized 完成)之后 isActive 设置为 true。 isRunning 在 doStart 完成后设置。 doStart 由 DefaultLifecycleProcessor 触发,该处理器在调用 @PostConstruct 注解的方法后发生。
如何保证在 bean b 初始化并启动后调用我的 Postconstruct 方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的特定情况下,bean A 的
@PostConstruct
方法在 B 完全初始化之前不会被调用,即。它的所有依赖项都被注入,并且它的@PostConstruct
完成执行。更新:
由于您在这里依赖 Spring Lifecycle 功能,您可以在 A 中实现 Lifecycle 并将 JMS 调用移至那里的 start() 方法吗?
In your particular case
@PostConstruct
method of bean A won't be called until B is fully initialized, ie. all its dependencies are injected and its@PostConstruct
finishes executing.updated:
Since you are relying on Spring Lifecycle functionality here, can you implement
Lifecycle
in A and move your JMS call tostart()
method there?