Java Reference
In-Depth Information
Click here to view code image
private final static int TEST = 1;
private final static int SHIFT = 2;
private int coderType = SHIFT; // default value
The producer method, annotated with @Produces and @Chosen as well as
@RequestScoped (so that it lasts only for the duration of a single request and re-
sponse), takes both implementations as arguments, then returns one or the other, based on
the coderType supplied by the user.
Click here to view code image
@Produces
@Chosen
@RequestScoped
public Coder getCoder(@New TestCoderImpl tci,
@New CoderImpl ci) {
switch (coderType) {
case TEST:
return tci;
case SHIFT:
return ci;
default:
return null;
}
}
Finally, the managed bean injects the chosen implementation, specifying the same quali-
fier as that returned by the producer method to resolve ambiguities:
@Inject
@Chosen
@RequestScoped
Coder coder;
The Facelets page contains modified instructions and a pair of radio buttons whose selec-
ted value is assigned to the property coderBean.coderType :
Click here to view code image
<h2>String Encoder</h2>
<p>Select Test or Shift, type a string and an integer, then
click
Encode.</p>
<p>If you select Test, the TestCoderImpl bean will display the
Search WWH ::




Custom Search