Spring's inversion of control (IOC) Idea, object instances are no longer created by the caller, but instead are created by the spring container. The spring container is responsible for the relationship between the control program, which is no longer directly controlled by the code, and the control is shifted from the application's code to the external container, the so-called control reversal. Spring has two IOC containers, and here I use the applicationcontext.
Take a class as an example:
public class UserService {public void AddUser () {System.out.println ("user Add");}}
register the class in XML, file name Beans.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "><!--production of any content--><bean Id= "Userserviceid" class= "Com.canyugan.hello.UserService" ></bean></beans>
Finally, let's take a little test:
<span style= "White-space:pre" ></span> @Testpublic void Demo1 () {//load config file ApplicationContext Applicationcontext=new classpathxmlapplicationcontext ("Com/canyugan/hello/beans.xml");// Get Object UserService userservice= (userservice) Applicationcontext.getbean ("Userserviceid") from spring's factory; Userservice.adduser ();}
Finally, enjoy the powerful features of spring's IOC.
Spring Control reversal IOC