首页 > 编程技术 > java

浅谈springBean的作用域

发布时间:2023-2-8 16:13 作者:郝老三

前言:最近在进行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 已经没有了。

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

bean的作用域具体实现

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

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方法,获得了同一个单例对象

我们设置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的作用域的文章就介绍到这了,更多相关springBean 作用域内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://blog.csdn.net/hlzdbk/article/details/128811271

标签:[!--infotagslink--]

您可能感兴趣的文章: