[cdi-dev] JMS 2.1: Proposal to allow any CDI managed bean in a Java EE application to listen for JMS messages

arjan tijms arjan.tijms at gmail.com
Tue Aug 25 17:16:27 EDT 2015


On Tue, Aug 25, 2015 at 7:20 PM, Nigel Deakin <nigel.deakin at oracle.com> wrote:
> Someone over on the JMS user alias told me about this yesterday. So that
> would avoid the need to call a method to trigger lazy initialisation on
> normal-scoped bean proxies. I presume the application still has to inject
> the bean.

The bean does not necessarily have to be injected at any point.

With @Eager it will be activated and its @PostConstruct method will be
invoked in case it hadn't been referenced before (which in case of
@Eager is unlikely, as it instantiates very early, but still
theoretically possible).

The bean can of course still be injected or otherwise referenced later
within the same scope, but as mentioned this is not required.

So for example if you have OmniFaces on your classpath and then only
the following bean in an application:

public class MyEagerApplicationScopedBean {

     public void init() {
         System.out.println("Application scoped init!");

Then you'll see "Application scoped init!" being printed in your
server log when you deploy that application. No other code is needed.

> If so then it sounds useful. Is it going to be included as a standard
> feature of CDI 2.0? I see someone has proposed
> https://issues.jboss.org/browse/CDI-473 .

As the issue already mentions, this is possible with current day CDI
already. The proposal may arguably make it a little more concise and a
little less verbose. An eager attribute on the scope looks like a
quite minimal and elegant solution. Additionally it may make sense for
some scopes to restrict for which "IDs' it's eagerly instantiated
(e.g. paths for @RequestScoped, view IDs for @ViewScoped, flow IDs for
@FlowScoped, etc).

A new @Startup seems more like a very specific usage of the eager
attribute, namely for @ApplicationScoped (or Singleton?) beans.

Kind regards,

> Nigel

More information about the cdi-dev mailing list