【发布时间】:2017-09-24 18:49:22
【问题描述】:
我的 JEE 应用程序中有 2 个单例,我想在启动时对其进行初始化。像这样的:
@Singleton
@Startup
public class ServiceB {
@EJB
private ServiceA a;
@PostConstruct
private void init() {
....
}
}
ServiceB 并不真正需要 ServiceA,我只是添加了依赖项以确保 ServiceA 在 ServiceB 的 init() 方法启动之前完全初始化(阅读:@PostConstruct-method finished)。
但它不会等待。 ServiceB 实际上在 ServiceA 之前启动。
有什么方法可以确保一个 Bean 的 @PostConstruct- 方法等待另一个 Bean 的 @PostConstruct- 方法完成?
我知道我可以删除 ServiceA 中的 @PostConstruct 注解并直接从 ServiceB 调用它
@PostConstruct init() {
a.init();
}
但我有没有 ServiceB 的部署。所以我不能依赖ServiceB来初始化ServiceA。 ServiceA 必须自己做。并且 ServiceB 必须等待 ServiceA 完成它。
【问题讨论】:
标签: java dependency-injection java-ee-6