EJB 3.1 Killed The Backing Beans Or JSF 2, EJB 3.1, JSR-330 - The Perfect Synergy

A Stateless Session Bean with the JSR-330 @Named annotation:

@Stateless

@Named("helloService")

public class HelloService {

    @EJB    ClockService clockService;

    public String getHello(){

        return "Hello from EJB / CDI: " + clockService.currentTime();

    }

 can be directly referenced in the JSF 2:

    <h:body>

        <h:form>

            <h:outputLabel value="#{helloService.hello}"/>

        </h:form>

    </h:body> 

 You don't need the Backing Bean anymore - but you can still use it.

The project (JSF2EJB3CDI) was implemented in few minutes with NetBeans 6.8, tested with Glassfish v3 and pushed into: http://kenai.com/projects/javaee-patterns/. The entire WAR (JSF 2.0 , EJB 3.1, CDI) is 8 kB small. The initial deployment took 1.516 ms seconds, and was twice as slow, as in the Java EE 6 observer example:

INFO: Initializing Mojarra 2.0.2 (FCS b10) for context '/JSF2EJB3CDI'

INFO: Loading application JSF2EJB3CDI at /JSF2EJB3CDI

INFO: JSF2EJB3CDI was successfully deployed in 1,516 milliseconds. 

Comments:

I did not get it - to me it still looks like a backing bean (because it is used by JSF) ... just without the faces-config.xml - but thats part of the JSF 2 spec.

Can you tell us what exactly the "problem" was? To me it sounds dimmish.

Posted by mcahornsirup on December 18, 2009 at 01:36 PM CET #

@mcahornsirup

in Java EE 5 there was no problem. Just an additional class - so called Backing Beans was required. In this bean the EJB 3 was injected. With Java EE 6 an EJB 3(1) can be used directly as backing bean.

This examples works with backing beans: http://www.adam-bien.com/roller/abien/entry/simplest_possible_jsf_2_ejb

hope it is clear now, how lightweight Java EE 6 actually is :-),

Posted by Adam Bien on December 18, 2009 at 01:51 PM CET #

But the killer is neither EJB 3.1 nor JSR330. CDI takes it all! :-)

Posted by Benjamin on December 18, 2009 at 05:30 PM CET #

@Benjamin,

a single EJB 3.1 in the front of CDI is the simplest possible thing. Otherwise you will still have to deal with transactions, monitoring, concurrency etc...

Convention Over Configuration rules!,

thanks!,

adam

Posted by Adam Bien on December 18, 2009 at 05:54 PM CET #

IIRC wasn't there a change to allow EJB injection into backing beans (even prior to JSF 2.0)?
I think CDI is the real backing bean killer, everytime you go to use a managed bean annotation, you should be using the CDI equivalent.

Technically, you could just add the named annotation to your stateless bean and skip a whole step.

Either way, it is all still very slick and easy to use. Unfortunately, Netbeans came out with great support for backing beans which are now completely unnecessary.

Posted by Andy Gibson on December 18, 2009 at 06:05 PM CET #

@Adam,

Correct me if I'm wrong, but this looks like a very simple example with limited use. Off the top of my head, this would work well for pages that primarily display information.

For anything that would require holding state or form input, one would need either a stateful session bean or a (managed | backing) bean with a stateless session bean for business logic.

Posted by John Leed on December 18, 2009 at 06:57 PM CET #

CDI (Weld) is a great thing indeed. Seam's fathers took their ideas to the next level.
Andy said: Unfortunately, Netbeans came out with great support for backing beans which are now completely unnecessary.
I agree, but I think it's an option for those who prefers to use @ManagedBean annotation.
I think the real problem is: there's no support for CDI at all, specially with autocomplete or code assist feature. I've created a bug entry for this here: https://netbeans.org/bugzilla/show_bug.cgi?id=178687 , but nobody replied so far. I hope they have a solution soon, 'cause I like netbeans and I like cdi/weld/seam even more.

Posted by Geraldo Luiz on December 18, 2009 at 09:27 PM CET #

@John,

it is the simplest possible example and you are absolutely right. Backing Beans are still needed for the implementation of presentation logic.
In some cases you can bind rich domain objects directly to the UI and use the stateless session bean as a gateway... Then a Backing Bean is not need - it would be degrade to a plain delegate...

thanks!,

adam

Posted by Adam Bien on December 18, 2009 at 11:46 PM CET #

In order to get this to work I had to create a beans.xml file under WEB-INF with the content:

<beans></beans>

(as in your example project). NetBeans 6.8 did not create this for me.

I may have missed some documentation somewhere -- it took me a little while to figure out how to make this work for my project.

Thanks for the tips, keep them coming!

Posted by Chris Rued on December 22, 2009 at 06:51 PM CET #

Hi Adam,

How would JSF 2 resolvers behave suppose there is a managed bean with the same bean name as that of the annotated stateless session beans?

JSF 1.2 supported chaining of EL resolvers. I would suggest that stateless session beans with @Name annotations should at least have a namespace to avoid duplicate bean names.

Jerwin

Posted by Jerwin Louise Vergara Uy on December 23, 2009 at 05:16 AM CET #

Does this create a new HelloService for each request (debugging it seems to indicate that) ?
Adding @javax.enterprise.context.ApplicationScoped
yields "org.jboss.weld.DefinitionException: Scope interface javax.enterprise.context.ApplicationScoped is not allowed on stateless enterprise beans for class de.diron.vaude.model.facade.LanguageFacade. Only @Dependent is allowed on stateless enterprise beans"

Posted by Sven Peters on December 23, 2009 at 03:19 PM CET #

@Sven,

either it creates a new @Stateless session bean each request (I would prefer that), or reuses one from the pool. I would prefer the creation of a new instance every request. This depends mainly on your application server...

thanks for your comment and debugging efforts!,

adam

Posted by Adam Bien on December 23, 2009 at 06:39 PM CET #

I recently came accross your blog and have been reading along. I thought I would leave my first

comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep

visiting this blog very often.so you also can read my website,if you like
<a href="http://www.uggprovide.com/">ugg boots</a> or want to buy
<a href="http://www.purchaseuggboots.com/">ugg boots</a>,welcome to my website:

http://www.uggprovide.com/ or http://www.purchaseuggboots.com/

Posted by ugg boots on December 25, 2009 at 11:15 AM CET #

Cool example. However, one question (although maybe I should post this on a glassfish forum).

Following your example works great when the project is deployed as WAR to glassfish. However, when the WAR is included in an EAR file an exception is raised by javassist: Caused by: javassist.CannotCompileException: by java.lang.NoClassDefFoundError: nl/chriswesdorp/calculator/ejb/ClientProcessor
at javassist.util.proxy.FactoryHelper.toClass(FactoryHelper.java:169)

When I remove the beans.xml from the WEB-INF the deploy succeeds but dependency injection in the JSF fails.

Thanks (also for all the other useful information)
Chris

Posted by Chris Wesdorp on February 25, 2010 at 01:20 AM CET #

Hi Adam,

first of all: I really like your blog and what you are doing for the java community: Showing us how easy, lightweight and fun EE 6 really is.

I agree with you, that EJB 3.1 has killed (should kill) the Backing Bean.

Being a senior developer (senior as of age and as of experience) I have no problems learning new languages and new technologies. But learning Java EE (5/6) is very hard, confusing and frustrating.

I want to give some examples what else should be killed in Java EE 6:

It took me a while to figure out the naming: Is it J2EE? Java EE 1.5? Java EE 5? There should not be such a thing like J2EE 5 but even on the sun website if found the definition.

Facelets killed JSP. I have read lots of tutorials which praised JSP as the default View-Handler for JSF. By chance I found out that JSP is dead for a long time. But how come even NetBeans 6.5 had JSP in all their examples? Very hard to understand for a beginner :-)
And I am sure for a long there still will be a lot of "how can I use JSP in my JSF 2 app?" in the forums.

With delight I started to read the latest EJB 3 book from O'Reilly. But when they explained "Entity Beans and their role in EJB 3" I laid the book away (think I sell it pretty soon on eBay).

@People: Please forget everything else before EJB 3.1 and start using Java EE 6 with EJB 3.1, JSF 2 and CDI as the perfect synergy.

@Adam: Keep up the good work :-)
Sascha

Posted by Sascha on April 09, 2010 at 11:00 AM CEST #

Hi,
I've read here: http://smokeandice.blogspot.com/2009/12/cdi-and-declarative-transactions.html

that CDI will also kill ejbs since most of the time ejbs are used because of declarative transactions and remoting which can be achieved with CDI annotations. Sounds logical. Why do we still need ejbs?!

Posted by Daniel on June 23, 2010 at 02:34 AM CEST #

Hi, Can I use a Statefull Session Bean to replace JSF managedBean, but this Session Bean will have JSF components to do some application logic and a place holder to delimiter transaction bondary like "Save()", etc? Is this approach what people call gateway?

Posted by Ramires Coin on September 09, 2010 at 08:09 AM CEST #

@Ramires,

sure - just put @Named on the EJB 3.1 and hit ctrl-space in the JSF (using NetBeans),

enjoy!,

adam

Posted by adam-bien.com on September 09, 2010 at 02:06 PM CEST #

I am happy to introduce you about UGG Australia boots, nothing but give yourself a chance on the low price and to selection. I know that we all love to save money, and want to pass to our friends. Therefore, let me share any information about UGG boots with you. Discount Ugg Boots say that these boots are cozy and fashion like yesterday, and would be a fair opportunity for everyone. UGG boots are some of the most comfortable boots on the market today. Mainly, because of the materials used to manufacture these boots was genuine sheepskin. We are all ready to provide you with quality products and services. I am glad that you took time to look at this news, thanks so much.

Posted by new ugg on October 03, 2010 at 12:40 PM CEST #

Just to be easier to understand I wouldn't have called another EJB in HelloService.
I would have put a simple function like this:
public String getHello(){
return "Hello from EJB / CDI:" }

Posted by Alexis Krier on January 11, 2012 at 05:14 PM CET #

I'm using these (CDI, EJB3.1, JSF2) for a while and I find a problem using ejb methods directly in a JSF page.

In the example above, if you start the application in debug mode with a breakpoint on "getHello", and then call the JSF page a single time, you'll find that the ejb getHello method is called many times by the JSF engine, event if we reference it only once in the jsf page.

So I often separate "backing beans" from ejbs bussiness methods simply to do some caching to work around the problem.

But that is annoying, not knowing this could eventually lead to performance issues.

Posted by Richard on May 01, 2012 at 11:20 PM CEST #

Is it possible to inject a remote ejb, and have a bean validation (with jsf integration) capabilities? :)

Posted by dude on October 01, 2013 at 12:45 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License