[cdi-dev] [JBoss JIRA] (CDI-473) Standardize eager initialisation of ApplicationScoped bean

Antonin Stefanutti (JIRA) issues at jboss.org
Thu Mar 24 04:12:02 EDT 2016

    [ https://issues.jboss.org/browse/CDI-473?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13181698#comment-13181698 ] 

Antonin Stefanutti commented on CDI-473:

[~mkouba] indeed.

Ordering is an extension of the use case, but often you just need:
public class Foo {
  public void init() {

This would be an equivalent to Spring that eagerly pre-instantiates all singleton beans at startup.

It would be very useful in Java SE so that you can bind the lifecycle of beans that deal for example with external resources, pools, executors to that of the container. In Java EE, these tend to be resources managed by the application containers as opposed to Java SE.

A number of extensions already do it (Camel CDI for example to start endpoints, consumers, ...). But it looks like the need exists at application developer level as well. 

> Standardize eager initialisation of ApplicationScoped bean
> ----------------------------------------------------------
>                 Key: CDI-473
>                 URL: https://issues.jboss.org/browse/CDI-473
>             Project: CDI Specification Issues
>          Issue Type: Feature Request
>          Components: Contexts
>            Reporter: Antonin Stefanutti
>             Fix For: 2.0 (discussion)
> Given the proxying strategy documented in the CDI specification, normal scoped beans get initialize when an injected proxy reference is first called.
> While that's perfectly fine in the vast majority of use cases, that proves inconvenient when dealing with {{ApplicationScoped}} beans that capture application singletons which we want to bound to the application lifecycle with a {{postConstruct}} callback. As this callback is only called when a proxy is invoked, it is frequent to see the application developers using a CDI extension to meet that need, e.g.:
> {code}
> void forceInitialization(@Observes AfterDeploymentValidation adv, BeanManager manager) {
>     for (AnnotatedType<?> type : eagerBeans)
>         // Calling toString is necessary to force the initialization of normal-scoped beans
>         BeanManagerHelper.getReferencesByType(manager, type.getBaseType(), AnyLiteral.INSTANCE).toString();
> }
> {code}
> There should be a concise way to declare that intent which would then be address by the CDI container, for example:
> {code}
> @ApplicationScoped(eager = true}
> class EagerApplicationScopedBean {
> }
> {code}

This message was sent by Atlassian JIRA

More information about the cdi-dev mailing list