Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  JavaServerTM Faces Technology

5.  Introduction to Facelets

6.  Unified Expression Language

7.  Using JavaServerTM Faces Technology in Web Pages

8.  Using Converters, Listeners and Validators

9.  Developing With JavaServerTM Faces Technology

10.  Java Servlet Technology

Part III Web Services

11.  Introduction to Web Services

12.  Building Web Services with JAX-WS

13.  Building RESTful Web Services with JAX-RS and Jersey

Part IV Enterprise Beans

14.  Enterprise Beans

15.  Getting Started with Enterprise Beans

16.  Running the Enterprise Bean Examples

Part V Contexts and Dependency Injection for the JavaTM EE Platform

17.  Introduction to Contexts and Dependency Injection for the JavaTM EE Platform

Overview of Contexts and Dependency Injection for the Java EE Platform

About Beans

About Managed Beans

Beans as Injectable Objects

Using Qualifiers

Injecting Beans

Using Scopes

Giving Beans EL Names

Adding Setter and Getter Methods

Using a Managed Bean in a Facelets Page

Configuring a CDI Application

Further Information

18.  Running the Basic Contexts and Dependency Injection Examples

Part VI Persistence

19.  Introduction to the Java Persistence API

20.  Running the Persistence Examples

21.  The Java Persistence Query Language

22.  Creating Queries Using the Criteria API

Part VII Security

23.  Introduction to Security in the Java EE Platform

24.  Getting Started Securing Enterprise Applications

25.  Getting Started Securing Web Applications

Part VIII JavaTM EE Supporting Technologies

26.  Introduction to JavaTM EE Supporting Technologies

27.  Transactions

28.  Resource Connections

Index

 

Injecting Objects by Using Producer Methods

Producer methods provide a way to inject objects that are not beans, objects whose values may vary at run time, and objects that require custom initialization.

For example, if you want to initialize a numeric value defined by a qualifier named @MaxNumber, you can define the value in a managed bean and then define a producer method, getMaxNumber, for it:

    private int maxNumber = 100;
    ...
    @Produces @MaxNumber int getMaxNumber() {
        return maxNumber;
    }

When you inject the object in another managed bean, the container automatically invokes the producer method, initializing the value to 100:

    @Inject @MaxNumber private int maxNumber;

If the value can vary at run time, the process is slightly different. For example, the following code defines a producer method that generates a random number defined by a qualifier called @Random:

    private java.util.Random random = 
        new java.util.Random( System.currentTimeMillis() );

    java.util.Random getRandom() {
        return random;
    }

    @Produces @Random int next() {
        return getRandom().nextInt(maxNumber);
    }

When you inject this object in another managed bean, you declare a contextual instance of the object:

    @Inject @Random Instance<Integer> randomInt;

You then call the get method of the Instance:

this.number = randomInt.get();