JAVA-Spring Bean作用域

简介: JAVA-Spring Bean作用域

目录

基本概念

Bean

作用域

spring支持的bean作用域有哪些?

实例:singletgn和prototype

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。
近日研究Spring和SpringBoot的一些内容,给大家做一些分享,请大家多多提出您的宝贵意见

学习知识要了解其涉及到的基本概念,才能理解这个知识,并且做到融汇贯通。

基本概念

Bean

官网链接:核心技术 (spring.io)

官网上介绍,bean是一个由Spring IoC容器实例化、组装和管理的对象。

作用域

首先,我们先了解什么是作用域,为什么要有作用域?

百度百科作用域概念:百度百科-验证

只要是代码,就至少有一个作用域

spring支持的bean作用域有哪些?

① singletgn(唯一Bean实例)

使用该属性定义Bean时,IOC容器仅创建一个Bean实例,IOC容器每次返回的是同一个Bean实例。
② prototype(原型Bean)

使用该属性定义Bean时,lOC容器可以创建多个Bean实例,每次返回的都是一个新的实例。
③ request

该属性仅对HTTP请求产生作用,使用该属性定义Bean时,每次HTTP请求都会创建一个新的Bean,适用于WebApplicationContext环境。
④ session

该属性仅用于HTTP Session,同一个Session共享一个Bean实例。不同Session使用不同的实例。
⑤ global-session

该属性仅用于HTTP Session,同session作用域不同的是,所有的Session共享一个Bean实例。

实例:singletgn和prototype

写了一个maven项目引入了spring-webmvc基础依赖,对singletgn和prototype二者做个比较
pom.xml文件

UserService文件

package com.tfjy.test;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
 * @BelongsPackage: com.tfjy.test
 * @Author: aqiu
 * @Description: 服务类
 * @CreateTime: 2023-01-28 09:59
 * @Version: 1.0
 */
@Component
//@Scope("prototype")  //这里是开启注解,使用property类型的bean
public class UserService {
}

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: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"/>
    <!--    将UserService设置为原型bean-->
<!--    <bean id="UserService" class="com.tfjy.test.UserService" scope="prototype"></bean>-->
</beans>

Test文件

import com.tfjy.test.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @BelongsProject: demo
 * @Author: aqiu
 * @Description: TODO
 * @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");
        UserService userServiceOne = context.getBean("UserService", UserService.class);
        UserService userServiceTwo = context.getBean("UserService", UserService.class);
        System.out.println(userServiceOne);
        System.out.println(userServiceTwo);
        //通过equals方法判断两个对象是否相等
        if(userServiceOne.equals(userServiceTwo)){
            System.out.println("两次getBean方法,获得了同一个单例对象");
        }else{
            System.out.println("两次getBean方法,获得的不是同一个单例对象");
        }
    }
}

不做任何操作,默认的获得了同一个单例对象 使用两种方式可以更换成property类型的bean

其一,使用注解的方式

在需要交由IOC容器管理的bean对象类上面添加**@Scope(“prototype”)**注解。

其二,使用xml配置文件的方式

bean.xml文件中的

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


如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。

相关文章
|
2天前
|
安全 Java Spring
Spring框架中的单例Bean是线程安全的吗?
Spring框架中的单例Bean是线程安全的吗?
10 1
|
2天前
|
XML 前端开发 Java
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
5 0
|
2天前
|
存储 缓存 Java
【JavaEE】Spring中注解的方式去获取Bean对象
【JavaEE】Spring中注解的方式去获取Bean对象
3 0
|
2天前
|
存储 Java 对象存储
【JavaEE】Spring中注解的方式去存储Bean对象
【JavaEE】Spring中注解的方式去存储Bean对象
7 0
|
2天前
|
存储 Java 对象存储
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
9 0
|
2天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
21 1
|
2天前
|
XML Java 数据格式
Spring Bean
【4月更文挑战第30天】Spring Bean
17 0
|
2天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
32 2
|
2天前
|
XML Java 数据格式
谈谈 Spring 中 Bean 的生命周期?
谈谈 Spring 中 Bean 的生命周期?
20 1
|
2天前
|
XML Java 数据格式
Spring Bean的定义(含创建Bean的三种方式)
Spring Bean的定义(含创建Bean的三种方式)