3.2 three implementation methods of dependency Injection

Source: Internet
Author: User

 

3.2 three implementation methods of dependency Injection

Before explaining the three implementation methods of dependency injection, let's clarify the meaning of dependency injection: Make the component dependent on abstraction. When the component needs to be dependent on other actual objects, inject actual dependent objects through abstraction.

There are three implementation methods for dependency injection: interface injection, setter injection, and constructor injection ). Next, I will introduce the three implementation methods of dependency injection through examples.

3.2.1 interface Injection)

Interface injection refers to defining the information to be injected in the interface and completing the injection through the interface. The procedure is as follows.

(1) Compile an interface, ibusiness, and inject various databases through this interface. The sample code of ibusiness. Java is as follows:

// ******** Ibusiness. Java **************

Public interface ibusiness {

Public void createdi (Database dB );

}

(2) Any class that wants to use the database instance must implement this interface. The business logic class implements this interface ibusiness. The example Code of Business. Java is as follows:

// ******** Business. Java **************

Public class business implement ibusiness {

Private Database dB;

Public void createdi (Database dB ){

This. DB = dB;

}

......

// Obtain data from the database ××× Based on the injected Database Class

Public void getdata (){

......

DB. getdata ();

......

}

}

(3) Compile the test class testbusiness. The sample code of testbusiness. Java is as follows:

// ******** Testbusiness. Java **************

Public class testbusiness {

Private business = new business ();

......

// Obtain data from the Oracle database based on the injected Database Class

Public void getdata (){

......

Business. createdi (New oracledatabase ());

Business. getdata ();

......

}

}

If you want to inject dependency objects, you must implement the ibusiness interface.

3.2.2 set injection (setter injection)

Set injection refers to defining a set method in the class to be injected and defining the elements to be injected in the parameters. To allow business-like businesses to accept database injection, we need to define a set method for it to accept database injection. The example Code of Business. Java is as follows:

// ******** Business. Java **************

Public class business {

Private Database dB;

Public void setdatabase (Database dB ){

This. DB = dB;

}

......

// Obtain data from the database ××× Based on the injected Database Class

Public void getdata (){

......

DB. getdata ();

......

}

}

For more detailed code, see the second example in section 3.1, using the set injection method.

3.2.3 constructor Injection)

Constructor injection refers to defining a constructor in the class to be injected and defining the elements to be injected in the parameters. To allow business-like businesses to accept database injection, we need to define a constructor for it to accept database injection. The example Code of Business. Java is as follows:

// ******** Business. Java **************

Public class business {

Private Database dB;

Public Business (Database dB ){

This. DB = dB;

}

......

// Obtain data from the database ××× Based on the injected Database Class

Public void getdata (){

......

DB. getdata ();

......

}

}

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.