An extremely common use case for extension developers is to register one or more beans with Weld to make them available within an application. To make it possible to register new beans, CDI provides the Bean
interface, which represents a bean within an application. For every bean in our application, including interceptors, decorators, and producers, there is an instance of Bean
that is registered with the BeanManager
.
Note
The AnnotatedType
represents the metadata of a bean type, which is used by Weld to then construct a physical Bean
instance from that metadata.
We will create a simplified example of how an extension can add a bean to a CDI application.
public class AddBeanExtension implements Extension { public void afterBeanDiscovery (@Observes AfterBeanDiscovery after, BeanManager beanMgr) { // read the annotations of our class AnnotatedType<MyClass> type = beanMgr.createAnnotatedType(MyClass.class); // instantiate class and inject dependencies...