那些年我们一起着迷的Spring:作用域与生命周期(四)

spring面试题及答案解析

参考:Bean(xml 配置) ---- Spring入门
来源:慕课网


bean的配置项(常用)

Id:整个IOC容器中bean的唯一标识
Class:具体要实例化的类
Scope:是单例还是多例
Constructor arguments:构造参数
Properties:成员变量
Autowiring mode:自动装配模式
lazy—initialization mode:懒加载模式
Initialization/destruction method:初始化和销毁的方法

spring中Bean的作用域

作用域描述
singleton:指一个 Bean 容器中只存在一份,context 只存在一份
prototype:每次请求(每次使用)创建新的实例,destroy 方式不生效
request:每次 http 请求创建一个实例且仅在当前 request 内有效
session:(同上) 在一次Http Session中,容器会返回该Bean的同一实例。而对不同的Session请求则会创建新的实例,该bean实例仅在当前Session内有效。
global Session:基于 portlet 的 web 中有效(portlet 定义了 global session),如果是 web 中,同 session

五种作用域中,request、session和global session三种作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架),只能用在基于web的Spring ApplicationContext环境。

慕课网Bean 的作用域案例
Bean 类

package com.imooc.bean;
public class BeanScope {
  public void say() {
  System.out.println("BeanScope say : " + this.hashCode());
  }
}

spring 配置 Bean
spring-beanscope.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" > 

<--在同一个IOC容器中-->
<bean id="beanScope" class="com.imooc.bean.BeanScope" scope="singleton"></bean> (每次请求都调用同一实例----单例)
当<bean id="beanScope" class="com.imooc.bean.BeanScope" scope="prototype"></bean> 
(每次请求都会创建不同的实例)</beans>
</beans>

单元测试方法

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {

  public TestBeanScope() {
  super("classpath*:spring-beanscope.xml");
  }

  @Test
  public void testSay() {
  //BeanScope scope=singleton 单例
  //beanScope 和 beanScope2 的 hashCode 相同 --> 同一份容器中只会出现一个 bean。

  //配置文件中scope="prototype"时
  //beanScope 和 beanScope2 的 hashCode 不相同 -->同一份容器中会出现多个 bean
  BeanScope beanScope = super.getBean("beanScope");
  beanScope.say();

  BeanScope beanScope2 = super.getBean("beanScope");
  beanScope2.say();
  }
}

Spring中bean的作用域与生命周期


spring中Bean的生命周期

定义 spring  xml 配置 bean
初始化 context 加载 spring xml,context.start() 初始化
使用 从 Bean 容器中获取使用
销毁 Bean 容器销毁所有 Bean 实例

初始化方法 2种

1.实现 org.springframework.beans.factory.InitializingBean 接口,覆盖
afterPropertiesSet 方法。
接口
2.配置 init-method
配置

Bean 销毁 2种

1.实现 org.springframework.beans.factory.DisposableBean 接口,覆盖 destory 方法
这里写图片描述
2.配置 destroy-method(与init-method类似)

<bean id="" class="" destory-method="cleanup"></bean>

配置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" 
default-init-method="defautInit"  default-destory-method="defautDestory"> <!--全局默认始化、销毁方法 -->

<bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle"  init-method="start" destroy-method="stop"></bean>
</beans>
public class BeanLifeCycle implements InitializingBean, DisposableBean {
	/**
	*默认配置初始化、销毁方法 如果配置了,不实现也不会报错(可选方法)
	*/
	public void defautInit() {
		System.out.println("Bean defautInit.");
	}
	
	public void defaultDestroy() {
		System.out.println("Bean defaultDestroy.");
	}
	/**
	*实现接口的 销毁、初始化方法
	*/
	@Override
	public void destroy() throws Exception {
		System.out.println("Bean destroy.");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Bean afterPropertiesSet.");
	}
	/**
	*Bean配置 初始化、销毁方法  配置了就一定要实现,不然会报错
	*/
	public void start() {
		System.out.println("Bean start .");
	}
	
	public void stop() {
		System.out.println("Bean stop.");
	}
	
}

注:当三种方法同时使用时,先执行接口方法,然后执行 bean 配置方法,不执行默认方法(默认方法是一个可选的方法)【如果实现了接口,或在bean里配置了,可不配置默认的 default-init-method="defautInit" default-destory-method="defautDestory"方法】。

bean的实例化过程
Spring中bean的实例化过程

  • 实例化一个Bean,也就是我们通常说的new。

  • 按照Spring上下文对实例化的Bean进行配置,也就是IOC注入。

  • 如果这个Bean实现了BeanNameAware接口,会调用它实现的setBeanName(String beanId)方法,此处传递的是Spring配置文件中Bean的ID。

  • 如果这个Bean实现了BeanFactoryAware接口,会调用它实现的setBeanFactory(),传递的是Spring工厂本身(可以用这个方法获取到其他Bean)。

  • 如果这个Bean实现了ApplicationContextAware接口,会调用setApplicationContext(ApplicationContext)方法,传入Spring上下文。

  • 如果bean实现了BeanPostProcessor接口,它的postProcessBeforeInitialization方法将被调用;

  • 如果bean实现了InitializingBean接口,spring将调用它的afterPropertiesSet接口方法,类似的如果bean使用了init-method属性声明了初始化方法,该方法也会被调用;

  • 如果bean实现了BeanPostProcessor接口,它的postProcessAfterInitialization接口方法将被调用;

  • 当Bean不再需要时,会经过清理阶段,如果Bean实现了DisposableBean接口,会调用其实现的destroy方法。

  • 最后,如果这个Bean的Spring配置中配置了destroy-method属性,会自动调用其配置的销毁方法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值