02_Spring Bean Scope

Overview:  There are four kinds of bean scope in Spring, the default is singleton.

Types of Scope
singleton The default type singleton, the object obtained through the Spring container is unique
prototype When you get an object through the Spring container, a new object will come out
request Means valid within a request
session Means valid in a session

 

 

 

 

 

 

Sample:

Entity class:

 1 package com.test.entity;
 2 
 3 public class Students {
 4     private int id;
 5 
 6     private String name;
 7 
 8     private int age;
 9 
10     private Classes classes;
11 
12     public Classes getClasses() {
13         return classes;
14     }
15 
16     public void setClasses(Classes classes) {
17         this.classes = classes;
18     }
19 
20     public Students(int id, String name, int age) {
21         this.id = id;
22         this.name = name;
23         this.age = age;
24     }
25 
26     public Students(int id, String name, int age, Classes classes) {
27         this.id = id;
28         this.name = name;
29         this.age = age;
30         this.classes = classes;
31     }
32 
33     public Students(){
34 
35     }
36 
37     public int getId() {
38         return id;
39     }
40 
41     public void setId(int id) {
42         this.id = id;
43     }
44 
45     public String getName() {
46         return name;
47     }
48 
49     public void setName(String name) {
50         this.name = name;
51     }
52 
53     public int getAge() {
54         return age;
55     }
56 
57     public void setAge(int age) {
58         this.age = age;
59     }
60 
61     @Override
62     public String toString() {
63         return "Students{" +
64                 "id=" + id +
65                 ", name='" + name + '\'' +
66                 ", age=" + age +
67                 ", classes=" + classes +
68                 '}';
69     }
70 }
View Code

Spring configuration:

1 <bean id="stu1" class="com.test.entity.Students"></bean>
2 
3 <bean id="stu2" class="com.test.entity.Students" scope="prototype"></bean>
View Code

Test class:

 1 public static void main(String[] args) {
 2         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
 3 
 4         //bean scope test --- default
 5         Students stuSingleton1 = (Students) applicationContext.getBean("stu1");
 6 
 7         Students stuSingleton2 = (Students) applicationContext.getBean("stu1");
 8 
 9         System.out.println(stuSingleton1 == stuSingleton2);
10 
11         //bean scope test --- prototype
12         Students stuPrototype1 = (Students) applicationContext.getBean("stu2");
13 
14         Students stuPrototype2 = (Students) applicationContext.getBean("stu2");
15 
16         System.out.println(stuPrototype1 == stuPrototype2);
17 }
View Code

Test Results:

1 true
2 false
View Code

 

depends-on:

 1 <bean id="car" class="com.test.entity.Car">
 2         <property name="id" value="1"></property>
 3         <property name="name" value="car"></property>
 4     </bean>
 5 
 6     <bean id="dependsStu" class="com.test.entity.Students" depends-on="car">
 7         <property name="id" value="55"></property>
 8         <property name="name" value="testDepends"></property>
 9         <property name="age" value="25"></property>
10     </bean>
View Code

 

Guess you like

Origin www.cnblogs.com/jiyanping/p/12677649.html