Introduction:

This article means to use the vernacular to clarify what IOC is, only say the basic idea, do not involve the source code. No advanced technology or theory, just a joke. If there is a technical bull feel some place logic can be told, I will correct.

What is the IoC

IoC (IoC of Control) is an idea in object-oriented programming, often referred to as Inversion of Control, designed to reduce coupling between code.

So what exactly does the inversion of control reverse? How is it reversed? In contrast to traditional new objects, which actively create dependent objects, IoC asks you what you need and then gives it to you, hence its nickname of “Dependency Injection”.

This is like when you go to school to find a partner, you like the type, and then find the type of girl to fall in love with, this is called the positive turn, I rely on the object by their own control.

Results you do not cherish the school time, graduated to the age of marriage, the home often urge you to get married, now even an object did not how to get married. How to do? Find a matchmaker, and then you tell the matchmaker what kind of person you want to find, be pretty, have a good figure and so on.

This is the time when all the matchmakers that fit your expectations will call you and meet up. This process is actually called inversion.

So, what’s reversed? Reverses control over dependent objects from active to passive. How is it reversed? It gives the framework control over dependencies.

Basic realization of IoC

IoC is like a matchmaker, which may not be a good analogy.

Those of you who live in rural areas should have seen the power of matchmakers. You know all the villages around you, where there are women of marriageable age and boys of marriageable age.

Every Spring Festival, people would come to the door to ask if they have a partner, whether they are looking for a partner, what kind of person they are looking for and so on. What is he doing, please? This is gathering information. Collecting information is the first step as a media, and I need to master the basic information of my brother and sister to achieve accurate push.

First of all, ask you the basic information, where you live, how many acres of land, do you have a car and a house, this is the basic definition of a person.

Second, asking what type of person you are looking for is a way to master what you want.

Finally, find a suitable introduction to you before introducing you, this is introducing you to what you want.

Implementation of IoC in Spring

Initialize the IoC container. It’s like a matchmaker.

2. Load the Bean definition. The matchmaker came to the house to inquire about the situation. The baby is not bad, and the family situation is good.

3. Register the Bean’s BeanPostProcessor, which is used to intercept Bean initialization. It’s like a matchmaker who wants to introduce you to someone and see if you need it, what you need.

4. Instantiate beans (instantiate singleton beans). The matchmaker is going to introduce you, this time you need to understand your needs, start calling the post-processor, analyze what you want.

5, assembly Bean, find the appropriate, let you introduce. Then it’s all about you two, arranging a meeting or something.

conclusion

In Spring, the general IoC process is like the above, but there are more details. Use a metaphor to make it easier to understand logically, but it may not be as vivid, so don’t be stereotyped by it. Have time or hope you can follow the source code, and then think about it. I will write about the IoC process from the code side in the next part.

I am not just, but also ask you to give me everything you have

Wechat search “idle pick up wharf”, thank you for following the public number.

Rich text is really difficult. Next time, Markdown