Documentation



Java Platform, Enterprise Edition: The Java EE Tutorial

23.5 Beans as Injectable Objects

The concept of injection has been part of Java technology for some time. Since the Java EE 5 platform was introduced, annotations have made it possible to inject resources and some other kinds of objects into container-managed objects. CDI makes it possible to inject more kinds of objects and to inject them into objects that are not container-managed.

The following kinds of objects can be injected:

  • (Almost) any Java class

  • Session beans

  • Java EE resources: data sources, Java Message Service topics, queues, connection factories, and the like

  • Persistence contexts (Java Persistence API EntityManager objects)

  • Producer fields

  • Objects returned by producer methods

  • Web service references

  • Remote enterprise bean references

For example, suppose that you create a simple Java class with a method that returns a string:

package greetings;

public class Greeting {
    public String greet(String name) {
        return "Hello, " + name + ".";
    }
}

This class becomes a bean that you can then inject into another class. This bean is not exposed to the EL in this form. Giving Beans EL Names explains how you can make a bean accessible to the EL.

Close Window

Table of Contents

Java Platform, Enterprise Edition: The Java EE Tutorial

Expand | Collapse