In this article, services and components can be understood as the same meaning. Component's definition: Component is "a Glob of Software That INTENDED TO BE Used, without change, by Application That is out of the control of the write of the component" (component is defined as MartInfowler). With the emergence of lightweight containers (Spring and PicoContainer et al., Inversion of Control and Dependency Injection are accommodated. In fact, Inversion of Control is one of the most basic features of all Frameworks frameworks, "reverse control" is not new. For example, the reversal of the program process control, the control is transferred from the program itself to the Framework hand. Template Method mode is generally used in the Framework framework to implement the reversal of control. And the new lightweight IOC container proposed in Inversion of Control actually "how to locate the component", we can understand the components here as some classes, that is, how to get the classes I need. Before the IOC container, this work is implemented by our own in the program (such as through new, factory class, or service local mode). Now IOC containers can achieve this for us. The IOC container is solved how the instances we need to inject into our app. Martin Fowler believes that Inversion of Control is more popular and does not effectively describe the characteristics of new IOC Container. So he proposed a new term: Dependency Injection, injecting the components dependent on a class in this class when the program is running. There are three main forms in the form of injecting, referred to as constructor inject, setting value method injection, and interface injection (Interface Injection). To understand the following relationship, we will help us understand "dependent injection". (1) The relationship between IOC container (dependent injection) and OO design. The OO design emphasizes the interface to interface, but we must involve instance to interface. The IOC container is to resolve how to inject the dependencies that rely on instance into our application.
(2) Depending on the relationship between injection and service Locator eliminates the application between applications and components: one is dependent on injection, and the other is to use Service Locator mode. The main difference between the two is to use the Service Locator mode, you need to call Service Locator to request the service you need to request the service. Using the Dependency Injection mode, the application does not have to send an explicit request, and the implementation of the service will naturally appear in the application code. When using Service Locator mode, the user must rely on Service Locator. Service Locator can hide the user's dependence on the specific implementation of the service, but you must first see the service locator itself. So, choose Service Locator or dependency inJection, depending on whether "dependency on the service locator" will bring you trouble. Reference: http://martinfowler.com/articles/injection.html