Version 1.0.0.PREVIEW3 of Web Beans (the implementation for JSR 299 Contexts and Dependency Injection For Java EE) now uses the annotations fromJSR 330(Dependency Injection For Java) and it is available in GlassFish V3.
In this entry, we'll look at a simple JSF 2 application that uses Web Beans and the JSR 330 annotations. There are other features available in this release of Web Beans which will be discussed in subsequent blog entries. This application is a simple "guess number" application which I'm sure we are probably all familiar with by now. Let's start by taking a look at the application's UI markup.
Listing 1: User Interface Markup
Everything on this page is really just standard JSF 2.0 view markup. I've emphasized the Expression Language (EL) portions of the view, especially game because it refers to a contextual bean instance also known as a Web Bean.
- Line 17: Binding to Web Bean properties
- Line 21: Binding to Web Bean properties and validation method
- Line 22, 23: Binding to Web Bean action method
- Line 24, 25: Binding to Web Bean properties
As you can see, in JSF, binding to a Web Bean is no different than binding to a typical managed bean.
Supporting Classes And Annotations For The Applicaton
Let's take a look at some of the utility classes for the application, namely the classes that are used to generate a random number and some of the supporting annotations.
Listing 2: The "Random" Annotation
The JSR 330 @Qualifierannotation is used to make Random a binding type. This will allow us to inject a random number into the application.
Listing 3: The "MaxNumber" Annotation
Likewise, the @Qualifier annotation is used to makeMaxNumber a binding type that will allow us to inject the maximum number allowed (for a guess) in the application.
Listing 4: The "Generator" Class
- Line 7: An instance of this class exists for the lifecycle of the application
- Line 15: next() is a Web Beans producermethod. It will get called by the Web Beans Manager to obtain an instance of the next random number.
- Line 18: getMaxNumber() is also a Web Beansproducer method. It will get called by the Web Beans Manager to obtain an instance ofmaxNumber - essentially returning the maximum number to "guess" in the application (in this case "100").
The Anatomy of a Simple Contextual Bean
Now that we have are utility classes and annotations in place, we can use them in our Web Bean.
Listing 5: The "Game" Contextual Bean
I've color coded the areas of interest in this class. Theblue areas are JSR 299 (Web Beans) implementation details. The red areas are JSR 330 (Dependency Injection For Java) areas. The purple areas are the binding types.
- Line 14: We are using the Namedannotation to associate a name with this bean. Because there is no argument specified with the Named annotation, its name will be the name of the bean itself with the first letter lowercase (game). This allows us to reference the bean by that name using the Expression Language in the view.
- Line 15: We declare this bean as a session scoped bean, meaning it's lifecycle is the lifecycle of the session.
- Line 23: The producer method in the Generator class (line 18) will service this injection point causing maxNumber to have a value of "100".
- Line 29: The producer method in the Generator class (line 15) will service this injection point making a randomNumber instance available for this Web Bean.
- Line 72: We use a standard @PostConstruct annotation causing variable initialization (after this Web Bean is created).
- Line 78: Our Web Bean has been created, so the randomNumber.get() call will cause theproducer method in theGenerator class (line 15) to get called.
Well, that's a quick overview of a simple JSF2 application that uses the Web Beans (JSR 299 implementation) and the Java Dependency Injection (JSR 330) annotations. You can find this version of Web Beans in any of the GlassFish V3 builds after September 2, 2009.