Bean 的做用域:spring
默認狀況下 , Spring 只爲每一個在 IOC 容器裏聲明的 bean 建立惟一一個實例 , 整個 IOC 容器範圍內都能共享該實例:全部後續的 getBean() 調用和 bean 引用都將返回這個惟一的 bean 實例 , 該做用域被稱爲 singleton , 他是全部 bean 的默認做用域 , 屬於單例對象。app
類別函數 |
說明this |
Singletonspa |
在 Spring IOC 容器中僅存在一個 bean 實例 , bean 以單例的方式存在prototype |
Prototypecode |
每調用一次 getBean() 都會生成一個新的實例xml |
Request對象 |
每次 HTTP 請求都會建立一個新的 bean , 該做用域僅適用於 WebApplicationContext 環境blog |
Session |
同一個 HTTP Session 共享一個 bean , 不一樣的 HTTP Session 使用不一樣的 bean , 該做用域僅適用於 WebApplicationContext 環境 |
Singleton:
1 package com.itdjx.spring.beans.scope; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /** 7 * @author Wáng Chéng Dá 8 * @create 2017-03-02 8:34 9 */ 10 public class Main { 11 12 public static void main(String[] args) { 13 14 ApplicationContext app = new ClassPathXmlApplicationContext("scope_beans.xml"); 15 16 17 } 18 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id="person" class="com.itdjx.spring.beans.scope.Person"> 7 <property name="name" value="華崽兒"/> 8 <property name="age" value="27"/> 9 </bean> 10 11 </beans>
1 package com.itdjx.spring.beans.scope; 2 3 /** 4 * @author Wáng Chéng Dá 5 * @create 2017-03-02 8:33 6 */ 7 public class Person { 8 9 private String name; 10 11 private int age; 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public int getAge() { 22 return age; 23 } 24 25 public void setAge(int age) { 26 this.age = age; 27 } 28 29 public Person() { 30 System.out.println("I am scope.Person Constructor..."); 31 } 32 }
控制檯輸出:
I am scope.Person Constructor... |
1 package com.itdjx.spring.beans.scope; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /** 7 * @author Wáng Chéng Dá 8 * @create 2017-03-02 8:34 9 */ 10 public class Main { 11 12 public static void main(String[] args) { 13 14 ApplicationContext app = new ClassPathXmlApplicationContext("scope_beans.xml"); 15 Person person = (Person) app.getBean("person"); 16 17 Person person1 = (Person) app.getBean("person"); 18 19 System.out.println("person==person1: " + (person == person1)); 20 21 } 22 }
控制檯輸出:
I am scope.Person Constructor... |
在 IOC容器初始化時就已經將 bean 實例化。當新建立對象時 , 沒有調用構造函數 , 兩個實例 == 比較時是 true , 說明這隻在 IOC 容器初始化的時候建立了一次 bean 的實例。
Prototype:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id="person" class="com.itdjx.spring.beans.scope.Person" scope="prototype"> 7 <property name="name" value="華崽兒"/> 8 <property name="age" value="27"/> 9 </bean> 10 11 </beans>
package com.itdjx.spring.beans.scope; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Wáng Chéng Dá * @create 2017-03-02 8:34 */ public class Main { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("scope_beans.xml"); } }
控制檯輸出:
控制檯沒有輸出 |
1 package com.itdjx.spring.beans.scope; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /** 7 * @author Wáng Chéng Dá 8 * @create 2017-03-02 8:34 9 */ 10 public class Main { 11 12 public static void main(String[] args) { 13 14 ApplicationContext app = new ClassPathXmlApplicationContext("scope_beans.xml"); 15 Person person = (Person) app.getBean("person"); 16 17 Person person1 = (Person) app.getBean("person"); 18 19 System.out.println("person==person1: " + (person == person1)); 20 21 } 22 }
控制檯輸出:
I am scope.Person Constructor... |
調用兩次 getBean() , 每一次都會實例化一次 , 建立兩個新的實例。