Inversion of Control and dependency injection

Inversion of Control (IOC), Dependency Injection (DI)

What is inversion of Control? Simply put, inversion of control is the idea of changing the creation of an object from active to passive. The following example is an active new object.

public class Service{
    public DependObject object1 = new DenpendObject();
}
Copy the code

So how do you do this without using new? Dependency injection is one way to do this.

There are three ways to do dependency injection

  • Constructor injection
  • Setter injection
  • Interface injection
public class Service{
    public DependObject object;

    // Constructor injection
    public Service (DependObject obj){
        this.object = obj;
    }

    / / setter injection
    public void setDependObject(DependObject obj){
        this.object = obj; }}Copy the code

As you can see, the life cycle of an object is no longer created actively in the Service class, but is assigned by injection. By the way, compare the advantages and disadvantages of the three methods:

  • Interface injection: In terms of the use of the injection mode, interface injection is not advocated very much now, and is basically in the “retired state”. It is intrusive because it forces the injected object to implement an unnecessary interface. This is not required for constructor injection and setter method injection.
  • Constructor injection: The advantage of this type of injection is that the object is ready for use immediately after construction. The disadvantage is that when there are many dependent objects, the constructor argument list can be long. When the object is constructed by reflection, it is difficult to deal with the parameters of the same type, and it is also troublesome to maintain and use. Also, in Java, constructors cannot be inherited and default values cannot be set. For non-essential dependency handling, multiple constructors may need to be introduced, and changes in the number of parameters may cause maintenance inconvenience.
  • Setter method injection: Because methods can be named, setter method injection is more descriptive than constructor injection. In addition, setter methods can be inherited, allowing default values to be set, and there is good IDE support. The downside, of course, is that the object has no way to enter the ready state immediately after construction.

The IOC container

For all objects to be managed uniformly, there needs to be a role that binds these interdependent objects together, and the IOC container is such a role. Its main responsibilities can be divided into two:

  1. Build management of business objects: for example, how to load classes, manage the life cycle of classes, etc.
  2. Dependency binding between business objects: Provide built objects for use by the business side.

The bottom line is create and bind

How does the IOC container know which objects to manage? This series will be implemented in two ways: 1. Using the encoding declaration, 2. Using the @bean annotation, and for those familiar with Spring and familiar with the common XML declaration, you can implement this yourself.

In Spring, the concept of beans is used to represent ioc-managed objects, and this definition is still used here. The next article describes the structure of the IOC module.

Develop reading

If you are not familiar with the basic IOC concepts, read Spring Disclosure 2.1 Basic IOC Concepts