简体   繁体   English

EJB bean生命周期依赖项?

[英]EJB bean lifecycle dependencies?

I can't find in the specs any clear statement of how dependency injection with either @EJB or @Inject interact with the container's bean lifecycle management. 在规范中找不到关于使用@EJB或@Inject进行依赖项注入如何与容器的bean生命周期管理进行交互的清晰说明。

I want to inject singleton bean A into singleton bean B. I want the container to guarantee that A exists by the time B's @PostConstruct method is invoked, and continues to exist until after B's @PreDestroy method completes. 我想将单例bean A注入到单例bean B中。我希望容器确保在调用B的@PostConstruct方法时A存在,并且一直存在直到B的@PreDestroy方法完成为止。

  1. Will @Inject (CDI injection) accomplish this? @Inject(CDI注入)会完成此任务吗?
  2. What about @EJB? @EJB呢?
  3. Or do I always need to add @DependsOn? 还是我总是需要添加@DependsOn?

CDI and EJB are two different specifications. CDI和EJB是两个不同的规范。 CDI provides integration allowing you to use EJB beans as if they were CDI (but not vice-versa!). CDI提供了集成,使您可以像使用CDI一样使用EJB Bean(反之亦然!)。 So note whether you are talking CDI or EJB as not all annotations are applicable to all beans. 因此,请注意您是在使用CDI还是EJB,因为并非所有注释都适用于所有bean。 For instance @Singleton (EJB) can have @Startup , whereas @ApplicationScoped bean from CDI cannot. 例如,@ @Singleton (EJB)可以具有@Startup ,而CDI中的@ApplicationScoped bean则不能。

You are mentioning "singleton bean". 您提到的是“单粒豆”。 Just to dispel confusion, in EJB that means @Singleton , in CDI that means @ApplicationScoped bean. 为了消除混乱,在EJB中表示@Singleton ,在CDI中表示@ApplicationScoped bean。

That being said, to the questions you had: 话虽这么说,对你的问题:

  1. Will @Inject (CDI injection) accomplish this? @Inject(CDI注入)会完成此任务吗?

@Inject should work as well so long as you use it to create a non-circular dependency among those beans - eg if you use @Inject at constructor of one bean, CDI will first initiate the dependency than this bean is trying to use. @Inject应该工作以及只要你使用它来创建这些bean中的非循环依赖-例如,如果你使用@Inject在一个bean的构造函数,CDI将首先启动的依赖比这个bean尝试使用。 Keep in mind that CDI is initiating things lazily though - nothing happens until you actually try to use that bean. 请记住,尽管CDI懒惰地启动事情-在您实际尝试使用该bean之前什么也不会发生。

  1. What about @EJB? @EJB呢?

Not sure what you mean here - @EJB is an EJB equivalent of @Inject in CDI. 不知道你的意思在这里是什么- @EJB是EJB相当于@Inject在CDI。

  1. Or do I always need to add @DependsOn? 还是我总是需要添加@DependsOn?

This would be an option as well but again, note that this can only be used on EJB @Singleton beans (which is perfectly fine, just denoting the CDI vs. EJB difference here). 这也将是一个选项,但是再次注意,它只能在EJB @Singleton Bean上使用(这很好,在这里仅表示CDI与EJB的区别)。

  1. Inject (CDI) is not part of the EJB spec. 注入(CDI)不属于EJB规范。 This is probably vendor specific behavior. 这可能是特定于供应商的行为。 Use EJB injection with EJBs. 将EJB注入与EJB一起使用。 Perhaps this part of the CDI spec may help: cdi spec: relationship to other specifications CDI规范的这一部分可能会有所帮助: cdi规范:与其他规范的关系

  2. and 3. See EJB Spec 3.2 chapter 4.8.1: Singleton Session Bean Initialisation. 和3.参见EJB Spec 3.2章节4.8.1:单例会话Bean初始化。 You didn't say anything about the initialisation (eager or at startup), so please refer to the docs for details. 您没有对初始化进行任何说明(急切或在启动时),因此请参考文档以获取详细信息。

In some cases, explicit initialization ordering dependencies exist between multiple singleton session bean components in an application. 在某些情况下,应用程序中的多个单例会话bean组件之间存在显式的初始化排序依赖性。 The DependsOn annotation is used to express these dependencies. DependsOn批注用于表达这些依赖性。 A DependsOn dependency is used in cases where one singleton session bean must initialize before one or more other singleton session beans. 在一个单例会话Bean必须在一个或多个其他单例会话Bean之前初始化的情况下,使用DependsOn依赖项。 The container ensures that all singleton session beans with which a singleton session bean has a DependsOn relationship have been initialized before the PostConstruct method is called. 容器确保在调用PostConstruct方法之前已初始化与单例会话Bean具有DependsOn关系的所有单例会话Bean。

Note that if one singleton session bean merely needs to invoke another singleton session bean from its PostConstruct method, no explicit ordering metadata is required. 请注意,如果一个单例会话bean仅需要从其PostConstruct方法调用另一个单例会话bean,则不需要显式的排序元数据。 In that case, the first singleton session bean would merely use an EJB reference to invoke the target singleton session bean. 在那种情况下,第一个单例会话Bean将仅使用EJB引用来调用目标单例会话Bean。 In this case, the acquisition of the EJB reference (either through injection or lookup) does not necessarily imply the actual creation of the corresponding singleton session bean instance. 在这种情况下,(通过注入或查找)获取EJB引用不一定意味着实际创建了相应的单例会话bean实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM