Spring中的Bean配置 (11)——Bean的作用域

在这里插入图片描述
Spring中的Bean配置 (1)——内容提要
Spring中的Bean配置 (2)—— IOC和DI
Spring中的Bean配置 (3)——在Spring的IOC容器里配置Bean(通过全类名(反射))——基于XML文件的方式
Spring中的Bean配置 (4)——依赖注入方式
Spring中的Bean配置 (5)——字面值
Spring中的Bean配置 (6)——引用其他Bean
Spring中的Bean配置 (7)——注入参数详解:null值和级联属性
Spring中的Bean配置 (8)—— 集合属性
Spring中的Bean配置 (9)—— XML 配置里的 Bean自动装配
Spring中的Bean配置 (10)—— 继承 Bean 配置和依赖 Bean 配置
Spring中的Bean配置 (11)——Bean的作用域
Spring中的Bean配置 (12)——使用外部属性文件
Spring中的Bean配置 (13)—— Spring表达式语言:SpEl
Spring中的Bean配置 (14)——IOC容器中Bean的生命周期
Spring中的Bean配置 (15)——在Spring的IOC容器里配置Bean(通过工厂方法创建Bean)——基于XML文件的方式
Spring中的Bean配置 (16)——在Spring的IOC容器里配置Bean(通过FactoryBean)——基于XML文件的方式

Spring中的Bean配置 (17)——在Spring的IOC容器里配置Bean——基于注解的方式来配置Bean

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

在这里插入图片描述

代码演示

Person.java

package com.atguigu.spring.bean;

public class Person {
	
	private String name;
	
	public Person(String name) {
		
		super();
		
		this.name = name;
		System.out.println("构造函数被调用");
		
	}

	public  Person() {
		
	}

}

application.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的scope属性来配置bean的作用域
    singleton:单例的 ,默认值,容器初始化时创建bean实例,在整个容器的生命周期只创建这一个bean
    prototype:原型的,容器初始化时不创建bean实例,而在每次请求时都创建一个新的bean实例,
 -->
<bean id="person1" class="com.atguigu.spring.bean.Person" >
<constructor-arg value="zlj"></constructor-arg>
</bean>

<bean id="person2" class="com.atguigu.spring.bean.Person" scope="prototype" >
<constructor-arg value="zlj"></constructor-arg>
</bean>

</beans>

Main.java

package com.atguigu.spring.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		
		System.out.println("类别singleton演示");

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");

		Person person1 = (Person) applicationContext.getBean("person1");
		Person person2 = (Person) applicationContext.getBean("person1");
		
		System.out.println("类别prototype演示");
		Person person3 = (Person) applicationContext.getBean("person2");
		Person person4 = (Person) applicationContext.getBean("person2");

		System.out.println(person1==person2);
		System.out.println(person3==person4);

	}

}

结果:在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值