在 Spring 框架中,实现 InitializingBean
和 DisposableBean
接口用于管理 Bean 的生命周期回调,分别控制 Bean 的初始化后和销毁前行为。具体作用如下:
1. InitializingBean
接口
public interface InitializingBean {void afterPropertiesSet() throws Exception;
}
- 作用:在 Bean 的依赖注入完成后立即执行初始化逻辑。
- 触发时机:当 Spring 完成以下步骤后调用:
- Bean 实例化(调用构造器)
- 注入所有依赖(通过
@Autowired
或 XML 配置) - 完成其他属性设置(如
@Value
注入)
- 典型用途:
- 数据校验(检查必要属性是否注入)
- 启动后台线程
- 加载初始化数据(如缓存预热)
- 建立数据库连接
public class DatabaseService implements InitializingBean {private DataSource dataSource;@Overridepublic void afterPropertiesSet() throws Exception {// 校验必要依赖if (dataSource == null) {throw new IllegalStateException("DataSource 未注入!");}// 初始化连接池Connection conn = dataSource.getConnection();// ... 其他初始化操作}
}
2. DisposableBean
接口
public interface DisposableBean {void destroy() throws Exception;
}
- 作用:在 Bean 销毁前执行清理操作。
- 触发时机:
- 单例 Bean:Spring 容器关闭时(
context.close()
) - 原型 Bean:Spring 不管理其销毁,需手动触发或依赖其他机制
- 单例 Bean:Spring 容器关闭时(
- 典型用途:
- 关闭数据库连接
- 停止后台线程
- 释放文件句柄或网络资源
- 发送销毁通知日志
public class ResourceHolder implements DisposableBean {private Thread backgroundThread;@Overridepublic void destroy() throws Exception {// 安全停止线程backgroundThread.interrupt();// 释放资源releaseFileHandles();}
}
为什么不推荐直接使用这两个接口?
- 侵入性强:强制 Bean 实现 Spring 特定接口,导致代码与 Spring 耦合。
- 替代方案:Spring 提供更灵活的、无侵入的生命周期管理方式:
- 初始化替代方案:
@PostConstruct
注解- XML 配置中
init-method
属性(如<bean init-method="init">
) - Java 配置中
@Bean(initMethod = "customInit")
- 销毁替代方案:
@PreDestroy
注解- XML 配置中
destroy-method
属性 @Bean(destroyMethod = "customCleanup")
- 初始化替代方案:
生命周期回调顺序示例
当使用多种初始化/销毁方式时,执行顺序如下:
public class ExampleBean implements InitializingBean, DisposableBean {// 构造器 -> 依赖注入 -> @PostConstruct -> afterPropertiesSet() -> 自定义init-method@PostConstructpublic void postConstruct() { /* 1st */ }@Overridepublic void afterPropertiesSet() { /* 2nd */ }public void customInit() { /* 3rd */ }// 销毁顺序:@PreDestroy -> destroy() -> 自定义destroy-method@PreDestroypublic void preDestroy() { /* 1st */ }@Overridepublic void destroy() { /* 2nd */ }public void customDestroy() { /* 3rd */ }
}
最佳实践建议
- 优先使用无侵入方案:用
@PostConstruct
和@PreDestroy
替代接口,降低框架耦合度。 - 需要细粒度控制时:例如需要确保在依赖注入后立刻执行关键逻辑(早于其他初始化方法),可使用
afterPropertiesSet()
。 - 处理遗留代码:旧版 Spring 项目可能依赖这些接口,迁移时可逐步替换。
💡 总结:
InitializingBean
和DisposableBean
是 Spring 生命周期管理的底层机制。现代 Spring 开发中,应优先选择@PostConstruct
和@PreDestroy
等解耦方案,但在需要精确控制执行顺序的场景下仍可合理使用接口。