How To Discover All Deployed Beans


import javax.enterprise.inject.Any;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.util.AnnotationLiteral;

@Inject
BeanManager beanManager;

Set<Bean<?>> beans = beanManager.getBeans(Object.class,new AnnotationLiteral<Any>() {}));
        for (Bean<?> bean : beans) {
            System.out.println(bean.getBeanClass().getName());
        }

Enjoy discovering!

See you at Upcoming Java EE Workhops at MUC Airport!

Comments:

Cool! BeanManager is new for me. Nice to know. Tnx.
BTW the "private" keyword can be left out ;-) (http://www.adam-bien.com/roller/abien/entry/why_not_private_visibility_for)

Posted by Ivar on September 12, 2012 at 07:47 PM CEST #

I think there is something missing in the beans declaration. Am I wrong?

Posted by frank on September 12, 2012 at 11:17 PM CEST #

Nice one! Is there a recipe for Java EE 5, too?

Posted by Danilo Piazzalunga on September 13, 2012 at 01:18 AM CEST #

Hi Ivar,

sorry for the bloat--I removed "private",

thanks!,

adam

Posted by Adam Bien on September 13, 2012 at 04:12 AM CEST #

@Frank,

you are not wrong--I always forget to escape generics (lesser than, greater than characters) and they are going to be swallowed by the browser.

Not it is fixed!,

adam

Posted by Adam Bien on September 13, 2012 at 04:13 AM CEST #

Adam, how can I get an instance of the bean.getBeanClass()?

I tried:

CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
beanManager.getReference(bean, bean.getBeanClass(), creationalContext);

It worked, Is it right?

Thanks.

Posted by Sandro on December 04, 2012 at 11:48 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License