IoC (inversion of control, inversion of controls)
Spring's IOC application is the biggest feature of its framework, and dependency injection can greatly reduce the coupling between the code, thus enabling separation between code and functionality. You can not directly connect to objects and servers in your code, but it is the container's responsibility to describe which component requires which task in the configuration. With reverse control, the object passively receives the dependent class instead of creating or locating the object it depends on.
Before introducing spring's dependency injection, let's start by looking at a simple Java application:
The change example contains four files: one is the person interface class, one is the student class, the class implements the person interface, and the Go () method in the person interface is implemented, as well as a teacher class. The last one is the test main class that contains the main method. Create the student class and the teacher class in the program, and invoke the instance's Go method.
Public Interface Person { publicvoid Go ();}
Public class Implements person{ @Override publicvoid Go () { System.out.println ("Go to Class" ); }}
Public class Implements person{ @Override publicvoid Go () { System.out.println ( "To lecture in the classroom");} }
Public class Test { publicstaticvoid main (string[] args) { new Teacher (); Teacher.go (); New Student (); Student.go (); }}
Create their instance objects directly from the teacher class and the student class, with a high degree of code coupling.
Here's how to build a Java instance directly from the spring container
Applicationcontext.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <BeanID= "Student"class= "Com.jie.demo.Student"></Bean> <BeanID= "Teacher"class= "Com.jie.demo.Teacher"></Bean></Beans>
Importorg.springframework.beans.factory.xml.XmlBeanFactory;ImportOrg.springframework.core.io.ClassPathResource; Public classTest { Public Static voidMain (string[] args) {Classpathresource ISR=NewClasspathresource ("Applicationcontext.xml"); Xmlbeanfactory Factory=NewXmlbeanfactory (ISR); Person Teacher= (person) factory.getbean ("Teacher"); Teacher.go (); Person Student= (person) factory.getbean ("Student"); Student.go (); }}
The IOC container of spring is a large factory, which is taken directly from it when it is needed. The biggest benefit of spring is that it can automatically inject the required instances.
Here's an easy-to-understand answer from a blog called Bromon:
IOC and DI
The first thing to say about IOC (inversion of control, inversion of controls). This is the core of spring, throughout. The so-called IOC, for the spring framework, is the responsibility of spring to control the object's life cycle and the relationship between objects. What does that mean, for example, how do we find a girlfriend? The common situation is that we go everywhere to see where there is a beautiful body and good mm, and then inquire about their interests, QQ number, telephone number, IP number, IQ number ..., find ways to know them, give them what they want, then hey ... The process is complex and profound, and we have to design and face each link ourselves. The same is true of traditional program development, in an object, if you want to use another object, you have to get it (your own new one, or a query from Jndi), after the use of the object will be destroyed (such as connection, etc.), the object will always and other interfaces or classes together.
So how does the IOC do it? It's kind of like finding a girlfriend through a dating agency, introducing a third party between me and my girlfriend: the Marriage Institute. Matchmaking management of a lot of men and women's information, I can give a list of matchmaking, tell it I want to find a girlfriend, such as like Michelle Reis, figure like Lin Xire, singing like Jay Chou, speed like Carlos, technology like Zidane, and then the matchmaking will be according to our requirements, provide a mm, We just have to go to love her and get married. As simple as it is, if a matchmaking person doesn't meet our requirements, we'll throw an exception. The whole process is no longer controlled by myself, but by a similar container-like institution that has a matchmaking system. This is how spring advocates for development, and all classes are registered in the spring container, telling spring what you are, what you need, and then spring will give you what you want when the system runs to the right time, as well as handing you over to other things that need you. All classes are created and destroyed by spring, which means that the object that controls the lifetime of the object is no longer a reference to it, but spring. For a specific object, it was previously controlled by other objects, and now all objects are controlled by spring, so this is called control inversion. If you do not understand, I decided to give up.
A key point of the IOC is to dynamically provide the other objects it needs to an object during the system's operation. This is achieved through DI (Dependency injection, Dependency injection). For example, object A needs to manipulate the database, before we always have to write code in a to get a connection object, with spring we just need to tell spring,a need a connection, as for this connection how to construct, when to construct , a does not need to know. When the system is running, Spring creates a connection at the right time, and then, like an injection, it injects into a, which completes the control of the relationship between the various objects. A relies on connection to function properly, and this connection is injected into a by spring, and the name of the dependency injection comes in. So how is di implemented? An important feature after Java 1.3 is reflection (reflection), which allows the program to dynamically generate objects, execute methods of objects, and change the properties of objects when it is run, and spring is injected through reflection. Refer to Java Doc for information on reflection.
Spring's IOC application