Can I (and if so, how?) lookup CDI managed beans using javax.naming.Context#lookup
in EJB module?
I'm using GlassFish v3. I suppose that I can use @Named
, but what is JNDI name of CDI managed bean? I want to lookup them from unmanaged POJOs so I can't use @Inject
.
You can lookup the
BeanManager
via JNDI (java:comp/BeanManager
) then use the JSR-299 API hung off of theBeanManager
to get a contextual reference to a managed bean.JSR-299 managed beans are not available for direct JNDI lookup.
You can also access the BeanManager by using CDI.current(), which saves you typing a good few lines of code. Example taken from here
Using CDI.current()
Using JNDI:
Now you have the BeanManager you can access your CDI beans by doing either a type-based lookup or a name-based lookup.
Type based:
Name-based
Full example:
UPDATE - This can now be achieved in one line if you are using CDI 1.1: