Survival range of 1.bean:
(1) Singleton: Default, Single case
(2) Prototype: Prototypes, non-singleton
(3) Request: In an HTTP request, the container returns the same instance of the bean, and for different requests, a different instance is returned.
(4) Session: The scope of the request becomes session
(5) Gloabsession: Global Session
Request,session is primarily used in the Web
This time we are mainly exploring the 2 survival areas of Singleton and prototype.
<span style= "color: #FF0000;" > If scope is Singleton, the two are equal, and if scope is prototype, the two are not equal. </span>
Instance Code
public class User {}
Configuration file
<?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-3.0.xsd "><bean name=" user "class=" Com.qzp.model.User "scope=" prototype "></bean></ Beans>
Test code
public class Testscope {public static void main (string[] args) {ApplicationContext cxt=new Classpathxmlapplicationcontext ("Applicationcontext.xml"); User u1= (user) Cxt.getbean ("User"); User u2= (user) Cxt.getbean ("User"); System.out.println (U1.hashcode ()); System.out.println (U2.hashcode ()); <span style= "color: #FF0000;" >//If scope is singleton, the two are equal, and if scope is prototype, the two are not equal. </span>system.out.println (U1==U2);}}
Life cycle of 2.bean
(1) Lazy-init: Lazy load, if true, the bean will delay loading, if false, the bean will be loaded immediately, the default is True
(2) Init-method: Initialization method
<bean name= "user" class= "Com.qzp.model.User" lazy-init= "true" init-method= "Init" destroy-method= "destroy" depends-on= "Student" ></bean>
where init-method= "Init" Init is the Init method in the class user
(3) Destory-method: Method of Destruction
(4) Depends-on: Initialization of one bean depends on the initialization of another bean
Instance code:
public class User {private string Username;private string Password;public User () {super () ; System.out.println ("User's construction method ...."); public void init () { SYSTEM.OUT.PRINTLN ("User initialization method ..."); }&N bsp; public void Destroy () { System.out.println ("User's Destruction Method ..."); nbsp; }public String getusername () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}
public class Student {private String name;private int age;public Student () {super (); System.out.println ("Construction Method of student ..."); Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
Configuration file
<?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" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "> <bean name=" User "class=" Com.qzp.model.User "lazy-init=" true "init-method=" Init " Destroy-method= "Destroy" depends-on= "student" ></bean> <bean name= "student" class= " Com.qzp.model.Student "></bean></beans>
Test code:
public class Testlifecycle{public static void Main (string[] args) { Abstractapplicationcontext cxt=new Classpathxmlapplicationcontext ("Applicationcontext.xml"); <span style= "color: #FF0000;" >//If the following code is executed for lazy loading, then the constructor of the class is executed. </span> Cxt.getbean ("user"); Cxt.close ();}}
The test results are as follows:
The construction method of student ...
User's construction method ....
User Initialization Method ...
User's Destruction Method ...
The life span and lifecycle of spring learning beans