How to create an instance bean in JavaEE using its class

JavaEE6 has really revolutionize the development using CDI (from seam) but there are times when we need to create an instance of a class not by injection but by other means such as Component.getInstance from seam.

In seam we have something like
MyClass myClass = (MyClass) Component.getInstance("MyClass");

In JavaEE6, we do that by:
Bean bean = (Bean) beanManager.getBeans(MyClass.class).iterator().next();
CreationalContext ctx = beanManager.createCreationalContext(bean);
return (MyClass) beanManager.getReference(bean, MyClass.class, ctx);


I use this type of approach normally with FacesConverters.

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source

Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.

Explore NERV on GitHub →