Spring学习-- Bean 的作用域

Bean 的作用域:

  • 在 Spring 中 , 可以在 <bean> 元素的 scope 属性里设置 bean 的作用域。
  • 默认情况下 , Spring 只为每个在 IOC 容器里声明的 bean 创建唯一一个实例 , 整个 IOC 容器范围内都能共享该实例:所有后续的 getBean() 调用和 bean 引用都将返回这个唯一的 bean 实例 , 该作用域被称为 singleton , 他是所有 bean 的默认作用域 , 属于单例对象。

类别

说明

Singleton

在 Spring IOC 容器中仅存在一个 bean 实例 , bean 以单例的方式存在

Prototype

每调用一次 getBean() 都会生成一个新的实例

Request

每次 HTTP 请求都会创建一个新的 bean , 该作用域仅适用于 WebApplicationContext 环境

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...
person==person: true

在 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...
I am scope.Person Constructor...
person==person1: false

 调用两次 getBean() ,  每一次都会实例化一次 , 创建两个新的实例。 

原文地址:https://www.cnblogs.com/chinda/p/6488687.html