--------------------------------------------------------------------------------
Original link: http://code.google.com/p/jee6-cdi/wiki/DependencyInjectionAnIntroductoryTutorial
This section describes @ Producer.
1. Use @ Producer to determine what to create
But you may want to extract the method of the selector from AutomatedTellerMachineImpl.
You can create a Producer Method to Determine the creation and selection of the data transmitter. See the following example:
Example 1. TransportFactory determines which transmitter to use/create
Package org. cdi. advocacy;
Import javax. enterprise. inject. Produces;
Public class TransportFactory {
Private boolean useJSON = true;
Private boolean behindFireWall = true;
@ Produces ATM transport createTransport (){
// Look up config parameters in some config file or LDAP server or database
System. out. println ("ATM transport created with producer makes decisions ");
If (behindFireWall ){
If (useJSON ){
System. out. println ("Created JSON transport ");
Return new JsonRestAtmTransport ();
} Else {
System. out. println ("Created SOAP transport ");
Return new soapatm transport ();
}
} Else {
System. out. println ("Created Standard transport ");
Return new StandardAtmTransport ();
}
}
}
The process of separating the creation from the AutomatedTellerMachineImpl Generation Code is higher than that of other generation codes.
But if you do, producer can help you.
Output and front.
Output
ATM transport created with producer makes decisions
Created JSON transport
Deposit called
Communicating with bank via json rest transport
2. In @ Producer, use the limit clause to determine how to create the statement.
This example is created at the bottom.
In the same example, you can use the "add to" parameter as the parameter to input the parameter to the producer, as shown below:
Example 2. TransportFactory determines which transmitter to use/create
Package org. cdi. advocacy;
Import javax. enterprise. inject. Produces;
Public class TransportFactory {
Private boolean useJSON = true;
Private boolean behindFireWall = true;
@ Produces ATM transport createTransport (@ Soap ATM transport soapTransport,
@ Json ATM transport jsonTransport ){
// Look up config parameters in some config file
System. out. println ("ATM transport created with producer makes decisions ");
If (behindFireWall ){
If (useJSON ){
System. out. println ("return passed JSON transport ");
Return jsonTransport;
} Else {
System. out. println ("return passed SOAP transport ");
Return soapTransport;
}
& Nb