前言:最近在进行springBean的作用域学习,并且学习了对应的例子。这里进行一下总结 一:Bean的作用域基础概念

如果想学习SpringBean的生命周期,那么就必须要学习Bean的作用域。因为不同的作用域的bean的生命周期不同

1:singleton(唯一bean实例,由Spring容器管理其生命周期)
2:prototype(原型bean,创建后容器不管理其生命周期)
3:request(每次http都产生新的bean,仅在http request内有效)
4:session(首次http请求创建一个实例,作用域是浏览器首次访问直至浏览器关闭)
5:global-session(全局 session 作用域,仅仅在基于 Portlet 的 web 应用中才有意义,Spring5 已经没有了。

  • singleton 是默认的作用域,我们如果不对bean的scope配置项进行配置的话,默认就是Singleton类型。 在创建起容器时就同时自动创建了一个 bean 的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。
  • prototype: 原型bean,每次对此类型的bean进行请求的时候,都会创建一个新的bean实例。与我们 java中的new效果类似。Spring 只负责创建,当容器创建了 Bean 的实例后,Bean 的实例就交给客户端代码管理,Spring 容器将不再跟踪其生命周期。
  • request:每次HTTP请求都会创建一个新的Bean
  • session:首次http请求创建一个实例,作用域是浏览器首次访问直至浏览器关闭
  • global-session:作用域范围是WebApplicationContext中。一般用于Portlet应用环境,该运用域仅适用于WebApplicationContext环境。

后三种只有在web环境下才有效。

bean的作用域具体实现

我针对对前两种作用域编写了一个对应的例子,这是一个普通的Maven项目,引进了spring的包。首先看一下项目结构

浅谈springBean的作用域

1.空的AService类

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.tfjy.test
 * @Author: haolizhuo
 * @Description: A服务类
 * @CreateTime: 2023-01-28 09:59
 * @Version: 1.0
 */
@Component
//@Scope("prototype")
public class AService {

}

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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!--开启注解的支持-->
    <context:annotation-config/>
    <!-- 自动扫描指定包及其子包下的所有Bean类 -->
    <context:component-scan base-package="com.tfjy.test"/>

    <!--    将AService设置为原型bean-->
<!--    <bean id="AService" class="com.tfjy.test.AService" scope="prototype"></bean>-->

</beans>

Test测试类

/**
 * @BelongsProject: demo
 * @BelongsPackage: PACKAGE_NAME
 * @Author: haolizhuo
 * @Description: test测试类
 * @CreateTime: 2023-01-28 10:01
 * @Version: 1.0
 */
public class Test {
    //bean验证
    @org.junit.Test
    public void beanTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        AService aServiceOne = context.getBean("AService",AService.class);
        AService aServiceTwo = context.getBean("AService",AService.class);

        System.out.println(aServiceOne);
        System.out.println(aServiceTwo);
        //通过equals方法判断两个对象是否相等
        if(aServiceOne.equals(aServiceTwo)){
            System.out.println("两次getBean方法,获得了同一个单例对象");
        }else{
            System.out.println("两次getBean方法,获得的不是同一个单例对象");
        }
    }
}

4.pom文件引入的依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

代码分析

上述代码中直接运行的话,是没有配置bean的作用域的。所以控制台会打印,两次getBean方法,获得了同一个单例对象

浅谈springBean的作用域

我们设置bean对象为prototype类型的方式也有两种。
1.注解方式。
在需要交由IOC容器管理的bean对象类上面添加@Scope(“prototype”)注解。
2.xml配置文件方式

<bean id="AService" class="com.tfjy.test.AService" scope="prototype"></bean>

这两种方式设置任意一种,spring加载bean的时候都会去读取配置,并将对应bean设置为prototype类型。

浅谈springBean的作用域

原文地址:https://blog.csdn.net/hlzdbk/article/details/128811271

相关文章: