bourne bourne - 2 months ago 22
Java Question

CDI. How to check whether bean instantiated or not?

I've got a name of CDI @Named bean. For example, 'firedEmployeeBean'.

Is there any way in other CDI bean check whether 'firedEmployeeBean' already instantiated or not?


As already stated if you use @Inject once you check it will be. What you instead want is to have a property to tell you what you want:

boolean initiated;

if this simple solution does not cut it I would recommend to use Deltaspike:

MyBean myBean = BeanProvider.getContextualReference(MyBean.class, true);

Note the second argument, true - from docs: Pass true as second argument, if you look for an implementation of the given interface and an implementation isn't required or it isn't required that there is an instance with the given qualifier (see the qualifier example for further details).

Lastly you can use events. Events are really simple to use in CDI. What you need to do is fire the event when the bean is created and have the other bean observe that event.