笔记:Spring的Bean实例的作用域

Spring bean 实例作用域

作用域类型

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

类别说明
singleton在SpringIOC容器中仅存在一个Bean实例,Bean以单实例的方式存在
prototype每次调用getBean()时都会返回一个新的实例
request每次HTTP请求都会创建一个新的Bean,该作用域仅适合于WebApplicationContext环境
session同一个HTTP Session 共享一个Bean,不同的HTTP session使用不同的Bean。该作用域仅适合于WebApplicationContext环境

在这里插入图片描述
当bean的作用域为单例时,Spring会在IOC容器对象创建时就创建bean的对象实例

代码测试

public class Book {
	private Integer id;
	private String title;
	private String author;
	private double price;
	private Integer sales;

	public Book() {
		System.out.println("Book对象被创建了");
	}
	public Book(Integer id, String title, String author, double price, Integer sales) {
		super();
		this.id = id;
		this.title = title;
		this.author = author;
		this.price = price;
		this.sales = sales;
		System.out.println("全参的构造器被调用");
	}
	public Book(Integer id, String title, String author, double price) {
		super();
		this.id = id;
		this.title = title;
		this.author = author;
		this.price = price;
		System.out.println("不含销量的构造器被调用");
	}
	public Book(Integer id, String title, String author, Integer sales) {
		super();
		this.id = id;
		this.title = title;
		this.author = author;
		this.sales = sales;
		System.out.println("不含价格的构造器被调用");
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}

	public Integer getSales() {
		return sales;
	}

	public void setSales(Integer sales) {
		this.sales = sales;
	}

	@Override
	public String toString() {
		return "Book [id=" + id + ", title=" + title + ", author=" + author + ", price=" + price + ", sales=" + sales
				+ "]";
	}

}
class SpringTest {
	//01.Spring Bean的作用域之间有什么区别
	//创建IOC容器对象
	ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
	@Test
	void testBook() {
		Book book = (Book) ioc.getBean("book");
		Book book2 = (Book) ioc.getBean("book");
		System.out.println(book==book2);
	}
}

更改beans.xml中的配置

<bean id="book" class="com.atguigu.spring.beans.Book" scope="singleton">
	 	<property name="id" value="8"></property>
	 	<property name="title" value="红高粱"></property>
	 	<property name="author" value="莫言"></property>
	 	<property name="price" value="10.00"></property>
	 	<property name="sales" value="800"></property>
	</bean>
  1. singleton 时 ,运行结果如下: 只创建一次bean
    在这里插入图片描述
  2. prototype:运行结构如下
<bean id="book" class="com.atguigu.spring.beans.Book" scope="prototype">
...
</bean>

每次实例化都会创建一个新的Book对象
在这里插入图片描述

学习途径:尚硅谷视频

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值