Spring IOC (Inversion of Control) and DI (Dependency Injection)

1. IOC: Inversion of Control.

Traditional programming method:  All objects and resources are controlled by developers, and you decide when to new an object, when to apply for resources, use resources, and release resources.

Control : refers to controlling the acquisition of external resources and controlling the life cycle of objects.

Inversion:  It was popular at first that developers control everything, but now it has changed, and the Spring framework controls external resources in the program, controls the life cycle of objects, and so on. So the name "inversion", that is, the right of control is transferred from the developer to the Spring framework. The Spring framework helps me create the dependent objects in our objects, and our objects can only be passively accepted.

The advantage of IOC is decoupling, and the coupling between objects becomes lower, which is convenient for testing and function reuse.


2. DI (Dependency Injection) translated into Chinese is called "dependency injection". Since the entire life cycle of the object is maintained by the Spring framework, then another object is referenced in my object. What do you plan to do? The Spring Framework naturally takes this into account. The two terms "dependency injection" should also be separated:

Dependency:  My A object references the B object, which means that the A object depends on the B object. You tell Spring the dependencies between your objects through configuration files .

Injection:  Your objects have been handed over to Spring for management, and you have also told Spring the dependencies between your objects, then when appropriate, Spring will inject other objects (or resources, constants, etc.) you depend on to you .

To sum up, give all the control to Spring, and let Spring help you create objects and help you maintain dependencies between objects .



3. The relationship between Inversion of Control and Dependency Injection

Inversion of Control (IOC) is a design principle in object-oriented programming that can be used to reduce the coupling between computer code. The most common way is called Dependency Injection (DI), and another is called "Dependency Lookup". Through inversion of control, when an object is created, an external entity that controls all objects in the system passes it a reference to the object it depends on. It can also be said that dependencies are injected into objects.

4. Why IOC and DI are needed

  Why Dependency Injection (a design code pattern)? Because we want inversion of control (the idea of ​​designing code).

Why Inversion of Control? Because our software design needs to conform to the software design principle, dependency inversion (design code principle), single responsibility principle.

To put it simply, we need to decouple. The MVP pattern is a more comprehensive design pattern model for decoupling.

5. Dependency injection generally includes 1. Construct injection 2. Interface injection 3. Set value injection.

  Spring does not use interface injection (violating Spring's claimed non-intrusive principle (lives without it)). Use setpoint injection.

  Setter injection refers to passing in the instance of the callee through the setter method . This injection method is simple and intuitive, so it is widely used in Spring 's dependency injection .

   Example:  

   //This is the interface of a Person

    public interface Person {

        // a method that uses an axe (abstract method, not implemented)
        public void UseAxe ();

    }

   //Define an Axe interface

   public interface Axe {

        //The axe has a method to chop Chop

        public void Chop();

   }

 //An implementation of Axe like StoneAxe

 publice class StoneAxe implements Axe {

     //default constructor
    public StoneAxe(){

    }

    // Implement the chop method of the Axe interface

    public String Chop () {

        return "Stone Axe";
   }

}

 
  //The realization of Person is like Chinese

  public class Chinese implements Person {

      // Interface Axe programming for axe, not implementation classes
      private Axe axe;

      //default constructor

     public Chiniese() {

     }

     // Set the required setter method for injection

     public void setAxe (Axe axe) {

         this.axe = axe;
     }

     / / Implement the method of using the axe of the Person interface

     public void UseAxe {
       System.out.println(axe.Chop());

     }



  }

Next, use the configuration file of spring to associate the implementation class Chinese of Person with the implementation class StoneAxe of Axe Axe.

<!-- Below is the standard XML file header-->
<?xml version="1.0" encoding="gb2312"?>
<!-- The following line defines the dtd of Spring's XML configuration file -->
"http://www.springframework.org/dtd/spring-beans.dtd">
<!-- The above three lines are the same for all Spring configuration files -->
<!-- The root element of the Spring configuration file-->
<BEANS>
    <!—Define the first bean, the id of the bean is Chinese, and the class specifies the implementation class of the bean instance -->
    <BEAN class=lee.Chinese id=chinese>
        <!-- The property element is used to specify the properties that need to be injected by the container. The axe property needs to be injected by the container. Here is the set value injection, so the Chinese class must have the setAxe method -->
        <property name="axe">
            <!-- here injects another bean reference to the chinese bean -->
            <REF local="”stoneAxe”/">
        </property>
</BEAN>
<!-- define stoneAxe bean -->
<BEAN class=lee.StoneAxe id=stoneAxe />
</BEANS>

The following is the main program part:

public class PersonTest{
  //The main method, the entry of the program
  public static void main (String[] args) throw exception {
     //Because it is a standalone application, the context of instantiating Spring shown here
     ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");
     //Get the bean instance through the id of the Person's bean, interface-oriented programming, so it is forced to be converted to an interface class here
     Person p = (Person)ctx.getBean("chinese");
     //Directly execute Person's UseAxe method
     p.UseAxe();
  }
}

The execution result of the above program: stone axe


*From: https://www.cnblogs.com/xxzhuang/p/5948902.html

                       https://blog.csdn.net/u011068996/article/details/76445379

                       https://blog.csdn.net/qin_zhimou/article/details/52662123

                      https://blog.csdn.net/baoendemao/article/details/24634835 *Explain the four ways of dependency injection in spring




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326009959&siteId=291194637