Spring学习第七天:Bean的作用域

在 Spring 中, 可以在 < bean> 元素的 scope 属性里设置 Bean 的作用域.
默认情况下, Spring 只为每个在 IOC 容器里声明的 Bean 创建唯一一个实例(单例), 整个 IOC 容器范围内都能共享该实例:所有后续的 getBean() 调用和 Bean 引用都将返回这个唯一的 Bean 实例.该作用域被称为 singleton, 它是所有 Bean 的默认作用域.

新建Spring的配置文件beans-scope.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="com.atguigu.spring.beans.autowire.Car">
        <property name="brand" value="Audi"></property>
        <property name="price" value="300000"></property>
    </bean>
</beans>

调用该bean,并作比较

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
Car car1 = ctx.getBean(Car.class);
Car car2 = ctx.getBean(Car.class);
System.out.println(car1 == car2);

输出结果

true

可见,在默认的scope下是单例的,car1 和 car2其实是同一个对象。

设置car的scope

<!-- 使用bean的scope来配置作用域,
    singleton: 默认值,容器初始化时创建bean实例,在整个容器的生命周期里面只创建一个bean,单例模式 -->
<bean id="car" class="com.atguigu.spring.beans.autowire.Car" scope="prototype">
    <property name="brand" value="Audi"></property>
    <property name="price" value="300000"></property>
</bean>

执行上面程序,输出结果

false

prototype: 表示每次向容器获取一个bean,都会生成一个新的bean。

我们在构造器输出一定的string字符串来更加直观的了解这个过程。

在Car类中加入如下代码:

public Car() {
    // TODO Auto-generated constructor stub
    System.out.println("Car's constructor...");
}

也就是增加一个无参的构造函数,这个构造函数只输出一个string字符串

XML配置

<bean id="car" class="com.atguigu.spring.beans.autowire.Car" scope="singleton">
    <property name="brand" value="Audi"></property>
    <property name="price" value="300000"></property>
</bean>

调用如下代码:

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");

输出如下内容:

Car's constructor...

调用如下代码:

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
Car car1 = ctx.getBean(Car.class);
Car car2 = ctx.getBean(Car.class);
System.out.println(car1 == car2);

输出如下内容

Car's constructor...
true

可见,当scope为singleton的时候,在spring IOC容器初始化时就初始化了bean,且整个容器中只有一个实例。

XML配置

<!-- protetype: 原型的,容器初始化时不创建bean的实例,而在每次请求时都创建一个新的bean实例 返回-->
<bean id="car" class="com.atguigu.spring.beans.autowire.Car" scope="prototype">
    <property name="brand" value="Audi"></property>
    <property name="price" value="300000"></property>
</bean>

调用如下代码:

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");

输出为空

调用如下代码:

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
Car car1 = ctx.getBean(Car.class);
Car car2 = ctx.getBean(Car.class);
System.out.println(car1 == car2);

输出结果:

Car's constructor...
Car's constructor...
false

可见,当scope为prototype的时候,spring IOC容器初始化时并没有初始化bean,在每次调用的时候才会初始化一个新的bean对象`

还有两个request和session相对用的比较少,就不做说明。具体可见下表

spring bean的作用域

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值