Spring中的Bean是有生命周期

本文介绍了Spring框架中Bean的生命周期管理,包括初始化和销毁的过程。详细讲述了三种定义Bean生命周期回调方法的方式:实现接口InitializingBean和DisposableBean;使用XML配置文件中的init-method和destroy-method属性;利用注解@PostConstruct和@PreDestroy。

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

Spring容器中的Bean是有生命周期,Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作。下面是常用的三种指定特定操作的方法:

通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。

xml配置
Xml代码 收藏代码

<bean id="myInitializingBean" class="com.sf.sfbuy2.context.filter.MyInitializingBean">
</bean>
<bean id="myXmlBean" class="com.sf.sfbuy2.context.filter.MyXmlBean" init-method="initMethod" destroy-method="destroyMethod"></bean>
<bean id="myAnnoationBean" class="com.sf.sfbuy2.context.filter.MyAnnoationBean"></bean>

方法一
Java代码 收藏代码

import javax.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyInitializingBean implements InitializingBean,DisposableBean{

public MyInitializingBean(){
System.out.println("执行InitAndDestroySeqBean: 构造方法");
}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet");
}


@PreDestroy
public void preDestroy() {
System.out.println("执行InitAndDestroySeqBean: preDestroy");
}

@Override
public void destroy() throws Exception {
System.out.println("执行InitAndDestroySeqBean: destroy");
}

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();
}
}

方法二、
Java代码 收藏代码

public class MyXmlBean{

public MyXmlBean(){
System.out.println("执行MyXmlBean: 构造方法");
}
public void initMethod() {
System.out.println("执行MyXmlBean: init-method");
}
public void destroyMethod() {
System.out.println("执行MyXmlBean: destroy-method");
}

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();
}
}

方法三、
Java代码 收藏代码

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyAnnoationBean{

public MyAnnoationBean(){
System.out.println("执行myAnnoationBean: 构造方法");
}
@PostConstruct
public void postConstructMethod() {
System.out.println("执行myAnnoationBean: postConstructMethod初始化方法");
}

@PreDestroy
public void preDestroyMethod() {
System.out.println("执行myAnnoationBean: destroy-method销毁方法");
}

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();
}
}

Bean在实例化的过程中:Constructor > @PostConstruct >InitializingBean > init-method

Bean在销毁的过程中:@PreDestroy > DisposableBean > destroy-method
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值