Working principle of spring, spring Workflow
[Reprinted]
This article describes the Special image of spring IOC and DI.
IoC and DI
First, I want to talk about IoC (Inversion of Control, Control reversal ). This is the core of spring. For the spring framework, the so-called IoC is that spring is responsible for controlling the relationship between object lifecycles and objects. What does this mean? For example, how do we find a girlfriend? The common situation is that we go around to see where there is a beautiful and good mm, and then inquire about their interests, QQ number, phone number, ip number, iq number ........., Find a way to get to know them and give them what they want ...... This process is complex and profound, and we must design and face each link on our own. This is also true for traditional program development. To use another object in an object, you must obtain it (new by yourself or query one from JNDI ), after use, the object will be destroyed (such as Connection), and the object will always be combined with other interfaces or classes.
How does IoC work? It's a bit like finding a girlfriend through a matchmaking agency. I introduced a third party between my girlfriend and me: the marriage agency. The matchmaking agency manages a lot of information about men and women. I can submit a list to the matchmaking agency to tell it what kind of girlfriend I want to find, such as Li jiaxin, Lin xilei, and Jay Chou, the speed is like Carlos, the technology is like Zidane, and then the matchmaking agency will provide a mm according to our requirements. We just need to get in love with her and get married. It is simple and clear. If the person selected by the matchmaking agency does not meet the requirements, we will throw an exception. The entire process is no longer controlled by myself, but controlled by a mechanism similar to a container such as matchmaking agency. This is the development method advocated by Spring. All classes will be registered in the spring container to tell spring what you are and what you need, then spring will take the initiative to give you what you want when the system runs properly, and also give you something else you need. The creation and destruction of all classes are controlled by spring. That is to say, the object lifecycle is not referenced but spring. For a specific object, it used to control other objects. Now all objects are controlled by spring, so this is called control inversion. If you still don't understand, I decided to give up.
IoC dynamically provides other objects required by an object during system running. This is achieved through DI (Dependency Injection, Dependency Injection. For example, object A needs to operate the database. In the past, we always had to write code in A to obtain A Connection object. With spring, we only need to tell spring that A Connection is required in, as for how to construct the Connection and when to construct it, A does not need to know. When the system is running, spring will create A Connection at an appropriate time and inject it into A like an injection. This completes the control of the relationship between objects. A needs to rely on the Connection to run normally, and the Connection is injected into A by spring, so the dependency injection name is like this. How is DI implemented? Reflection allows programs to dynamically generate objects, execute object methods, and change object attributes during runtime. spring injects objects through reflection.