The Java EE 6 Tutorial, Volume I

The Generator Managed Bean

The Generator managed bean contains the two producer methods for the application. The bean has the @ApplicationScoped annotation to specify that its context extends for the duration of the user's interaction with the application.

package guessnumber;


import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

public class Generator implements Serializable {

    private static final long serialVersionUID = -7213673465118041882L;

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

    private int maxNumber = 100;

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

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

    @Produces @MaxNumber int getMaxNumber() {
        return maxNumber;