Basic concepts and understanding of spring ioc

IOC, inversion of control , but from my point of view and understanding, it is from active to passive. To give a simple example: I was originally the boss, all the initiative is in my hands, I control everything, Later, a leader who was higher than me came over and was transferred to him to control it. I think it would be easier to understand this concept, which belongs to role exchange and inversion of control.

Inversion of control: It is a way to generate or obtain specific objects through description (in java, through xml and annotations) and through a third party.

The ioc container that implements the inversion of control in spring is implemented by dependency injection DI

for example:

In real system development, the developer of the system is a team, and the team is composed of many developers. If you are responsible for the development of an e-commerce website, you are familiar with the commodity transaction process, but are not familiar with finance, and some members of the team are very familiar with financial processing. During the transaction process, the commodity transaction process needs to schedule financial related interfaces , Can be realized, then our expectations should be:

1. Members who are familiar with financial processes develop corresponding interfaces.

2. The interface logic is as simple as possible, the internal complex business does not need to be understood by yourself, you can use it only through simple calls.

3. The interface instance can be obtained through a simple description, and the description should be as simple as possible.

When colleagues who are familiar with finance have completed the development of the financial interface module, they can publish their services to the spring ioc container,

At this time, you only need the process description to get the corresponding financial interface, then you can complete the corresponding financial operation, and these

You don’t need to understand, you just need to know that it can complete the corresponding financial operations. Similarly, for you who are familiar with trading,

The transaction interface module is also released in the Spring ioc container, so that financial developers can obtain the transaction interface through the container to obtain transaction details, how the transaction module works, and which objects it depends on. He does not need to know, the courseware spring ioc container brings It has a lot of convenience to use.

 

This is a concept of inversion of control. Although one of the disadvantages of this concept is the difficulty of understanding, its biggest advantage is to reduce the coupling between objects. There are some classes in a system, and it is not necessary to understand how to implement them. , Just need to know what it has

Just use it, but the generation of objects here depends on the ioc container, not the developer's active behavior. Actively created patterns,

The responsibility belongs to the developer, and in the passive mode, the responsibility belongs to the ioc container. Based on this passive form, we say that the object

The control was reversed. Based on reducing the difficulty of development and decoupling the modules, the Spring Ioc container can accommodate various Beans that we develop, and we can obtain various Beans published in the Spring Ioc container, and get it through the description.

 

Guess you like

Origin blog.csdn.net/crossroads10/article/details/89242205