The SPRINGIOC container is the core content of spring. Features: Creating objects, working with objects ' dependencies
IOC container Creation object:
There are several ways to create objects:
1) Call parameterless constructor
2) with parametric constructor
3) factory-created objects
Factory class, static method Create object
Factory class, non-static method to create an object
Configuration of the spring configuration file in the creation of the object
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd "><!--############## #对象创建 ############### --><!--1. The default parameterless constructor <bean id= "user1" class= "cn.itcast.b_create_obj. User "></bean>--><!--2. With parametric constructor--><bean id= "User2" class= "cn.itcast.b_create_obj. User "><constructor-arg index=" 0 "type=" int "value=" "></constructor-arg><constructor-arg Index = "1" type= "java.lang.String" value= "Jack" ></constructor-arg></bean><!--defines a string with the value "Jack"; StRing s = new String ("Jack")--><bean id= "str" class= "java.lang.String" ><constructor-arg value= "Jacks" > </constructor-arg></bean><bean id= "User3" class= "cn.itcast.b_create_obj. User "><constructor-arg index=" 0 "type=" int "value=" "></constructor-arg><constructor-arg Index = "1" type= "java.lang.String" ref= "str" ></constructor-arg></bean><!--factory Class Create object--><!--# Factory class, Instance method--><!--first create a factory--><bean id= "Factory" class= cn.itcast.b_create_obj. Objectfactory "></bean><!--Creating a User object,--><bean id=" User4 "factory-bean=" Factory "with factory-side instance method Factory-method= "getinstance" ></bean><!--# Factory class: Static method--><!--class specifies the factory type Factory-method must be inside the factory. "Static method"--><bean id= "user" class= "cn.itcast.b_create_obj. Objectfactory "factory-method=" Getstaticinstance "></bean></beans>
Entity class User.java
public class User {private int id;private String name;public User () {super (); SYSTEM.OUT.PRINTLN ("------The user object to create the parameterless constructor"------");} Public user (int ID, String name) {System.out.println ("-----User object created with parameter constructor"--------"); this.id = Id;this.name = name;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic String toString () {return "User [id=" + ID + ", name=" + name + "]";} public void Init_user () {System.out.println ("After object creation, initialization");} public void Destroy_user () {System.out.println ("IOC container destroyed, user object recycled!");}}
Test code
Create IOC container object ApplicationContext AC = new Classpathxmlapplicationcontext ("Cn/itcast/b_create_obj/bean.xml");// Gets the object in the container user user = (user) Ac.getbean ("User"); SYSTEM.OUT.PRINTLN (user);
Class to demonstrate factory-created objects
Factory, create object public class Objectfactory {///instance method create object public user getinstance () {return new User (100, "Factory: Invoke instance Method");} static method to create object public static User Getstaticinstance () {return new User (101, "Factory: Call static Method");}}
SPRINGIOC Container-Create object