我试图了解在 CDI 容器抛出的事件上下文中 @Initialized() 事件的生命周期/流程,这些事件在扩展中是可观察到的。
根据 WELD 2 docs, the Container lifecycle events是:
- BeforeBeanDiscovery
- ProcessAnnotatedType and ProcessSyntheticAnnotatedType
- AfterTypeDiscovery
- ProcessInjectionTarget and ProcessProducer
- ProcessInjectionPoint
- ProcessBeanAttributes
- ProcessBean, ProcessManagedBean, ProcessSessionBean, ProcessProducerMethod and ProcessProducerField
- ProcessObserverMethod
- AfterBeanDiscovery
- AfterDeploymentValidation
- BeforeShutdown
我很难找到的是,在这个容器生命周期中,@Initialized
会在哪里?事件被触发。我怀疑它是在 AfterDeploymentValidation 之后完成的,但我找不到任何文档来支持这一事实。此外,我似乎无法在 CDI 1.1 spec 中找到任何内容。它指示何时/何地抛出 @Initalized 事件。
例如,事件是在执行发现的bean 的所有@PostConstruct
方法之前还是之后抛出的?事件是在 EJB 之前还是之后抛出 @Startup
初始化了吗?是否有任何文档清楚地列出了 CDI 中这些事件的顺序/顺序?
最佳答案
Q1: What I'm having trouble finding out is where during this container lifecycle would the @Initialized event be triggered. I suspect that it is done AfterDeploymentValidation, but I cannot find any documentation to support that fact.
如 CDI 1.1 spec, section 11.5.4. AfterDeploymentValidation event 中所述:
The container must fire an event after it has validated that there are no deployment problems and before creating contexts or processing requests.
A1:因此,具有限定符 @Initialized
任何范围 的事件将在 之后 AfterDeploymentValidation
事件。
Q2: Additionally, I can't seem to find anything in the CDI 1.1 spec which dictates when/where the @Initalized event is thrown.
A2: 部分 6.7. Context management for built-in scopes描述每个内置范围的行为并为自定义范围实现提供建议:
Portable extensions are encouraged to fire an event with qualifier
@Initialized(X.class)
when a custom context is initialized ...
An event with qualifier@Initialized(RequestScoped.class)
is fired when the request context is initialized ...etc.
Q3: For instance, is the event thrown before or after all the
@PostConstruct
methods of discovered beans is executed?
如 6.7. Context management for built-in scopes 中所述:
The request scope is active:
- ...
- during@PostConstruct
callback of any bean.
The application scope is active:
- ...
- during@PostConstruct
callback of any bean.
...etc
A3:要使范围变为 Activity 范围,首先需要对其进行初始化。因此,带有限定符 @Initialized
的事件将在任何 bean 的 @PostConstruct
回调之前被触发,但仅限于必须处于 Activity 状态的范围在回调中。
Q4: Is the event thrown before or after an EJB
@Startup
is initialized? Is there any documentation that clearly lists the order/sequence of these events in CDI?
A4: EJB 由单独的规范涵盖 JSR 345: Enterprise JavaBeans TM ,Version 3.2 EJB Core Contracts and Requirements .
根据 4.8.1 单例 session Bean 初始化部分:
By default, the container is responsible for deciding when to initialize a singleton session bean instance. However, the Bean Provider can optionally configure the singleton session bean for eager initialization. If the Startup annotation appears on the singleton session bean class or if the singleton session bean has been designated via the deployment descriptor as requiring eager initialization, the container must initialize the singleton session bean instance during the application startup sequence. The container must initialize all such startup-time singleton session beans before any external client requests (that is, client requests originating outside of the application) are delivered to any enterprise bean components in the application.
...
In some cases, explicit initialization ordering dependencies exist between multiple singleton session bean components in an application. TheDependsOn
annotation is used to express these dependencies. ADependsOn
dependency is used in cases where one singleton session bean must initialize before one or more other singleton session beans. The container ensures that all singleton session beans with which a singleton session bean has aDependsOn
relationship have been initialized before thePostConstruct
method is called.
因此,具有限定符 @Initialized
的事件也将在 EJB bean 的 @PostConstruct
回调之前触发,但仅限于必须在回调。
关于java - @Initialized(ApplicationScoped.class) 事件何时在 CDI 中发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53229576/