Home > Java > javaTutorial > body text

Understanding Spring IOC and DI

PHP中文网
Release: 2017-06-22 14:55:21
Original
1344 people have browsed it

IOC is a design idea called "Inversion of Control".

1. Shallower level - analysis from the name
"Control" refers to the control of the life cycle of objects such as creation, maintenance, and destruction. This process is generally taken proactively by our program. Controlled, such as using the new keyword to create an object (creation), maintaining a reference during use (maintenance), and having the GC recycle the object (destroy) after all references are lost.
"Inversion" means that the control of the object's creation, maintenance, destruction and other life cycles is changed from program control to IOC container control. When an object is needed, it can be obtained directly from the IOC container by name.

2. A deeper level - when it comes to DI, dependency injection is an important implementation of IOC
The creation of an object often involves the creation of other objects, such as the member variables of an object A Holding a reference to another object B, this is dependency, A depends on B. Since the IOC mechanism is responsible for the creation of objects, this dependency must also be taken care of by the IOC container. The responsible way is DI - Dependency Injection, by writing dependencies into the configuration file, and then when creating objects with dependencies, the IOC container injects the dependent objects. For example, when creating A, it is detected that there are dependencies, IOC The container creates the object B that A depends on and injects it into A (assembly, implemented through the reflection mechanism), and then returns A to the object requester to complete the work.

3. What is the significance of IOC?
IOC does not implement more functions, but its existence allows us to obtain appropriate objects from the IOC container without a lot of code or complex coupling relationships between objects, and provides reliability for objects. management, greatly reducing the complexity of development.

The above is the detailed content of Understanding Spring IOC and DI. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template