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 ();
......
}
}