What is the 2.1.1 IOC?
Ioc-inversion of control, or "inversion of controls", is not a technique, but a design idea. In Java development, IOC means handing over your designed objects to container control rather than the traditional direct control within your object. How to understand the good IOC? The key to understanding the IOC is to make it clear "who controls who, what controls, why it is reversed (there should be a reversal in reverse), and which aspects are reversed", so let's look at it in depth:
Who controls who, what controls: Traditional Java SE Programming, we create objects directly inside objects through new, is the program to create dependent objects, and the IOC has a special container to create these objects, that is, the IOC container to control the creation of objects; who controls who? The IOC container, of course, controls the object; That is the primary control of external resource acquisition (not just objects such as files, etc.).
Why reverse, what is reversed: there is a reversal of the forward, the traditional application is our own active control in the object to directly obtain the dependent object, that is, the reverse is the container to help create and inject dependent objects; Because the container helps us to find and inject dependent objects, the object is only passively accepting dependent objects, so it is reversed; The acquisition of the dependent object is reversed.
Illustrated by the legend, the traditional programming 2-1, are actively to create related objects and then combined together:
Figure 2-1 Legacy applications
When you have a Ioc/di container, you are no longer actively creating these objects in the client class, as shown in 2-2:
Figure 2-2 Ioc/di container Post-Program structure
What 1.1.2 IOC can do
IOC is not a technology, but an idea, an important object-oriented programming law that can guide us in designing loosely coupled, better programs. Traditional applications are created by us to create dependent objects within the class, which causes the class to be highly coupled to the class and difficult to test; with the IOC container, the control of creating and finding dependent objects is given to the container, which is injected by the container, so that the object is loosely coupled with the object, so it is also easy to test. Facilitates functional reuse and, more importantly, makes the entire architecture of the program very flexible.
In fact, the biggest change of the IOC to programming is not from the code, but from the thought, the "master-slave Transposition" change. The application was originally the boss, to get what resources are active attack, but in Ioc/di thought, the application becomes passive, passively wait for the IOC container to create and inject the resources it needs.
IOC is a good embodiment of the object-oriented design of one of the rules of Hollywood: "Do not call us, we find you", that is, the IOC container for the object to find the corresponding dependent objects and injected, rather than the object to be actively looking for.
2.1.3 IOC and DI
Di-dependency injection, or "dependency injection": is the dependency between components is determined by the container at run time, in the image, that is, the container dynamically injects a dependency into the component. The purpose of dependency injection is not to bring more functionality to a software system, but to increase the frequency of component reuse and to build a flexible and extensible platform for the system. Through the dependency injection mechanism, we only need to use simple configuration, without any code to specify the resources required by the target, to complete its own business logic, without the need to care about where the specific resources come from and by whom.
The key to Understanding di is: "Who depends on who, why they need it, who injects it, and what is injected into it," Let's look at it in depth:
Who depends on who: Of course the application relies on the IOC container;
Why you need to rely on: applications need an IOC container to provide the external resources required by the object;
Who injects who: it is obvious that an IOC container injects an object into an application, an application-dependent object;
What is injected: the external resources (including objects, resources, constant data) that are required to inject an object.
What is the relationship between the IOC and di? In fact, they are different angles of the same concept, because the concept of inversion of control is ambiguous (may be just understood as a container control object this level, it is difficult to think of who to maintain the object relationship), so the 2004 master figure Martin Fowler again gave a new name: "Dependency Injection", In contrast to the IOC, "dependency Injection" clearly describes the "injected object relies on the IOC container configuration dependent object".
spring--learning of the IOC DI