Spring中bean的作用域(易懂版)


作用域:用于确定Spring创建Bean的实例个数。

作用域类别描述
singleton单例的(默认的),使用singleton定义的Bean是单例的,每次调用getBean都是调用的同一个对象。只要IOC容器一创建就会创建Bean的实例。
prototype多例的,每次通过Spring IOC容器获取prototype定义的Bean时,容器都将创建一个新的Bean实例。创建时不会实例该Bean,只有调用getBean方法时,才会实例化。
request作用于web的请求范围,在每一次HTTP请求时,容器会返回Bean的同一个实例,对不同的HTTP请求则会产生一个新的Bean,而且该Bean仅在当前HTTP Request内有效。
session作用于web的会话范围,在一次HTTP Session中,容器会返回该Bean的同一个实例,对不同的HTTP请求则会产生一个新的Bean,而且该Bean仅在当前HTTP Session内有效。
global-session作用于集群环境的会话范围(全局会话范围),在一个全局的HTTP Session中,容器返回Bean的同一个实例。当不是集群环境时,它就是session。

单实例(singleton)的bean

这种单实例的是默认的类型
使用singleton定义的Bean是单例的,每次调用getBean都是调用的同一个对象。只要IOC容器一创建就会创建Bean的实例。

随便拿来一个Book类演示:

package com.Keafmd.spring5.collectiontype;

import java.util.List;

/**
 * Keafmd
 *
 * @ClassName: Book
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:56
 */
public class Book {
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
    public void test(){
        System.out.println(list);
    }
}

bean2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


    <!--设置为单实例,不写默认的就是这种类型:scope="singleton"-->
    <!--设置为多实例:scope="prototype"-->
    <bean id="book" class="com.Keafmd.spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>

</beans>

测试代码:

package com.Keafmd.spring5.testdemo;

import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 {

    @Test
    public void test(){

        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book",Book.class);
        Book book2 = context.getBean("book",Book.class);
        System.out.println(book);
        System.out.println(book2);
    }
 
}

测试结果:

com.Keafmd.spring5.collectiontype.Book@376b4233
com.Keafmd.spring5.collectiontype.Book@376b4233

Process finished with exit code 0

输出结果相同证明是同一个对象,证明是单实例的。

多实例(prototype)的bean

设置为多实例需要用到scope属性,并且属性值设为"prototype"。这样就是多例的,每次通过Spring IOC容器获取prototype定义的Bean时,容器都将创建一个新的Bean实例。创建时不会实例该Bean,只有调用getBean方法时,才会实例化。

Book类:

package com.Keafmd.spring5.collectiontype;

import java.util.List;

/**
 * Keafmd
 *
 * @ClassName: Book
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:56
 */
public class Book {
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
    public void test(){
        System.out.println(list);
    }
}

修改bean2.xml的bean的scope的值为prototype。
bean2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


    <!--设置为单实例,不写默认的就是这种类型:scope="singleton"-->
    <!--设置为多实例:scope="prototype"-->
    <bean id="book" class="com.Keafmd.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>

</beans>

测试代码:

package com.Keafmd.spring5.testdemo;

import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 {

    @Test
    public void test(){

        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book",Book.class);
        Book book2 = context.getBean("book",Book.class);
        System.out.println(book);
        System.out.println(book2);
    }
 
}

测试结果:

com.Keafmd.spring5.collectiontype.Book@376b4233
com.Keafmd.spring5.collectiontype.Book@2fd66ad3

Process finished with exit code 0

输出结果不同的,证明不是同一个对象,证明是多实例的。

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

在这里插入图片描述
加油!

共同努力!

Keafmd

评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛哄哄的柯南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值