第一种注解:

  通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种是XML配置:

  通过 在xml中定义init-method 和  destory-method方法

第三种是接口实现:

  通过bean实现InitializingBean和 DisposableBean接口

具体接口和方法如表

Spring Bean 生命周期各阶段
相关接口及方法
Bean 自身方法
Bean 本身业务的方法:
配置文件中 init-method 和 destroy-method 指定的方法
Bean 生命周期接口方法
Initial izingBean 接口
DiposableBean 接口
BeanNameAware 接口
ApplicationCantextAware 接口
BeanFac町Aware 接口
其他
容器级生命周期接口方法
(一般称为“后处理器”)
lnstantiationAwareBeanPostPracessor 接口实现
BeanPostProcessor 接口实现
工厂级生命周期接口方法
(也可以归为容器级的)
AspecUWeavingEnabler
Con丑gurationC!assPostProcessor
CustomAutowireConfigurer

 

生命周期 序图 

Spring 的 init-method 和 destory-method

 

相关文章:

  • 2022-12-23
  • 2021-11-01
  • 2021-07-10
  • 2021-09-10
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
  • 2022-03-04
  • 2021-11-16
  • 2022-12-23
  • 2021-05-23
相关资源
相似解决方案