Spring's core mechanism - Dependency Injection and Inversion of Control

The concept relies injection

Spring mechanism is the core of the IoC (inversion control) containers, IoC another is called dependency injection (DL). Both address is the same concept as described from two perspectives.

IoC is an important rule of object-oriented programming, computer programs for reduction coupling problems, but also the core of lightweight Sping framework.

, Application to the various components need not be coupled by the method of hardcoded dependency injection, when a Java Java instance requires additional examples, the system automatically provides needed example, without displaying the program acquired.

Thus, dependency injection to achieve decoupling between the components.

Injection control and dependency inversion same meaning when a Java object (caller) to another Java object need to call (the caller, i.e. dependent objects), the

The traditional method is to use the caller way "new callee" to create objects, this approach will lead to increased coupling between the caller and the callee, the latter part of the upgrade and maintenance projects adversely.

After using the Spring framework, no instance of an object created by the caller, but created by the Spring container, Spring containers will be responsible for relations between the process control, rather than code directly controlled by the caller program.

In this way, control is transferred from the application code into the Spring container, in control of the inversion, which is the inversion of control Spring.

From the perspective of Spring container point of view, Spring container will be responsible for dependent objects assigned to the member variables of the caller, which is equivalent to inject an instance of it depends on the caller, this is Spring's dependency injection.

Spring advocate Interface-oriented programming, dependency injection basic idea is: a well-defined component interface, independent development of various components, and then run according to the dependency component assembly.

Injection of dependency types

Dependency injection is the role objects are created using the Spring Framework, dynamically injected into the object on which it depends Bean component.

There are two main ways to achieve it, one is the method of injection configuration, the other is a property setter injection method.

Constructor injection. Spring means is configured to use a container instance is dependent injection method, there may be a constructor with no arguments or parameters.

In most cases, we are all created by the object constructor, Spring reflection mode may be employed, accomplished by injecting the constructor that takes parameters, each representing a dependency, which is injected into the constructor of principle.

When using setter injection method, Spring by no constructor parameter JavaBean to instantiate the object. When writing with arguments constructor, JAVA virtual machine will not provide a default constructor with no arguments.

In order to ensure flexibility of use, it is recommended to add their own a constructor with no arguments.

Property setter method injection. Refers to a property setter injection method Spring container using setter injection method is dependent on the value or the object, is a common dependency injection, this injection method has a high degree of flexibility.

The method of claim Bean property setter injection provides a default constructor, and provide the corresponding setter for properties to be injected.

Spring Bean default constructor is invoked to instantiate Bean, then injecting property value setter method is called by way of reflection.

Comparison of the two injection method

When using the setter methods, and more similar to traditional JavaBean written, programmers easier to understand and accept, set dependencies even more intuitive and natural through the setter method.

For complex dependencies, if the constructor injection will result in the constructor too bloated, difficult to read. Especially in the case of certain attributes Alternatively, multi-parameter constructor is more cumbersome.

Constructor injection injection sequence may be determined in dependence of the constructor, assignment when there are certain properties of the operation sequence, which is particularly important.

No need to change the dependence of Bean, construction method inject more useful. If no setter method, all dependencies are all within the configuration settings, code does not produce damage to subsequent dependency.

 

Guess you like

Origin www.cnblogs.com/kjitboy/p/12059215.html