Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

Define a producer field or method

04.23.2011
| 5678 views |
  • submit to reddit

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

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: