ejb - Details requried on javax.annotation.ManagedBean
Asked Answered
R

1

3

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.

  1. If I annotate a class with @javax.annotation.ManagedBean will it be singleton or it will have instance pool like stateless?
  2. Will the methods inside such class be concurrent? I should make sure as in a singleton they are synchronized by default.
  3. 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?
  4. 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.

Raney answered 4/9, 2015 at 14:35 Comment(0)
C
2
  1. Neither. They are per lookup/injection instances, more like stateful.

  2. No, there is no container-managed concurrency.

  3. (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.

Crusade answered 6/9, 2015 at 4:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.