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() , 每一次都会实例化一次 , 建立两个新的实例。