ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Spring】学习笔记06-Bean作用域

2022-06-11 00:32:58  阅读:192  来源: 互联网

标签:definition singleton 06 作用域 Spring single bean context


Spring官方,Beans作用域类型

ScopeDescription

singleton

(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.

prototype

Scopes a single bean definition to any number of object instances.

request

Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

application

Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.

websocket

Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

The Singleton Scope

这一点,Spring官网说明的很详细,

Only one shared instance of a singleton bean is managed, and all requests for beans with an ID or IDs that match that bean definition result in that one specific bean instance being returned by the Spring container.

To put it another way, when you define a bean definition and it is scoped as a singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition.
This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object. The following image shows how the singleton scope works:

他说我们对当前单例作用域的bean的所有请求,都是有Spring容器返回的特殊bean实例(同一个),而且这个单例实例是存储在这些单例bean的一个缓存中

 

 然后就是

<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

以上这两种都是单例作用域bean的注册,也就是说我们默认注册的bean的作用域就是“singleton”单例的

实例测试:

ApplicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="UserBean.xml"/>
    <import resource="UserBean_c_namespace.xml"/>
    <bean id="address_bean" class="com.wang.pojo.Address">
        <constructor-arg index="0" value="北京市海淀区"/>
    </bean>
    <bean id="student" class="com.wang.pojo.Student">
<!--        普通值注入,value-->
        <property name="name" value="王广元"/>
<!--        Bean注入,ref-->
        <property name="address" ref="address_bean"/>
<!--        数组注入-->
        <property name="books">
            <array>
                <value>你的名字</value>
                <value>我的天才女友</value>
                <value>万历十五年</value>
            </array>
        </property>
        <!--            list注入-->
        <property name="hobbys">
            <list>
                <value>jungle</value>
                <value>yoga</value>
                <value>music</value>
            </list>
        </property>
<!--        map注入-->
        <property name="card">
            <map>
                <entry key="学生卡" value="200"></entry>
                <entry key="洗澡卡" value="20"></entry>
                <entry key="剪发卡" value="200"></entry>
            </map>
        </property>
<!--        set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>Dota</value>
            </set>
        </property>
<!--        null值注入-->
        <property name="wife">
            <null></null>
        </property>
<!--       Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">11223344</prop>
                <prop key="大学">新加坡国立大学</prop>
                <prop key="专业">计算机科学与技术</prop>
            </props>
        </property>

     </bean>
</beans>

在上述bean的注册中,我们没有声明它的作用域,所以默认是单例的

单元测试

  @Test
    public void  test04(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Student student1= (Student) context.getBean("student");
        Student student2= (Student) context.getBean("student");
        System.out.println(student1 == student2);
    }
//结果 true

 

The Prototype Scope

The non-singleton prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made. 
That is, the bean is injected into another bean or you request it through a getBean() method call on the container.
As a rule, you should use the prototype scope for all stateful beans and the singleton scope for stateless beans.

Spring官方文档说,当对所有非单例模式的原型作用域的注册bean的的部署的请求,都会导致一个新的实例的创建,无论我们是将这个bean注入到其他bean当中,或者使用getBean()获取这个bean

 

 实例测试

bean.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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    </bean>
    <bean id="user_c1" class="com.wang.pojo.User" c:_0="user_c_0" c:_1="18" scope="prototype"/>
</beans>

单元测试

  @Test
    public void test05(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("UserBean_c_namespace.xml");
        User user1 = (User) context.getBean("user_c1");
        User user2 = (User) context.getBean("user_c1");
        System.out.println(user1 == user2);
    }
//false

 

标签:definition,singleton,06,作用域,Spring,single,bean,context
来源: https://www.cnblogs.com/WangGuangYuan/p/16365059.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有