Spring IOC and SpringIOC
I have met the interviewer for several interviews and asked, "my resume says you are familiar with the ssh framework. Then, let's talk about ioc and aop in spring ", I really regret this kind of problem. Why didn't I focus on basic concepts and practice it? (because I used to watch videos and practice it when I was learning this course, so after a while, conceptual things will soon be forgotten ). Let's summarize what IOC is.
Concept: IoC (Inverse of Control, Control inversion) refers to an object of Class B in Class A, which needs to be initialized by the developer in Class A and is now configured, spring can automatically initialize class B objects in Class. This process can also be called DI (Depend Injection, dependency Injection), because Class B objects Depend on Class A and are injected into Class A objects through spring Class B variables. The reason is described in detail below:
IOC is the core of spring. For the spring framework, spring is responsible for controlling the relationship between object lifecycles and objects. 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 try to understand them and wait for the process, we must design and face each link on our own. This is also true for traditional program development. In an object, if you want to use another object, you must get it (either new by yourself or query one from JNDI) after use, you also need to destroy the object and other processes. The object will always be coupled with other interfaces or classes. ,
However, how does IOC do it? It is a bit like you are looking for a girlfriend through a matchmaking agency, introducing it to you through a third party, and then doing what you want to do by yourself. However, this third party manages a lot of information about men and women. As long as you tell your needs, it will find the relevant mm for you through your needs; if not, it 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. I. The introduction of a "third party" in the middle location, that is, the IOC container
Figure 1
In addition to controlling the reversal of IOC, the Dependency Injection (Dependency Injection) is introduced later. For example, object A needs to operate on the database. In the past, we wrote code in object A to obtain A Connection object. In spring, we need to tell spring that A Connection object is required in object, object A does not need to know how to construct and when to construct the object. When the system is running, spring constructs A Connection at an appropriate time and injects it into A. This completes the control of the relationship between objects. The Connection must be dependent on the Connection to run normally, and the Connection is injected to A by spring. This is dependency injection.
Role (benefits): Flexible configuration. In IOC mode, IOC containers can be used to manage object lifecycles and Dependencies by introducing IOC containers, thus, application configuration and dependency specifications are separated from the actual application code. One feature is to configure the relationship between application components through the text accessory file, instead of modifying and compiling the specific code. Because the object generation is defined in XML, when we need to change an implementation subclass, it will become very simple (generally, such an object is actually an interface ), you only need to modify the XML. Although you do not need to re-compile java code after modifying XML, XML is often packaged with java source code. If you want to modify the XML file, you still need to repackage the file and release it again. It is not too troublesome to modify the java code and re-compile the modified Code. What are the essential benefits of using the IOC model? There is a saying that many java classes developed by others will be used when writing large programs. When programmer A uses java class A developed by programmer B, if IOC is used, after programmer B writes the XML configuration file, programmer A does not need to care about class A's initialization issues, use it directly. IOC is conducive to improving development efficiency in large programs developed by many people.
Implementation Principle: Java reflection mechanism.
Overall process of spring ioc, Xml configuration
Spring ioc initialization processStep No.Completed work1The spring container reads the configuration file and resolves it to the Registry.2Find the bean implementation class to instantiate the bean according to the Registry3Put the instantiated bean in the spring container4Bean ready for use by spring
References: 1. Example