问题
There is lot of information about Stateless, Stateful and Sigleton beans everywhere but almost nothing about javax.annotation.ManagedBean. At a first look I assumed that It's be similar to Spring's @Component but I can't use it without complete information.
- If I annotate a class with
@javax.annotation.ManagedBean
will it be singleton or it will have instance pool like stateless? - Will the methods inside such class be concurrent? I should make sure as in a singleton they are synchronized by default.
- I was thinking of annotating my DAO class with this but the
@javax.enterprise.context.*;
scopes put me doubt. I think@Stateless
will be better. Any comments? - If not on DAO or service classes, where does this annotation fit in?
This answer gives very good explanation but doesn't answer the above questions.
回答1:
Neither. They are per lookup/injection instances, more like stateful.
No, there is no container-managed concurrency.
(and 4.) Do you need transaction, security, or other EJB capabilities? Then
@Stateless
is probably better. Otherwise, I would just use CDI since it is better than the@javax.annotation.ManagedBean
annotation in nearly all ways, and it is enabled by default in EE 7, so it is clearly the forward direction for EE.
As a bit of background, the @javax.annotation.ManagedBean
annotation was added late in the development of the EE 6 cycle, and it is not widely used. The managed bean spec was intended to unify the lifecycle, injection, and naming behaviors of the EJB, CDI, and JSF managed bean component models. That was useful, but in my opinion, the @javax.annotation.ManagedBean
annotation was just an afterthought to allow developers to access the minimal component model functionality without the overhead/complexity (real or perceived) of the other component models (EJB necessarily has a fixed set of required services and associated overhead, CDI is better in nearly all ways but is clearly more complex, and JSF managed beans are tied to WAR). However, this "common denominator" is then a quite limited component model with just @PostConstruct, @Resource (and other EE injection), and @Interceptors. There's no security, transaction, scoping/lifecycle (as in EJB or CDI), @PreDestroy, tight integration with the web tier, etc.
来源:https://stackoverflow.com/questions/32400686/ejb-details-requried-on-javax-annotation-managedbean