There are 3 different bean defining annotations in Java EE that will make a bean scoped to the entire application, which can cause confusion for developers. Each of them allow a bean to be application-wide, but they each provide a different context for accessing it. The differences are covered below.
javax.enterprise.context.ApplicationScoped
A CDI scope. Only one version of the bean will be accessible to the whole application. Clients hold a proxy object of the actual bean, which allows problems with serialization to be taken care of automatically.
javax.inject.Singleton
A CDI pseudo scope. It attempts to do a similar thing to ApplicationScoped, except clients hold a direct reference to the object. This does however mean these beans can cause problems with serialization, as the object may end up being duplicated.
javax.ejb.Singleton
An EJB scope. This means that the methods will be transactional by default, and you can utilise any functionality specific to EJBs, for example injecting an EJBContext.
Add Comment
Comments
Please sign in to leave a comment.