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();
    }
}

结果


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