知识点三:Bean的初始化方法和销毁方法

本文详细解析了Spring框架中Bean的初始化和销毁过程,包括不同作用域下Bean的实例化时机,以及如何通过init-method和destroy-method属性指定初始化和销毁方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 在容器中注册的bean到底什么时候初始化的问题?
验证方式:在默认方法里面输出一句话即可知道,是否容器建立后就实例化在里面注册的bean了。
------------
public class PersonServiceBean implements PersonService {
public PersonServiceBean() {
System.out.println("我被实例化了");
}
}

-------------
<bean id=”personService” class=”cn.beanservice.PersonService” />
-------------
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");


//输出了“我被实例化了”。说明容器建立时就实例化注册在里面的bean

//////////////////如果注册时,改为scope=”prototype”,那么容器实例化的时候不会实例化bean,只有在通过容器getBean的时候实例化!!但是如果是scope=”singleton”或者省略会与容器同时。

加上lazy-init=”false”:不在容器实例化的时候实例化该bean

如何让bean在实例化的时候执行一个方法?
指定bean的属性init-method=”init”
public class PersonServiceBean implements PersonService {
public void init() {
System.out.println("做好数据库链接");
}
Public void save(){
System.out.println(“执行save方法”);
}
}

<bean id=”personService” class=”cn.beanservice.PersonService”
Inin-method=”init”/>
这样不管什么时候初始化bean,都会执行init-method参数指定的方法
同时,我们在销毁一个实例的时候,也需要做一些工作,比如,关闭数据库连接
指定bean的属性  destroy-method

public class PersonServiceBean implements PersonService {
public void init() {
System.out.println("做好数据库链接");
}
Public void save(){
System.out.println(“执行save方法”);
}
Public void destroy(){
System.out.println(“我被销毁了”);
}
}

<bean id=”personService” class=”cn.beanservice.PersonService”
Inin-method=”init” destroy-method=”destroy”/>

现在一个问题:容器中的bean什么时候被销毁?应该是容器被关闭的时候。
容器关闭我们需要一个抽象类:
AbstractApplicationContext ctx = new 
ClassPathXmlApplicationContext(“beans.xml”);

………
ctx.close();


-----------使用实现接口的方式初始化和销毁bean-------------
Lifecycle接口

Spring提供了几个标志接口(marker interface),这些接口用来改变容器中bean的行为;它们包括InitializingBean和DisposableBean。实现这两个接口的bean在初始化和析构时容器会调用前者的afterPropertiesSet()方法,以及后者的destroy()方法。

Spring在内部使用BeanPostProcessor实现来处理它能找到的任何标志接口并调用相应的方法。如果你需要自定义特性或者生命周期行为,你可以实现自己的 BeanPostProcessor。关于这方面更多的内容可以看第 3.7 节 “容器扩展点”。

下面讲述了几个生命周期标志接口。在附录中会提供相关的示意图来展示Spring如何管理bean,以及生命周期特性如何改变bean的内在特性。

初始化回调

实现org.springframework.beans.factory.InitializingBean接口允许容器在设置好bean的所有必要属性后,执行初始化事宜。InitializingBean接口仅指定了一个方法:

void afterPropertiesSet() throws Exception;
通常,要避免使用InitializingBean接口(而且不鼓励使用该接口,因为这样会将代码和Spring耦合起来)可以在Bean定义中指定一个普通的初始化方法,即在XML配置文件中通过指定init-method属性来完成。如下面的定义所示:

<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {

public void init() {
// do some initialization work
}
}
(效果)与下面完全一样

<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements InitializingBean {

public void afterPropertiesSet() {
// do some initialization work
}
}
但是没有将代码与Spring耦合在一起。

3.5.1.2. 析构回调
实现org.springframework.beans.factory.DisposableBean接口的bean允许在容器销毁该bean的时候获得一次回调。DisposableBean接口也只规定了一个方法:

void destroy() throws Exception;
通常,要避免使用DisposableBean标志接口(而且不鼓励使用该接口,因为这样会将代码与Spring耦合在一起)可以在bean定义中指定一个普通的析构方法,即在XML配置文件中通过指定destroy-method属性来完成。如下面的定义所示:

<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean {

public void cleanup() {
// do some destruction work (like releasing pooled connections)
}
}
(效果)与下面完全一样

<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements DisposableBean {

public void destroy() {
// do some destruction work (like releasing pooled connections)
}
}
但是没有将代码与Spring耦合在一起。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值