Skip to content

Consider introducing @Eager or similar annotation for bean eager init #835

@manovotn

Description

@manovotn

This has been discussed and requested repeatedly (last I know of was here).
Note that this is not a new feature as the same can be achieved via declaration of Startup event observer in any given bean.
However, that is often perceived as a workaround instead of a solution and is admittedly longer than using an annotation.

The idea would be to introduce an annotation such as @Eager/@Startup (name is subject to change), declarable at bean class level, which would make CDI initialize the bean on container startup.

Some thoughts:

  • This makes most sense for @ApplicationScoped (or @Singleton) beans but can technically work for all scopes
    • Should we forbid it for some scopes? Doesn't seem to make much sense for dependent beans for instance.
    • What about contexts that aren't active at startup? We should probably align this with how a startup event observer would behave.
  • Nothing in CDI specification requires impl to behave lazily (not create a bean until its method is invoked) yet this features indirectly assumes it as it would otherwise have no effect. This doesn't prevent us from adding it; just saying :)

FTR, some discussion about implementation took place in the aforementioned issue (here). It boils down to:

  • Gathering information about eager-init beans during discovery
  • Resolving them after container starts (along the lines of Instance#get())
    • Beans without client proxies will be created as soon as they are resolved.
    • Bean with client proxies will need to be "forcefully" initialized.
      • Dumb version is invoking their toString() which would work in 99% cases.
      • Otherwise needs to manually invoke their creation cycle for which some impls already have code anyway (here's Weld bit)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions