jakarta-eejsf-2cdijboss-weld

Getting null instead of new instance


I believe that if you inject a bean with say ApplicationScope and it has never been instantiated CDI will give you null.

However if you create a new instance and then try to inject it at a later point works fine and from then on it will continue to work fine.

In JSF depedency injection you never had to create it once though. I believe JSF would do something like

//pseudo code
AtStartUp () {
if (applicationScoped)
create new Instance();

or possibly

AtInjectionPoint () {
if (applicationScoped && null)
return new Instance();

else return Instance;

While I believe CDI does something like

AtInjectionPoint () {
return Instance;

Can you follow me here? My problem is basically every applicationscoped bean we have is null unless we create it once. We would very much like CDI to handle this somehow and behave like one of the first two pseudo code examples.

Does anyone know more about this?


Solution

  • A CDI bean is automatically created by the container at the time it is referenced the first time. No need to instantiate it yourself.

    I am not sure why you experience such behavior. Maybe you are mixing jsf managed bean annotations with CDI annotations? Or you are trying to access the injected bean in a constructor?