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

Antonio Goncalves (JIRA) issues at jboss.org
Mon Sep 15 06:06:02 EDT 2014


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

Antonio Goncalves commented on CDI-473:
---------------------------------------

What about using {{@Startup}} in this case ?

One idea in Java EE 7 was to take the [{{javax.ejb.Startup}}|http://docs.oracle.com/javaee/7/api/javax/ejb/Startup.html] annotation out of the EJB spec, and give this functionality to every managed bean (even a servlet). This wasn't done... unfortunately.

For Java EE 8, hopefully, we will be able to have such startup behavior back into the [Concurrency Utilities for Java EE|https://jcp.org/en/jsr/detail?id=236] where it would make more sense.


> 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
>
> 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
(v6.3.1#6329)


More information about the cdi-dev mailing list