In Chapter 2nd, I use two simple examples to demonstrate the IOC function of spring. Next I will explain in detail the IOC function of spring, because the core of spring is IOC. In this chapter, we first start with the basic idea of IOC, and then use instances to give readers an in-depth understanding of its concept and working principles, at last, the first instance in Chapter 2nd will be adapted to implement the same function through constructor.
3.1 reverse control/dependency Injection
In recent years, the Java Community has set off a boom in lightweight containers. Almost every once in a while, new lightweight containers have emerged, these lightweight containers can help developers quickly assemble different components into an application. Behind these lightweight containers, there is a common pattern that determines the way containers assemble components, that is, "reverse control", that is, IOC. The full name is inversion of control. Martin Fowler thoroughly explored the working principle of "Reverse control" and gave it a new name called "dependency injection", namely Di, which is the full name of dependency injection. For this article about Martin Fowler, you can view it on its website at http://www.martinfowler.com/articles/injection.html.
3.1.1 Reverse Control (IOC)
Literally, it is difficult to understand the meaning of "reverse control. In programming, developers often say that "implementation must depend on abstraction, rather than abstract dependency implementation" is a form of Reverse control. Below, I will give an example to illustrate this abstract concept. This example mainly describes how to use IOC to retrieve data from which database the business logic is implemented. There are three possible data retrieval methods:
● Retrieve data from the SQL Server database.
● Retrieve data from the DB2 database.
● Retrieve data from the Oracle database.
The idea of introducing this example is: first introduce the general practice of writing this type of program, then point out the shortcomings of this practice, and then give a better practice, that is, using IOC to implement this type of function, finally, we will summarize this practice so that readers can understand IOC step by step. The procedure for writing such programs is as follows:
(1) generally, this type of program is written first, namely, sqlserverdatabase. Java, which retrieves data from the SQL Server database. The sample code of sqlserverdatabase. Java is as follows. Getdatafromsqlserver () is a method in the sqlserverdatabase class. It is responsible for retrieving data from the SQL Server database.
// ******** Sqlserverdatabase. Java **************
Public class sqlserverdatabase {
......
// Obtain data from the sqlserver Database
Public list getdata (){
......
}
}
(2) Business Logic class business. Java uses methods in sqlserverdatabase. Java to retrieve data from the SQL Server database. The example Code of Business. Java is as follows. Sqlserverdatabase is a class used to retrieve data from the SQL Server database.
// ******** Business. Java **************
Public class business {
Private sqlserverdatabase DB = new sqlserverdatabase ();
......
// Obtain data from the SQL Server database
Public void getdata (){
......
List list = dB. getdatafromsqlserver ();
......
}
}
We can see the shortcomings of the above programming: the business class depends on the sqlserverdatabase class. If the business changes and the user needs to retrieve data from the DB2 or Oracle database, this program will not apply, the business class must be modified.
(3) to retrieve data from the DB2 database, the sample code of db2database. Java is as follows. Getdatafrom DB2 () is a method in the db2database class. It is responsible for retrieving data from the DB2 database.
// ******** Db2database. Java **************
Public class db2database {
......
// Obtain data from the DB2 database
Public list getdata (){
......
}
}
(4) the business logic class business. Java must be modified to retrieve data from the DB2 database. The example Code of Business. Java is as follows. db2database is a class used to retrieve data from the DB2 database.
// ******** Business. Java **************
Public class business {
Private db2database DB = new db2database ();
......
// Obtain data from the DB2 database
Public void getdata (){
......
List list = dB. getdatafromdb2 ();
......
}
}
(5) Similarly, if the user needs to retrieve data from the Oracle database, this program will not apply. The sample code of oracledatabase. Java is as follows. Getdatafromoracle () is a method in the oracledatabase class and is responsible for retrieving data from the Oracle database.
// ******** Oracledatabase. Java **************
Public class oracledatabase {
......
// Obtain data from the Oracle database
Public list getdata (){
......
}
}
(6) modify the business logic class business. Java to retrieve data from the Oracle database. The example Code of Business. Java is as follows. Here, oracledatabase is a class used to retrieve data from the Oracle database.
// ******** Business. Java **************
Public class business {
Private oracledatabase DB = new oracledatabase ();
......
// Obtain data from the Oracle database
Public void getdata (){
......
List list = dB. getdatafromoracle ();
......
}
}
So far, the reader should be able to find out that this is not a good design, because every change in business needs will lead to a large number of modifications to the program. How can we change this situation? How can we achieve business class reuse? IOC can solve this problem, which can be changed through abstract programming.
The following describes how to use IOC to reuse business classes. The writing idea is: first, compile an interface for getting data, and then implement this interface for each class that is responsible for obtaining data from various databases, in the business logic class, programming based on the interface does not deal with the specific class for obtaining data. The procedure for implementing this function through IOC is as follows.
(1) Compile the database interface used to obtain data. The sample code for database. Java is as follows:
// ******** Database. Java **************
Public interface database {
// This method is used to obtain data.
Public void getdata ();
}
(2) Compile the sqlserverdatabase class that is responsible for retrieving data from the SQL Server database, which implements the interface database. The sample code of sqlserverdatabase. Java is as follows:
// ******** Sqlserverdatabase. Java **************
Public class sqlserverdatabase implement database {
// This method is used to obtain data.
Public void getdata (){
// The following code retrieves data from the SQL Server database
......
}
}
(3) Compile the business logic class, which is only encoded for the interface database, not for the entity class. The example Code of Business. Java is as follows:
// ******** Business. Java **************
Public class business {
// Define variables for the interface Database
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 ();
......
}
}
(4) 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 SQL Server database based on the injected Database Class
Public void getdata (){
......
Business. setdatabase (New sqlserverdatabase ());
Business. getdata ();
......
}
}
In this way, the business class can be reused. No matter which database is used to obtain data, the business class does not need to be changed. You only need to implement the specific database interface. For example, if you want to retrieve data from a DB2 database, you only need to implement a class that is responsible for retrieving data from the DB2 database.
(5) write the class db2database that is responsible for retrieving data from the DB2 database, which implements the interface database. The sample code of db2database. Java is as follows:
// ******** Db2database. Java **************
Public class db2database implement database {
Public void getdata (){
// The following code retrieves data from the DB2 database:
......
}
}
(6) modify the test class testbusiness without making any changes to the business logic. The sample code of testbusiness. Java is as follows:
// ******** Testbusiness. Java **************
Public class testbusiness {
Private business = new business ();
......
// Obtain data from the DB2 database based on the injected Database Class
Public void getdata (){
......
Business. setdatabase (New db2database ());
Business. getdata ();
......
}
}
(7) If you want to retrieve data from the Oracle database again, you only need to implement a specific class to retrieve data from the Oracle database. Write the oracledatabase class responsible for retrieving data from the Oracle database, which implements the interface database. The sample code of oracledatabase. Java is as follows:
// ******** Oracledatabase. Java **************
Public class oracledatabase implement database {
Public void getdata (){
// The following code retrieves data from the Oracle database:
......
}
}
(8) modify the test class testbusiness without making any changes to the business logic. 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. setdatabase (New oracledatabase ());
Business. getdata ();
......
}
}
From the above example, we can see that in the first example, the business class depends on the class that obtains data, and in the second example, It is programmed through the interface, that is, the reverse transfer of control relationships enables the IOC function and reuse the code. This implements the "implementation must depend on abstraction rather than abstract dependency implementation" mentioned above ".
3.1.2 dependency injection (DI)
In his article, Martin Fowler raised the question of "what aspects of control have they reversed", and then gave IOC a new name that better illustrates the features of this pattern, it is called "dependency injection", that is, dependency injection. Spring uses dependency injection to implement IOC functions. Then Martin Fowler introduces three ways to implement dependency injection, next, I will give a detailed explanation of the three implementation methods based on the above examples.