Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF2 - backed by EJB or ManagedBean?

Tags:

java

jsf-2

ejb

As I am learning JSF2, I realized I am not sure what the backing components should be. From design point of view, what is the difference between EJBs and @ManagedBeans?

In the end I am going to use JPA, so EJB is a natural choice for business layer. Is it a good practice to use EJB directly from JSF (as explained here)?

At the moment I'm leaning towards using @ManagedBeans for components which don't need access to business layer (e.g. view helpers) or deal with request/session data. For other purposes, e.g. listing something in a grid, I would directly access EJB.

Is this a good design? Shall I use @ManagedBeans for all backing beans for the sake of clean layer separation, even if in some cases they only delegate to EJB?

like image 969
Konrad Garus Avatar asked Mar 11 '10 22:03

Konrad Garus


People also ask

When to use EJB vs CDI?

To sum it up: EJB = CDI + container services. There is no need to choose between them. Typically you start with POJOs, use the CDI features when needed and make your bean an EJB when any of the afore mentioned features is required.

What is CDI JSF?

Getting Started with CDI and JSF 2.0 Contexts and Dependency Injection (CDI), specified by JSR-299, is an integral part of Java EE 6 and provides an architecture that allows Java EE components such as servlets, enterprise beans, and JavaBeans to exist within the lifecycle of an application with well-defined scopes.

What are CDI beans?

A CDI bean is a POJO, plain old java object, that has been automatically instantiated by the CDI container, and is injected into all, and any qualifying injection points in the application. The CDI container initiates the bean discovery process during deployment.


1 Answers

Very valid question, but I guess the answer depends on the "strictness" of the approach for your project. There is indeed a bit of redundancy between JSF backing bean and EJB as both implement some business logic.

In a ideal usage of JSF features with converters, rendered, validator, etc. the backing bean could indeed be clean business logic code. But in practice some presentation-related logic frequently leaks in it.

This presentation-related logic should ideally not be in a EJB. This presentation-related logic may depend on the faces package, but not necessary. It's what it does that make it presentation-related or not.

A unified component model has some advantages though. And it's the approach taken by Seam and Spring. In both case, business component with declarative transaction, etc. can be used directly in JSF (Spring does not use EJB but provide a similar model). EJB purist would however say that you should dissociate the two.

So to me it's ultimately a question of taste and size of the project. I can imagine that for a small/medium project using EJB in JSF works fine. For bigger project where this strictness is critical, make sure you don't screw the layers.

like image 129
ewernli Avatar answered Sep 28 '22 04:09

ewernli