Java --- spring6的Bean的作用域

目录

一、bean的作用域为单例

二、bean的作用域为多例

三、Bean作用域的Scope属性的其它值

四、Bean作用域的自定义Scope


一、bean的作用域为单例

public class SpringBean {
    public SpringBean() {
        System.out.println("构造方法被调用");
    }
}

spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sb" class="com.cjc.bean.SpringBean"></bean>
</beans>

测试代码:

public class BeanTest {
    @Test
    public void getBean(){

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
        SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
        System.out.println(sb);
        SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
        System.out.println(sb1);
        SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
        System.out.println(sb2);
    }

测试结果:

 通过测试发现:spring在默认情况下是单例模式,在上下文初始化的时候就实例化了,所以每次调用getBean这个对象时都返回是单例对象。

二、bean的作用域为多例

在spring核心配置文件中进行配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sb" class="com.cjc.bean.SpringBean" scope="prototype"></bean>
</beans>

测试结果:

 将bean的scope属性设置为prototype:bean就是多例,spring上下文初始化的时候,并不会初始化这些prototype的bean,当每次调用getBean()方法时,实例化bean对象。

三、Bean作用域的Scope属性的其它值

1、singleton:默认的,单例。

2、prototype:原型。每调用一次getBean()方法获取一个新的Bean对象,或者每次注入的时候都是新对象。

3、request:一个请求对应一个Bean,仅限于在WEB应用中使用。

4、session:一个会话对应一个Bean,仅限于在WEB应用中使用。

5、global session:portlet应用中专用的,如果在Servlet的WEB应用中使用global session的话,和session一个效果。

6、application:一个应用中对应一个Bean,仅限于在WEB应用中使用

7、websocket:一个websocket生命周期对应一个Bean,仅限于在WEB应用中使用。

8、自定义scope:很少使用。

四、Bean作用域的自定义Scope

自定义Scope ,线程级别的Scope,在同一个线程中,获取的Bean都是同一个,跨线程则是不同的对象:

在spring配置文件中配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sb" class="com.cjc.bean.SpringBean" scope="threadScope"></bean>
    <!--配置自定义作用域-->
    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="threadScope">
                    <!--这个Scope接口的实现类使用的是Spring框架内置的,也可以自定义-->
                    <bean class="org.springframework.context.support.SimpleThreadScope"/>
                </entry>
            </map>
        </property>
    </bean>
</beans>

测试代码:

    @Test
    public void getScope(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
        SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
        System.out.println(sb);
        SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
        System.out.println(sb1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
                System.out.println(sb2);
                SpringBean sb3 = applicationContext.getBean("sb", SpringBean.class);
                System.out.println(sb3);
            }
        }).start();
    }

测试结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鸭鸭老板

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

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

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

打赏作者

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

抵扣说明:

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

余额充值