Spring系列文章:Bean的作⽤域

简介: Spring系列文章:Bean的作⽤域

1、singleton

默认情况下,Spring的IoC容器创建的Bean对象是单例的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="user" class="com.springcode.example.entity.User"/>
 
</beans>

测试

public class SpringTest {
    @Test
    public void testInsert(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User user = applicationContext.getBean("user", User.class);
        System.out.println(user);
        User user1 = applicationContext.getBean("user", User.class);
        System.out.println(user1);
    }
}

结果

通过测试得知可以看到Spring的IoC容器中,默认情况下,Bean对象是单例的。

这个对象在什么时候创建的呢?可以为SpringBean提供⼀个⽆参数构造⽅法,测试⼀下,如下:

public class User {
    public User(){
        System.out.println("无参构造执行");
    }
}

测试

public class SpringTest {
    @Test
    public void testInsert(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
    }
}

2、prototype

如果想让Spring的Bean对象以多例的形式存在,可以在bean标签中指定scope属性的值为: prototype,这样Spring会在每⼀次执⾏getBean()⽅法的时候创建Bean对象,调⽤⼏次则创建⼏次。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="user" class="com.springcode.example.entity.User" scope="prototype"/>
 
</beans>

测试

我们可以把测试代码中的getBean()⽅法所在⾏代码注释掉:并且user增加无参构造方法

可以看到这⼀次在初始化Spring上下⽂的时候,并没有创建Bean对象。

scope如果没有配置,它的默认值是什么呢?默认值是singleton,单例的。

3、其它scope

scope属性的值不⽌两个,它⼀共包括8个选项:

  1. singleton:默认的,单例。
  2. prototype:原型。每调⽤⼀次getBean()⽅法则获取⼀个新的Bean对象。或每次注⼊的时候都是新 对象。
  3. request:⼀个请求对应⼀个Bean。仅限于在WEB应⽤中使⽤。
  4. session:⼀个会话对应⼀个Bean。仅限于在WEB应⽤中使⽤。
  5. global session:portlet应⽤中专⽤的。如果在Servlet的WEB应⽤中使⽤global session的话,和 session⼀个效果。(portlet和servlet都是规范。servlet运⾏在servlet容器中,例如Tomcat。 portlet运⾏在portlet容器中。)
  6. application:⼀个应⽤对应⼀个Bean。仅限于在WEB应⽤中使⽤。
  7. websocket:⼀个websocket⽣命周期对应⼀个Bean。仅限于在WEB应⽤中使⽤。
  8. ⾃定义scope:很少使⽤。

4、⾃定义Scope

接下来咱们⾃定义⼀个Scope,线程级别的Scope,在同⼀个线程中,获取的Bean都是同⼀个。跨线程 则是不同的对象:(以下内容作为了解)

第⼀步:实现Scope接⼝

spring内置了线程范围的类:org.springframework.context.support.SimpleThreadScope,可 以直接⽤

第⼆步:将⾃定义的Scope注册到Spring容器中

    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="myThread">
                    <bean class="org.springframework.context.support.SimpleThreadScope"/>
                </entry>
            </map>
        </property>
    </bean>

第三步:使⽤Scope

<bean id="user" class="com.springcode.example.entity.User" scope="myThread"/>

测试

public class SpringTest {
    @Test
    public void testInsert(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User user = applicationContext.getBean("user", User.class);
        User user1 = applicationContext.getBean("user", User.class);
 
        System.out.println(user);
        System.out.println(user1);
 
        // 启动线程
        new Thread(()->{
            User usera = applicationContext.getBean("user", User.class);
            User userb = applicationContext.getBean("user", User.class);
            System.out.println(usera);
            System.out.println(userb);
        }).start();
    }
}

结果


相关文章
|
4天前
|
存储 Java 数据库
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
|
9天前
|
Java Spring 容器
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
21 1
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
|
9天前
|
Java 容器 Spring
Spring的加载配置文件、容器和获取bean的方式
Spring的加载配置文件、容器和获取bean的方式
21 3
Spring的加载配置文件、容器和获取bean的方式
|
9天前
|
Java Spring 容器
Spring核心概念、IoC和DI的认识、Spring中bean的配置及实例化、bean的生命周期
Spring核心概念、IoC和DI的认识、Spring中bean的配置及实例化、bean的生命周期
28 0
|
10天前
|
XML Java 数据格式
Spring框架学习 -- Bean的生命周期和作用域
Spring框架学习 -- Bean的生命周期和作用域
16 2
|
10天前
|
存储 XML Java
Spring框架学习 -- 读取和存储Bean对象
Spring框架学习 -- 读取和存储Bean对象
13 0
Spring框架学习 -- 读取和存储Bean对象
|
18天前
|
XML 前端开发 Java
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
17 0
|
18天前
|
存储 缓存 Java
【JavaEE】Spring中注解的方式去获取Bean对象
【JavaEE】Spring中注解的方式去获取Bean对象
4 0
|
18天前
|
存储 Java 对象存储
【JavaEE】Spring中注解的方式去存储Bean对象
【JavaEE】Spring中注解的方式去存储Bean对象
13 0
|
18天前
|
存储 Java 对象存储
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
11 0