自定义Bean的性质

生命周期回调

为了对容器bean生命周期的管理进行交互,可以实现InitializingBean和DisposableBean接口。容器为前者调用afterPropertiesSet(),为后者调用destroy(),让bean在初始化和销毁你的Bean时执行某些动作。

@PostConstruct和@PreDestroy注解通常被认为是在现代spring应用程序中接受生命周期回调的最佳实践。

也可以用init-method和destroy-method定义元数据。

<bean id="exampleInitBean" class ="com.ExampleBean" init-method="init">

public class ExampleBean {

    public void init(){}

}

等同于

public class AnotherExampleBean implements InitializingBean {

    public void afterPropertiesSet(){}

}

 

posted @ 2023-03-10 14:52  小路不懂2  阅读(12)  评论(0编辑  收藏  举报