Sep 13, 2018

EJBs vs CDI-managed beans

Context is the distinguishing feature between EJBs and CDI-managed beans. CDI beans exist within a defined context; EJBs do not. CDI beans are created within the context of a scope; they exist for the life of the scope and are destroyed when the scope finishes. There are four scopes that are annotated as follows: @ApplicationScope@ConversationScope@SessionScope, and @RequestScope. The CDI container controls the life of a bean based on the bean's defined scope. For example, a bean annotated with @SessionScope exists for as long as the HTTP session is alive; at the end the scope, the bean is destroyed and marked for garbage collection. This behavior is in contrast to that of EJBs, which are not bound to a scope. This means that you must explicitly remove the bean by calling a method annotated by the @Remove annotation.