To understand the IOC, Spring IOC is enough to read this!

IOC and AOP belong to the core content of Spring. If you want to master Spring, you must have enough knowledge about IOC @mikechen

Definition of IOC

IOC is the abbreviation of Inversion of Control, and most books translate it as "Inversion of Control".

IOC is not a technology, but an idea, an important principle of object-oriented programming, which can guide us how to design loosely coupled and better programs.

In traditional applications, we actively create dependent objects inside the class, resulting in high coupling between classes and difficult testing. With the IoC container, the control of creating and finding dependent objects is handed over to the container, as shown in the following figure shown:

The above figure introduces the IOC container, so that the four objects A, B, C, and D have no coupling relationship. The transmission between the gears all rely on the "third party", and the control of all objects is handed over to the "third party" IOC. container.

Therefore, IOC realizes the decoupling between objects with dependencies by means of "third parties", making the program better.

 

The relationship between IOC and DI

In fact, IOC includes Dependency Lookup (DL) and Dependency Injection (DI), but DL has been abandoned because it is intrusive (it requires users to use the API to find resources and assemble objects).

So now when it comes to IOC, what comes to mind more is Dependency Injection (DI), as shown in the figure:

The full name of DI is Dependency Injection, which is called Dependency Injection in Chinese. It has the same meaning as Inversion of Control (IOC), but these two names describe the same concept from two perspectives.

When a Java object (the caller) needs to call another Java object (the callee, that is, the dependent object), in the traditional mode, the caller usually uses the "new callee" code method to create the object, As shown in the figure:

This method will increase the coupling between the caller and the callee, which is not conducive to the upgrade and maintenance of the later project.

After using the Spring framework, the instance of the object is no longer created by the caller, but by the Spring container. The Spring container is responsible for controlling the relationship between programs, rather than being directly controlled by the caller's program code.

In this way, the control is transferred from the application code to the Spring container, and the control is reversed, which is Spring's inversion of control IOC.

From the perspective of the Spring container, the Spring container is responsible for assigning the dependent object to the member variable of the caller, which is equivalent to injecting the instance it depends on for the caller. This is Spring's dependency injection, as shown in the figure:

 

Dependency injection method

Spring's dependency injection, we generally use the @Autowired annotation to complete, there are generally three ways for dependency injection:

Property injection, constructor injection, setter method injection:

1. Property injection

Property injection is the most common and most used injection method. The code is as follows:

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    //...
}

 

2.Set injection

The set method injection is too bloated and is rarely used in practice:

@Service
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;
    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
}

 

3. Constructor injection

Constructor injection is the officially recommended way, as follows:

@Service
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;
    
    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
}

 

Advantages and disadvantages of IOC

Advantages of IOCs

Implement decoupling between components to improve program flexibility and maintainability.

Disadvantages of IOCs

Using IOC framework products can bring great benefits to our development process, but we must also fully understand the shortcomings of introducing IOC frameworks and be aware of them.

  1. The steps to generate an object become more complicated (in fact, the operation is quite simple), and for those who are not used to this method, it will feel awkward and unintuitive.
  2. Object generation has some loss in efficiency because it uses reflective programming, but compared to the improved maintainability and flexibility of IoC, this loss is negligible, unless the generation of an object requires particularly high efficiency.

 

Implementation principle of IOC

The IOC container is actually a big factory that manages all our objects and dependencies.

  • The principle is realized through Java's reflection technology. Through reflection, we can obtain all the information of the class (member variables, class names, etc.);
  • Then describe the relationship between classes through configuration files (xml) or annotations.

In this way, we can construct corresponding objects and dependencies through these configuration information and reflection technology, as shown in the following figure:

The creation process of IOC containers and objects is as follows:

1. First create the BeanFactory container

2. Load the configuration file and encapsulate it as a BeanDefinition

3. Call and execute BeanFactoryPostprocessor

  • Preparation;
  • Prepare BeanPostProcessor;
  • Prepare listeners, events, broadcasters;

4. Instantiate

5. Initialization

6. Get the complete object.

above

About the Author

Chen Rui | mikechen , 10 years + experience in architecture of large factories, author of the series "BAT Architecture Technology 500", focusing on Internet architecture technology.

Read Mikechen's collection of more technical articles on Internet Architecture

Java Concurrency | JVM | MySQL | Spring | Redis | Distributed | High Concurrency

Follow the official account of "mikechen's Internet Architecture" and reply to [Architecture] to receive "Java Advanced Architecture Mind Map & Java Advanced Architecture Article Collection"

 

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/jiagouzhan/blog/5580458