File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Why use CDI for beans and injection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Why use CDI for beans and injection?" Watch "Why use CDI for beans and injection?" New topic

Why use CDI for beans and injection?

john lazeraski
Ranch Hand

Joined: Nov 14, 2011
Posts: 76
Hi all,

I am posing this question because after quite a bit of reading, it seems to me that the only time CDI makes sense is if you absolutely will never ever port your JSF 2 app to a non-JEE container. I suppose in some cases this works, but it seems to me until a non-JEE container back ports CDI into their container (which would they ever?), using @ManageBean with the JSF 1.x scopes including ViewScope for ajax based JSF 2 apps, seems to be the best way to ensure your presentation layer will deploy on every container out there.

Is there a reason why you would want to use CDI in a JEE container over ManagedBean/Property? Do you gain anything extra by using CDI in the presentation layer in a JEE container? From what I've read I haven't found a good reason to use CDI other than if you are guaranteed never to deploy to a non-JEE container.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

I port JSF code to a non-JEE environment all the time: jUnit.

There is a strong possibility that CDI will, in fact eventually replace the platform-specific ManagedBean maps and annotations, since JSF was itself designed to minimize platform dependencies.

An IDE is no substitute for an Intelligent Developer.
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

In the specification JSR #344 it already make clear that JSF API that is like CDI API will be removed in next version (2.3).

The advantage of CDI is a specific and high quality framework for Dependency Injection with context. You can do a lot of thing with that (many many many really). Take a look on DeltaSpike which are tons of Utils built only over CDI (due it extension property). Another good source is the spec JSR #346 =) There you can find the features of CDI.


Please, visit me for some cool tech post at
john lazeraski
Ranch Hand

Joined: Nov 14, 2011
Posts: 76
This will be great when CDI is the only thing going, which I assume then that a JSF 2.3 container like Tomcat would support CDI at the time it's available. But that's years away given that 2.2 just came out, so for the forseeable future, if we're deploying in Tomcat, or Jetty, or what not, then ManagedBean seems the best route for now.
I agree. Here's the link:
subject: Why use CDI for beans and injection?
It's not a secret anymore!