Spring 支持以下五个作用域,分别为 singleton、prototype、request、session 和 global session,5种作用域说明如下所示

    作用域 描述
    singleton 在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,默认值
    prototype 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()
    request 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
    session 同一个HTTP Session共享一个Bean,不同Session使用不同的Bean,仅适用于WebApplicationContext环境
    global-session 一般用于Portlet应用环境,该作用域仅适用于WebApplicationContext环境

    singleton 作用域
    当一个bean的作用域为 singleton,那么 Spring IoC 容器中只会存在一个共享的 bean 实例,并且所有对 bean 的请求,只要 id 与该 bean 定义相匹配,则只会返回 bean 的同一实例。
    当将一个 bean 定义设置为 singleton 作用域的时候,Spring IoC 容器只会创建该 bean 定义的唯一实例。
    singleton 是单例类型,就是在创建起容器时就同时自动创建了一个 bean 的对象,不管你是否使用,它都存在,每次获取到的对象都是同一个对象。
    注意:singleton 作用域是 Spring 中的缺省作用域
    案例代码:
    Person.java

    1. package com.baklib.custom;
    2. public class Person {
    3. private String name;
    4. private String carType;
    5. public String getName() {
    6. return name;
    7. }
    8. public void setName(String name) {
    9. this.name = name;
    10. }
    11. public String getCarType() {
    12. return carType;
    13. }
    14. public void setCarType(String carType) {
    15. this.carType = carType;
    16. }
    17. public void buyCar(){
    18. System.out.println(this.getName()+"购买了"+this.getCarType()+"!");
    19. }
    20. }

    MainApp.java

    1. package com.baklib.custom;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. public class MainApp {
    5. public static void main(String[] args) {
    6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    7. Person person1 = (Person) context.getBean("person");
    8. person1.setName("张三");
    9. person1.setCarType("小轿车");
    10. person1.buyCar();
    11. Person person2 = (Person) context.getBean("person");
    12. person2.buyCar();
    13. }
    14. }

    配置文件 Beans.xml

    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. <bean id="person" class="com.baklib.custom.Person" scope="singleton">
    6. </bean>
    7. </beans>

    编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息

    1. 张三购买了小轿车!
    2. 张三购买了小轿车!

    prototype 作用域
    当一个 bean 的作用域为 prototype,表示一个 bean 定义对应多个对象实例,在每次对该 bean 请求(将其注入到另一个 bean 中,或者以程序的方式调用容器的 getBean() 方法)时都会创建一个新的 bean 实例。
    prototype 是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。
    注意:对有状态的 bean 应该使用 prototype 作用域,而对无状态的bean则应该使用 singleton 作用域
    案例代码:
    Person.java

    1. package com.baklib.custom;
    2. public class Person {
    3. private String name;
    4. private String carType;
    5. public String getName() {
    6. return name;
    7. }
    8. public void setName(String name) {
    9. this.name = name;
    10. }
    11. public String getCarType() {
    12. return carType;
    13. }
    14. public void setCarType(String carType) {
    15. this.carType = carType;
    16. }
    17. public void buyCar(){
    18. System.out.println(this.getName()+"购买了"+this.getCarType()+"!");
    19. }
    20. }

    MainApp.java

    1. package com.baklib.custom;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. public class MainApp {
    5. public static void main(String[] args) {
    6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    7. Person person1 = (Person) context.getBean("person");
    8. person1.setName("张三");
    9. person1.setCarType("小轿车");
    10. person1.buyCar();
    11. Person person2 = (Person) context.getBean("person");
    12. person2.buyCar();
    13. }
    14. }

    配置文件 Beans.xml

    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. <bean id="person" class="com.baklib.custom.Person" scope="prototype">
    6. </bean>
    7. </beans>

    编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息

    1. 张三购买了小轿车!
    2. null购买了null!