SPRINGIOC container-Create object _j2ee-spring

Source: Internet
Author: User
Tags instance method

SPRINGIOC container, is the spring core content. Functions: Creating objects to handle dependencies of objects

IOC container Creation object:
There are several ways to create an object:
1) calling the parameterless constructor
2) with Parameter Builder
3) Factory Create object
Factory class, static method Creation object
Factory class, non-static method creation Object


The spring configuration file is configured to create the object

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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/bea NS Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/cont Ext 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 parameter builder--> <bean id= "User2" class=. User "> <constructor-arg index=" 0 "type=" int "value=" M "></constructor-arg> <constructor-arg Index = "1" type= "java.lang.String" value= "Jack" ></constructor-arg> </beaN> <!--defines a string, the value is "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=" M "></constructor-arg> <constructor-arg Index = "1" type= "java.lang.String" ref= "str" ></constructor-arg> </bean> <!--factory Class Create Object--> <!--# work Factory class, instance method--> <!--first create factory--> <bean id= "Factory class=" cn.itcast.b_create_obj. Objectfactory "></bean> <!--creating the user object, factory instance method--> <bean id=" User4 "factory-bean=" Factory "   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 ("------User object creation" without parameter Builder "------");
	
	
	public User (int ID, String name) {
		System.out.println ("-----User object creation" with parameter Builder "--------");
		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;
	}
	
	
	@Override public
	String toString () {return
		"User [id=" + ID + ", name=" + name + "]";
	}


	public void Init_user () {
		System.out.println ("Initialize after object creation");
	}
	public void Destroy_user () {
		System.out.println ("IOC container destroy, User Object Recycle!");
	}
	


Test code

Create IOC container object
		applicationcontext ac = new Classpathxmlapplicationcontext ("Cn/itcast/b_create_obj/bean.xml");
		Gets the object
		user user = (user) Ac.getbean ("user") in the container;
		
		SYSTEM.OUT.PRINTLN (user);

Demo factory Create classes for objects

Factory, create object public
class Objectfactory {

	//instance method creation object public
	User getinstance () {return
		new User (100, " Factory: Invoke instance method ");
	}
	
	static method Creation Object public
	static User getstaticinstance () {return
		new User (101, Factory: Calling static method);
	}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.