I’m new to JavaEE and EJBs.
I made my first few steps with it but there are quite a few things about the basic annotations, that I don’t get, no matter how much time i spend googling and reading E-Books.
Maybe someone can help out or refer me to a good explanation.
@Stateful/@Stateless
I think I understood the basic concept. But which one is used by default?
@ManagedBean/@Named
Is the @Named Annotaion same as @ManagedBean simply with a custom Name?
@Sessionscoped/@Requestscoped
Do these interfere with @Stateful/@Stateless?
In my understanding it wouldn’t make sense to pair @Stateless with @Sessionscoped, because @Stateless beans are in this “Bean-Pool” on the server and are for Single-Method-Invocations and not for enduring tasks. Based on this logic I’d argue that it doesn’t make sense to have a @Requestscoped @Stateful bean, because after the initial HTTP-Request your Bean is “disconnected” from the client.
I get my Programs to work, but how can I now if I did it efficient, if I actually have no idea what I’m doing?
AD 1. None is the “default”. An EJB must be declared as an EJB and you do it either by annotating it with
@Stateless,@Stateful,@Singleton,@MessageDriven.If you don’t have any of them than it’s not an EJB, so there is no way to define a “default” value.
AD 2. There are two
@ManagedBeanin the matter of fact: javax.faces one and javax.annotation oneFrankly, I don’t know where the
@ManagedBeanfromjavax.annotationis really used. It declares that the bean is managed by the container, so its lifecycle is controlled – but what container controls it? I somehow feels that it’s obsolete as there are@Named,javax.faces @ManagedBean, EJB’s annotations and CDI annotations – all of them defines their annotated classes as managed but clearly defines container that manages them.@Nameddefines a CDI bean that can be used e.g. in the JSF views (UEL expressions).@ManagedBeandefines a JSF managed bean. Often they can be used interchangeably but there are some differences you should be aware of, e.g.@Namedbean you use cannot be annotated as JSF@ViewScopedas@ViewScopedcan only be applied to the JSF managed bean.AD 3. I don’t think you should mix CDI scopes with EJB scopes. That’s something that is often confused and it would be great if future Java EE address this issue.
EJB beans (
@Stateless/@Stateful) got their own lifecycles and CDI beans (@SessionScoped,@RequestScoped) got their own.I am not sure what you’d get by mixing those annotations – perhaps an exception, maybe some black-magic bugs or it might actually work as you wanted.
I guess it’s totally implementor-dependent as I don’t recall its defined in the EJB / CDI specification. I would not depend on it.