Beans created by a producer defaults to be @Dependent
scoped. A producer with no scope specified will be called each and every time Weld needs to inject a bean that is matched by the producer's bean types and qualifiers. It also means there will be a separate instance of the bean for each call made to the producer, which is not always what we want or intend.
CDI makes it easy for us to modify this behavior by adding any of @RequestScoped
, @ConversationScoped
, @SessionScoped
, or @ApplicationScoped
onto the producer, depending on which lifecycle we want for the produced bean. If we were to annotate a producer with @ApplicationScoped
, the producer would only be called once for the life of the application, and the created bean is stored in the application context to be shared by all clients.
An important point to remember when thinking of producers is that they are themselves a bean in their...