Spring IOC
IOC refers to the inversion of control, the creation of objects, initialization, destruction and other work to the spring container. The spring container controls the life cycle of the object. To illustrate the way we traditionally create classes and the differences after using spring:
To create a Java class:
Package COM.YIHAI.SPRINGIOC;//IOC refers to control inversion. The creation, initialization, and destruction of objects are/are given to the spring container. The spring container controls the life cycle of the object. public class HelloWorld {public void Hello () {System.out.println ("Hello World");}}
Place the class in the Spring container:
<?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-2.5.xsd "> <!--indicates that a class ID is a unique identifier for a class, and class represents the full name of the classes, that is, the package name + class name -- <bean id= "HelloWorld" class= "Com.yihai.springioc.HelloWorld" > </bean></beans>
Remove objects from the spring container for classes
Package Com.yihai.springioc;import Org.springframework.context.applicationcontext;import ORG.SPRINGFRAMEWORK.CONTEXT.SUPPORT.CLASSPATHXMLAPPLICATIONCONTEXT;//IOC refers to control inversion. The creation, initialization, and destruction of objects are/are given to the spring container.The spring container controls the life cycle of the object. public class HelloWorld {public void Hello () {System.out.println ("Hello World");} public static void Main (string[] args) {/** * 1, starting spring container * 2, extracting the object from the Spring container * 3, Object invocation method */applicationcontext context = new Classpathxmlapplicationcontext ("Beanioc.xml"); HelloWorld HelloWorld = (HelloWorld) context.getbean ("HelloWorld"); Helloworld.hello ();}}
This type of create Bean object is the default constructor for the spring call class to create the object. The other way to create an object in 2 is to use the static factory method to create the bean and use the instance factory method to create the bean.
Create a spring HelloWorld program