Monday, May 20, 2013

Contexts and Dependency Injection(CDI) - Producer


@Produces is a CDI mechanism which allows defining a source for injection. One can use Producer Method or Producer field to generate objects.


Here is brief explanation from JEE 6 tutorial:

A producer method generates an object that can then be injected. Typically, you use producer methods in the following situations:
  • When you want to inject an object that is not itself a bean
  • When the concrete type of the object to be injected may vary at runtime
  • When the object requires some custom initialization that the bean constructor does not perform
(Code snippet from attached Source)


A producer field is a simpler alternative to a producer method; it is a field of a bean that generates an object. It can be used instead of a simple getter method. Producer fields are particularly useful for declaring Java EE resources such as data sources, JMS resources, and web service references.

(Code snippet from attached Source)

This is how an Object in Injected

(Code snippet from attached Source)


This example shows both ways of declaring a producer and Injection.

Click here to download source code


Prasanna Bhale

Wednesday, May 15, 2013

JSF2.2 ViewScope Using CDI

JSF 2.0 introduced annotation @ViewScoped; A bean annotated with this scope maintained its state as long as the user stays on the same view(reloads or navigation - no intervening views).

One problem with the @ViewScoped is that it doesn't work with CDI (@Named); so one had to annotate bean as @ManagedBean to be ViewScoped.

JSF2.2 has introduced a NEW annotation @ViewScoped in javax.faces.view.ViewScoped (NOT reusing the existing javax.faces.bean.ViewScoped) which is CDI compatible.

Here is a working sample using Glassfish B4 v72.

Click here to download source code




Prasanna Bhale


Tuesday, May 14, 2013

JSF 2.2 Faces Flow

JSF 2.2 Faces Flow           

As the API for JSF 2.2 is evolving, I struggled a bit to get Arun Gupta's Faces Flow sample code (from blog JSF 2.2 Faces Flow )working.

I have finally got it to work after some minor API/code changes and so, here with sharing with rest of developer's community.

Download the source code from here

Click here to download source code


Prasanna Bhale