Java ee6 standard CDI tutorial Part 4

Source: Internet
Author: User


--------------------------------------------------------------------------------

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.