Spring5入门-05-Bean的作用域(单例、原型)

一、前言

目的:了解Bean的作用域



二、准备工作

2.1 依赖

注意:这里用到的应该是spring-context,但是spring-webmvc由于继承的关系会有一张依赖网:

image-20200922215433508

算是省心省力吧。

<!--Spring-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

<!--JUnit-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

2.2 创建实体类

路径

image-20200924191940271

代码

package com.duzhuan.pojo;

/**
 * @Autord: HuangDekai
 * @Date: 2020/9/24 19:18
 * @Version: 1.0
 * @since: jdk11
 */
public class User {
    int id;
    String name;

    public User() {
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

2.3 配置文件

路径

image-20200924193339420

代码

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="user" class="com.duzhuan.pojo.User" c:id="1" c:name="duzhuan"/>
    
</beans>


三、作用域(scope)

下面是官方文档给出的表格:

Scope Description
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.

3.1.单例模式(singleton)

默认的模式是单例模式(singleton),不用写出来。

image-20200924192706855

测试样例:

import com.duzhuan.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Autord: HuangDekai
 * @Date: 2020/9/24 19:45
 * @Version: 1.0
 * @since: jdk11
 */
public class UserTest {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        User user1 = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);

        System.out.println(user1==user2);
        System.out.println("user1.hashCode() = "+user1.hashCode());
        System.out.println("user2.hashCode() = "+user2.hashCode());

    }
}

结果

image-20200924195043007


3.2.原型模式(prototype):

显式地写明一个beanscope就可以改变其作用域:

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

这样就可以改为原型模式。

image-20200924192751870

修改beans.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 http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="user" class="com.duzhuan.pojo.User" c:id="1" c:name="duzhuan" scope="prototype"/>

</beans>

运行之前的测试样例得到结果:

image-20200924195219647

原型模式每次从容器get的时候,都会产生一个新的对象。

3.3 Request, Session, Application, and WebSocket Scopes

The request, session, application, and websocket scopes are available only if you use a web-aware Spring ApplicationContext implementation (such as XmlWebApplicationContext).

这些都是要在web中才能生效的,在web开发中使用,暂且按下。

posted @ 2020-09-24 20:15  杜撰丶  阅读(355)  评论(0编辑  收藏  举报