JBoss Weld CDI for Java Platform is a relatively short book (approximately 100 "main" pages) that introduces JBoss Weld, which it describes as "the open source reference implementation" of Java Contexts and Dependency Injection. Weld is the reference implementation of Java Specification Request 299 ("Contexts and Dependency Injection for the Java EE platform") and is not limited to use with JBoss Application Server. This post is my review of Packt Publishing's JBoss Weld CDI for Java Platform by Ken Finnigan.
The Preface of JBoss Weld CDI for Java Platform explains that the book is written for "anyone wanting to understand what CDI 1.0 is and how it can be used to benefit an application's architecture." This Preface also states that the latest version of Weld 1.x is required along with Maven and a "favorite runtime container" (out of JBoss AS7, GlassFish, and Tomcat).
Chapter 1: What is a Bean?
The opening chapter of JBoss Weld CDI for Java Platform provides a brief history of Java "beans" that includes mention of early JavaBeans, Enterprise JavaBeans, and JSF Managed Beans. There are, of course, many more types of "beans" in Java such as JMX Managed Beans (MBeans and MXBeans) and product-specific beans like Spring Framework's beans.
The initial chapter goes on to provide a definition of a CDI bean. Finnigan explains that a CDI bean generally "needs a non-private constructor with no parameters" to be "proxied by the container" before discussing use of the @Inject annotation for classes without a no-argument public constructor. There is a typo here as the text reads, "It is also possible for a bean to be proxied by the container if it does not have a constructor with any parameters," but I think the text should read, "It is also possible for a bean to be proxied by the container if it does not have a constructor without parameters" or "It is also possible for a bean to be proxied by the container if it does not have a constructor no parameters."
Chapter 1 of JBoss Weld CDI for Java Platform introduces several other annotations besides @Inject; these include @Dependent, @RequestScoped, @ApplicationScoped, @SessionScoped, @ConversationScoped, and @Alternative. The chapter includes coverage of the beans.xml file and discusses where it should be placed and what it should contain.
The first chapter continues its introduction to CDI beans by quoting the CDI specification of the characteristics required of a CDI bean and then focuses on each characteristic (bean types, qualifiers, scope, interceptor bindings, bean implementation, and optional bean expression language [EL]). These introductions to the characteristics of a CDI bean introduce more annotations such as @Typed, @Qualifier, and @Named.
Chapter 2: Dependency Injection and Lookup
The second chapter of JBoss Weld CDI for Java Platform begins with a deeper look at dependency injection using Weld. Finnigan describes the ability to inject dependencies at class level (via no-arguments constructor or constructor annotated with
@Inject), at get/set method level, or at field level. I was happy to see that he specifically mentions constructor setting and field level setting of injection points as preferable to method level setting to support creation of immutable objects. (I think the importance and value of immutability (when possible) in Java objects is still under-appreciated.)
The second chapter provides more focus on qualifiers (including
@Default annotations), alternatives, and client proxies, and injection point metadata.
I like that Finnigan talks about some likely errors (including error message output) that can occur when configuration problems exist. I wish more introductory books did this, focusing on things that often go wrong rather than simply covering the happy path of using the framework or tool being introduced. Finnigan includes a section in this chapter called "Resolving Weld deployment errors" that lists the common types of errors that can occur with Weld deployments (unsatisfied dependencies and ambiguous dependencies) and then lists multiple ways to resolve each.
Chapter 3: Deploying JBoss Weld
The third chapter of JBoss Weld CDI for Java Platform covers "installation of JBoss Weld into Java EE 6 and Servlet containers." Finnigan points out that "all Java EE 6 containers will come with an implementation of CDI preinstalled," but also makes the point that it's worth knowing how to install or deploy Weld because the version bundled with the container may be insufficient. The chapter talks about how to download Weld and mentions that the current version as of the book's writing was 1.1.10 (at the time of this blog post's writing, Weld 1.2 beta and Weld 2.0 are available for download).
Chapter 3 concludes with a useful discussion on upgrading of Weld versions within three containers: JBoss AS 7 (7.1.1) with bundled Weld 1.1.5Final, GlassFish 3 (220.127.116.11) with bundled Weld 1.1.8Final, and Apache Tomcat 7.0.35 (no bundled Weld implementation because Tomcat is a web servlet implementation).
Chapter 4: Scopes and Contexts
JBoss Weld CDI for Java Platform's fourth chapter delves deeper into the concepts of scopes and contexts introduced earlier in the first chapter. This fourth chapter introduces the concepts of normal and pseudo scopes. Finnigan explains that the differentiating factor between normal scope and pseudo scope is whether a client proxy is required or not. The chapter provides a relatively large section on the built-in scopes introduced earlier and describes the underlying respective lifecycles: request context, session context, application context, and conversation context. I liked that Finnigan wanrns about conversation scope in CDI 1.0 being specific to JSF, but mentioning that CDI 1.1 (JSR 346) is expected to make conversation scope more generally available.
The majority of Chapter 4 is spent on normal, built-in scopes and their associated contexts. In particular, a significant portion of the chapter is dedicated to coverage of using the conversational scope with JSF. The chapter, however, does briefly describe pseudo scopes and concludes with an example of writing custom scope and context.
Chapter 5: Producers
The fifth chapter in JBoss Weld CDI for Java Platform provides greater focus on another topic introduced in the first chapter: producers with @Produces and @Disposes. This chapter's coverage includes some more highly useful side notes with special emphasis that discuss the use of @New, but also mention that
@New is deprecated in CDI 1.1 in favor of
@Dependent at the injection point.
Chapter 6: Interceptors and Decorators
Chapter 6 of JBoss Weld CDI for Java Platform introduces interceptors and decorators and differentiates them, "Interceptors are perfect for separating concerns that are orthogonal to our application ... Decorators are well suited for solving business problems that apply to a hierarchy of classes of the same type."
This chapter provides relatively in-depth coverage of created interceptors and decorators. I like the fact that the examples, especially the interceptors examples, include some complicating issues to take the examples past "Hello World" status and to be more reflective of what one might do with these constructs.
Chapter 7: Events
The seventh chapter of JBoss Weld CDI for Java Platform covers "events within CDI" including "how they are fired" (javax.enterprise.event.Event.fire), "how we listen for new events" (@Observes), "what we can use as an event payload" (concrete Java classes), and "how we can narrow what events we can listen to and fire" (event qualifiers and conditional observers).
Chapter 8: Writing a Portable Extension
Chapter 8 is the last chapter of JBoss Weld CDI for Java Platform that focuses on detailed coverage of Weld concepts in an purely introductory fashion because Chapter 9 and Chapter 10 focus on an example book store application. This chapter's focus is use of CDI-exposed Service Provider Interfaces (SPIs) that allow extension and customization of CDI. The chapter outlines the development of portable CDI extensions by implementing javax.enterprise.inject.spi.Extension and registering the extension in
META-INF/services/javax.enterprise.inject.spi.Extension. The chapter discusses the "core" role of BeanManager in allowing extensions to access the CDI lifecycle. Chapter 8 also mentions Apache DeltaSpike, a product which I had never heard of, but which its homepage advertises as "a number of portable CDI extensions that provide useful features for Java application developers."
Chapter 9: Book Store – CDI Services
JBoss Weld CDI for Java Platform's ninth chapter is the first of two chapters that use an online book store application as a CDI/Weld example intended to "provide a more indicative picture of how (Weld CDI) can be utilized," to "cover some of the CDI usage patterns that can be beneficial," and to "provide clear examples of topics that we have discussed in the previous chapters." Chapter 9 introduces the example application and demonstrates building its "back end" (services, interceptors, and events portions).
Chapter 10: Book Store – User Interfaces
The tenth and final chapter continues the example online bookstore application started in Chapter 9. This chapter focuses on the "front end" of the example application and demonstrates "how CDI services can be reused to create two distinct user interfaces." The two technologies used for the interfaces are AngularJS and JSF2/Rich Faces. In the book's example application, REST is used for communication between the user interfaces and the services.
- General CDI Specification (not just Weld) Resource - Although the book title and some of its content suggest focus on the CDI implementation Weld, the vast majority of the book is likely applicable to most common implementations of CDI (Chapter 3 being about the only significant exception that is highly Weld-specific rather than CDI-general). I find it often to be the case that books on reference implementations are good sources of information on the specification being implemented and that is definitely the case with JBoss Weld CDI for Java Platform; it can serve as simply "CDI for Java Platform."
- Forward-looking - Technology books, especially those focused on new implementations of ongoing specifications, always come with great risk of being outdated quickly. Finnigan's mention of CDI 1.1 in several parts of the book helps to mitigate this risk at least a bit.
- Appropriate Level of Detail - This is not a very lengthy book, but its length feels appropriate. There is nice introductory discussion along with discussion on some things to do and not do with CDI/Weld. There are several code examples, but most are short and easy to follow. The lengthy and more difficult to follow examples are reserved for the final two chapters on the sample application.
I really enjoyed JBoss Weld CDI for Java Platform and recommend it to anyone interested in learning about Java EE CDI in general or the Weld implementation of CDI in particular. Other than a few typos such as the one I mentioned in this post, the text is generally well-written. I particularly like the text's mixing of introductory material with forward-looking caveat statements and discussion of why certain practices are beneficial or detrimental in different circumstances.
Original posting available at http://marxsoftware.blogspot.com/ (Inspired by Actual Events)