天天看点

Spring-Bean的作用域什么是作用域作用域的类型作用域的实例

目录

什么是作用域

作用域的类型

作用域的实例

单例模式

1.基础服务的类

2.xml配置文件

3.测试类

4.引入依赖的pom文件

5.验证结果

原型模式

1.修改配置文件

2.添加注解

什么是作用域

        一段程序代码中所用到的名字并不总是有效/可用的,而限定这个名字的可用性的代码范围就是这个名字的作用域。

        Bean 的作用域是指 Bean 在 Spring 整个框架中的某种行为模式。比如 singleton 单例作用域,就表示 bean 对象在整个 Spring 中只有一份,它是全局共享的,那么当其他人修改了这个值之后,那么另一个人读取到的就是被修改的值。

作用域的类型

1.单例模式(singleton): 唯一 bean 实例,由 Spring 容 器管理其生命周期 ,获取或者注入bean对象,都是针对同一个对象来说的。

2.原型模式(prototype): 原型 bean ,创建后容器不管 理其生命周期 ,原型模式和单例模式正好相反,每次请求原型模式的bean对象都会创建新的实例。

3.请求作用域(request):每次 http 都产生新的 bean , 仅在 http request 内有效 

4.会话作用域 (session): 首次 http 请求创建一个实例, 作用域是浏览器首次访问直至浏览器关 闭 ) 。global-session( 全局 session 作用域, 仅仅在基于 Portlet 的 web 应用中才有 意义, Spring5 已经没有了。

作用域的实例

单例模式

1.基础服务的类

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.tfjy.test
 * @Author: wuzilong
 * @Description: 单例模式实例
 * @CreateTime: 2023年2月6日08:59:35
 * @Version: 1.0
 */
@Component
public class AService {

}
           

2.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"/>

</beans>
           

3.测试类

import com.tfjy.test.AService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @BelongsProject: demo
 * @BelongsPackage: PACKAGE_NAME
 * @Author: wuzilong
 * @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文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <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>

</project>
           

5.验证结果

Spring-Bean的作用域什么是作用域作用域的类型作用域的实例

         我们可以通过打印两个对象对应的hashcode码判断两个对象是否为同一个对象,我们没有设置bean对象的类型默认是单例模式。当然我们也可以通过配置将bean对象的类型改为其他类型。下面改为原型模式看看结果。

原型模式

        设置bean对象为原型模式有两个方式,第一种是通过在xml配置文件中添加配置项,另一个种是通过注解的方式设置为原型模式。

1.修改配置文件

<?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>
           
Spring-Bean的作用域什么是作用域作用域的类型作用域的实例

        通过在pom配置文件中添加对bean对象的设置,发现两个对象的hashcode码是不一致的,说明两个bean对象不是同一个对象,验证了原型模式每次请求bean对象都需要创新一个新的对象。 

2.添加注解

Spring-Bean的作用域什么是作用域作用域的类型作用域的实例

通过添加 @Scope("prototype")也能改变bean对象的类型。