JBoss Weld CDI for Java Platform (2013)
Preface
The CDI specification standardized the process of dependency injection for Java EE, opening the door to efficient integration with components and frameworks for your applications. JBoss Weld is the open source reference implementation for CDI that simplifies the development of applications with dependency injection.
JBoss Weld CDI for Java Platform will explain dependency injection with JBoss Weld and how you can use it to ensure that your applications take advantage of type safety, making your applications easier to debug and maintain. It is filled with information on what scopes CDI provides for your applications, how to fire and listen to events, creating new beans with producers, interceptors, and decorators, and developing portable extensions.
JBoss Weld CDI for Java Platform will have you up and running with CDI on JBoss Weld in a short space of time. Once we've covered the main topics of CDI, we will develop a simple application using CDI services with REST endpoints that we connect to from JSF and AngularJS.
What this book covers
Chapter 1, What is a Bean?, provides an overview of CDI beans and their history, before explaining how a Java class can be a bean.
Chapter 2, Dependency Injection and Lookup, explains injection and lookup functions, by understanding qualifiers and injection points. We also cover some possible Weld dependency errors and how our beans are proxied.
Chapter 3, Deploying JBoss Weld, covers how to deploy JBoss Weld to JBoss AS, GlassFish, and Tomcat.
Chapter 4, Scopes and Contexts, explains the scopes that are provided by CDI and how they should be used. We also explain pseudo scopes and creating a custom scope.
Chapter 5, Producers, covers producer methods and fields, injecting into producer methods, and how we can clean up beans that we produce.
Chapter 6, Interceptors and Decorators, explains how to create and enable an interceptor, and how their bindings are defined. We also look at enabling decorators and what is a decorator delegate.
Chapter 7, Events, explains how to listen for and fire events, and what type of payload can be sent. We also explain advanced event qualifiers and how to listen for events during specific transaction phases.
Chapter 8, Writing a Portable Extension, explains how to create an extension to CDI and what events the container fires that we can utilize. We then cover some examples of what can be achieved within an extension.
Chapter 9, Book Store – CDI Services, covers the development of CDI services, REST endpoints, and communicating with a database for our example application.
Chapter 10, Book Store – User Interfaces, explains how to develop a JSF admin interface and a user interface using AngularJS for our example application that will interact with our services.
What you need for this book
To be able to run the examples from this book, you should have:
· Any operating system based on Linux, Mac OS X, or Windows
· Java Development Kit (JDK) 1.6 or 1.7
· Apache Maven (latest version)
· JBoss Weld (latest 1.x version)
· Your favorite runtime container: JBoss AS7, GlassFish, or Apache Tomcat
Who this book is for
This book is for anyone wanting to understand what CDI 1.0 is and how it can be used to benefit an application's architecture. Experience with Java is required, but only so far as is needed to understand the coding constructs of the language. RESTful architecture, AngularJS, and Java Server Faces (JSF) skills are suggested, though not essential.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "In the preceding examples we specified @RequestScoped."
A block of code is set as follows:
@RequestScoped
public class MySecondBean {
MyFirstBean firstBean;
@Inject
public MySecondBean(MyFirstBean firstBean) {
this.firstBean = firstBean;
}
}
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.