Getting Started 03 - Dependent Injecting DI IOC Mode is basically a high-level concept, talking in the Martin Fowler Invrsion of Control Containers and The Dependency Injection Pattern, there are two ways to implement IOC: Dependency Injection and Service Locator. You can find this article in the following URL: http://www.martinfowler.com/articles/injection.html
Spring uses Dependency Injection to implement IOC, meaning in Chinese translation as dependent injection, dependency injection is: "Retain the abstract interface, let components depend on abstract interface, when the component is dependent on other actual objects, borrowed Abstract interfaces to inject relying on the actual object. "Take a look at this program:
Public class businessObject {
Private floppywriter Writer = new floppywriter ();
....
Public void save () {
...
Writer.savetofloppy ();
}
}
BusinessObject relies on the actual FLOPPYWRITER, in order to get businessObject to reuse, we don't let BusinessObject rely on actual FLOPPYWRITER, but dependent on abstract interfaces:
Public interface idevicewriter {
Public void savetodevice ();
}
Public class businessObject {
Private ideviceWriter Writer;
Public void setDeviceWriter (IDEVICEWRITER WRITER) {
This.writer = Writer;
}
Public void save () {
....
Writer.savetodevice ();
}
}
Public Class Floppywriter IMPLEMENT IdeviceWriter {
Public void savetodevice () {
....
/ / Actually store to FLOPPY program code
}
}
Public class usbdiskwriter type iDeviceWriter {
Public void savetodevice () {
....
/ / Actually store the program code to USBDisk
}
}
If today, BusinessObject wants to rely on the use of UseDiskWriter objects, you can build:
BusinessObject.SetDeviceWriter (New USBDiskWriter ());
Since BusinessObject relies on abstract interfaces, we can inject relying on dependence on an abstract interface when needed. Three implementations are discussed in the Martin Fowler article: Interface Injection, Setter Injection and Constructor Injection. And respectively, it is called Type 1 IoC, Type 2 IoC and Type 3 IOC. The above BusinessObject is implemented in Type 2 IoC, which is injecting dependencies through the setter, and Type 3 IOC is injecting dependencies on the construction of a function, such as:
Public class businessObject {
Private ideviceWriter Writer;
Public BusinessObject (IdeviceWriter Writer) {this.writer = Writer;
}
Public void save () {
....
Writer.savetodevice ();
}
}
Spring encourages SETTER INJECTION, but also allows you to use Constructor Injection, use setter or constructor to inject dependencies depending on your needs, one of the benefits of using the constructor is that you can complete the dependencies while constructing objects. Establish, however, if there is a lot of objects to be established, a long string of parameters will be left on the constructive function. At this time, it will be a good choice. On the other hand, setter can have a clear name to understand the injection. What is the object it will, like setXXX (), the name of SetXXX () is better than a certain parameter location on the memory constructor. TYPE 1 IOC is Interface Injection, which requires the actual interface when using Type 1 IOC. This interface is used by the container. The container knows the method specified on the interface. It can call the object of the real interface to complete the injecting of the dependency. E.g:
Public interface idependencyinjection {
Public Void CreateDependence (Map DependObjects);
}
Public Class BusinessObject Implement IdependencyInJection {
PRIVATE MAP DependObjects;
Public Void CreateDependence (Map DependObjects) {
THIS.DEPENDOBJECT = dependObjects;
/ / Realize the dependencies of BusinessObject here
......
}
Public void save () {
....
Writer.savetodevice ();
}
}
If you want to complete dependency injection, you must implement the IDEPENDENCYINJECTION interface, and will be submitted to the container management, the container calls the CreateDependency () method of the managed object to complete the establishment of dependencies. In the above example, Type 1 IoC requires BusinessObject to achieve a specific interface, which makes BusinessObject depend on the container, if the businessObject is detached from the current container in the future, you must modify the program, think about producing more in more complex dependencies Complex interfaces, components and containers (frames) are more complicated, and finally make components unable to detach from the container. Therefore, Type 1 IOC has strong invasive, using it to achieve dependency injection, so that the component is dependent on the container (framework), reducing the reuse of the component. The core of Spring is an IOC container, you can implement your business object with the setter or constructor, as for the relationship between objects and objects, then make Spring in the execution period according to the configuration Set to create dependencies between objects, you don't have to pay special written relations between Helper's dependencies between rows, which not only reduces a large number of procedures, but also reduces the degree of coupling between objects.