Over a million developers have joined DZone.
Platinum Partner

Define a producer field or method

· Java Zone

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

A producer method or field is annotated with the javax.enterprise.inject.Produces annotation.

Define a producer field

A producer field acts as a getter method and it is a simple alternative to a producer method. A producer field is declared by annotating a field of a bean class with the @Produces annotation. Usually, a producer field is also annotated with a CDI qualifier. Per example, a producer field can be used to inject a random number into a bean or non-Java context.

Note: Producer fields may have a primitive bean type - primitive types are considered to be like their corresponding wrapper types in java.lang (Integer, Float, Byte etc).

First, you write a qualifier (this is the recommended technique). Let us name it RandomQualifier (since you have a field qualifier, the target contains only the FIELD value):

@Qualifier
@Retention(RUNTIME)
@Target({FIELD})
public @interface RandomQualifier {
}

Moreover, the producer field that will inject a random int number is defined in the class RandomGenerator:
public class RandomGenerator {
    @Produces @RandomQualifier public int nr = new Random().nextInt(4);
}

Now, you can get a random number anywhere. You can have an injection point into a bean, like below:
@Inject @RandomQualifier int nr;

On the other hand, if you want a random number into a non-Java context, like a JSF page, then you should add the @Named annotation into the producer field annotations suite:
@Produces @Named @RandomQualifier public int nr = new Random().nextInt(4);

Now, the EL can sustain the injection of a random number into a JSF page:
<h:outputText value="${nr}" />

Define a producer method

A producer method is a method that provides bean instances when no instance exists in the specified context – the container will invoke this method for getting an instance, while the application is responsible to take care of bean instantiation process. Per example, let us write a producer method that randomly returns a command strategy based on your producer field. You can start with the producer method qualifier, named CommandQualifier:
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD})
public @interface CommandQualifier {
}

Next, the producer method itself is declared as below (named, generateCommandStrategy):
@Produces @CommandQualifier public CommandStrategy generateCommandStrategy() {
        switch (nr) {
            case 1:
                return new SingleCommandStrategy();
            case 2:
                return new MultipleCommandStrategy();
            case 3:
                return new CustomCommandStrategy();
            default:
                return new BasicCommandStrategy();
        }
    }

Above, you have isolated the generateCommandStrategy producer method. For the complete picture, here it is the entire CommandStrategy bean – notice how the injected producer field (nr) was used just any other Java field.
public class CommandStrategy {

    @Inject @RandomQualifier int nr;

    @Produces @CommandQualifier public CommandStrategy generateCommandStrategy() {
        switch (nr) {
            case 1:
                return new SingleCommandStrategy();
            case 2:
                return new MultipleCommandStrategy();
            case 3:
                return new CustomCommandStrategy();
            default:
                return new BasicCommandStrategy();
        }
    }
}

Now, the producer method result can be injected. The injection point will have the same type and qualifier annotations as the producer method. The producer method will be called by the container to obtain an instance to service the injection point.
@Inject @CommandQualifier CommandStrategy injectedStrategy;

 

From http://e-blog-java.blogspot.com/2011/04/define-producer-field-or-method.html

The Java Zone is brought to you in partnership with JetBrains.  Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}