It’s not an IoC — Inversion of Control. It’s a design idea. In Java development, Ioc means handing your designed objects over to the container for control, rather than the traditional direct control within your objects. How to understand Ioc? The key to a good understanding of Ioc is to be clear about “who controls whom, what controls, why there is a reversal (there should be a positive reversal), which aspects of the reversal”. Then let’s have an in-depth analysis:

Who controls whom and what:

In traditional Java SE programming, we create objects directly inside the object through new, and the program actively creates dependent objects. IoC has a special container to create these objects, that is, the IoC container controls the creation of objects; Who controls whom? The IoC container, of course, controls the objects; Control what? That is, it mainly controls the acquisition of external resources (not just objects including files, etc.).

Why and in what ways:

There is inversion and there is forward, the traditional application is by our own active control in the object to directly obtain the dependent object, that is, forward; In inversion, the container helps create and inject dependent objects. Why reverse? Because the container helps us find and inject the dependent object, the object only passively accepts the dependent object, so it is inversion; What has been reversed? The retrieval of dependent objects has been reversed

What does the IoC do

It’s not a technology, it’s an idea, an important object-oriented programming discipline that can guide us to design loose-coupled, better programs. In traditional applications, we actively create dependent objects within classes, which leads to high coupling between classes and is difficult to test. With the IoC container, the control of creating and finding dependent objects is given to the container, which injects composite objects. Therefore, objects are loosely coupled between objects, which facilitates testing, functional reuse, and more importantly, makes the overall architecture of the program very flexible.

IoC: I’d like to start with IoC.

This is at the heart of Spring, throughout. IoC, for the Spring framework, means that Spring is responsible for controlling the life cycle of objects and the relationships between objects. What does that mean, to take a simple example, how do we find a girlfriend? The common situation is that we go around to see where there are beautiful and nice MMS, and then ask about their interests, QQ number, telephone number, IP number, IQ number… Find a way to get to know them, give them what they want, and heh heh… The process is complex and profound, and we have to design and face each step ourselves. In traditional program development, if you want to use another object in an object, you have to get it (new one yourself, or look it up from JNDI) and then destroy the object (Connection, etc.). The object is always coupled with other interfaces or classes. So what does the IoC do? It was a bit like finding a girlfriend through a matchmaking agency, which introduced a third party between me and my girlfriend: the matchmaking agency. Matchmaking management a lot of data of men and women, I can put forward to dating a list, telling it what I want to find a girlfriend, look like michelle reis, for example, figure like Lin Xilei, singing like jay Chou, speed technology like zinedine zidane, like carlos, then dating would, according to the requirements of our provide a mm, We just need to fall in love with her and get married. Plain and simple, if the matchmaker gives us someone who doesn’t meet our criteria, we throw an exception. The whole process is no longer controlled by me, but controlled by the matchmaking agency, which is like a vessel. That’s what Spring is all about. All the classes register in the Spring container, tell Spring what you are and what you need, and Spring will give you what you want when the system is running properly, and hand you over to other things that need you. The creation and destruction of all classes is controlled by Spring, which means that it is not the reference object that controls the life cycle of an object, but Spring. For a specific object, it used to control other objects, but now all objects are controlled by Spring, so this is called inversion of control.

DI(Dependency Injection) An emphasis of IoC is to dynamically provide an object with other objects that it needs while the system is running.

This is achieved through DI (Dependency Injection). For example, object A needs to operate on the database. In the past, we always had to write code in A to get A Connection object. With Spring, we just need to tell Spring that A needs A Connection. When the system is running, Spring will make A Connection at the appropriate time and inject it into A like A needle, thus completing control over the relationships between objects. A relies on Connection to function properly, and this Connection is injected into A by Spring, hence the dependency injection name. So how is DI implemented? One of the most important features since Java 1.3 is Reflection, which allows programs to dynamically generate objects, execute object methods, and change object properties at runtime. Spring uses reflection to implement injection. Once you understand the concepts of IoC and DI, everything becomes straightforward, and all that is left is to stack the wood in the Spring framework.