adam bien's blog

EJB 3.1 or CDI Managed Bean As JSF Backing Bean 📎

Got interesting questions:

- I can use a ejb as a backing bean for Jsf(a session bean with @Named), but, is this useful I mean, have advantages against the @ManagedBean or @Named annotations directly in a class?

Answer: You can directly expose EJB 3.1 to JSF or JSP by annotating the class with @Named (see: http://www.adam-bien.com/roller/abien/entry/ejb_3_1_killed_the). However: in all non-trivial projects your EJBs will contain a significant amount of presentation logic and dependencies to JSF / JAX-RS APIs. Mixing presentation and business logic makes your code more complex, harder to test and therefore harder to maintain.

You can start with an EJB 3.1 exposed with @Named to JSF and on the first occurrence of any dependency to the presentation layer just refactor the presentation logic into a dedicated @RequestScoped @Named bean (I would not use @ManagedBean any more.)

- A bit far, I can annotate an interface of my Ejb with @Named. These approach is cool, but again, is this better?

Answer: It is hard to find a reason to use an interface for an EJB 3.1. I would start with no-interface view EJB 3.1 (a Java class annotated with @Stateless).

- Why not use directly an EJB project with all my Session Beans and only access the beans there? (consider an enterprise application with and ejb module and a web module - actual stage)

Answer: In Java EE 6 I always used WAR projects so far. You will have everything you need in one place. EAR-packaging works as well, but I would still separate the presentation from the business logic.