春天有没有像@predestroy 这样的东西,就像在温莎城堡里一样 [英] is there something like @predestroy in the spring as in the castle windsor

查看:26
本文介绍了春天有没有像@predestroy 这样的东西,就像在温莎城堡里一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

spring-framework 中有没有类似 @PreDestroy 的东西?

Anything like @PreDestroy in the spring-framework?

推荐答案

如果你定义了一个实现 DisposableBean 接口的 bean,那么 Spring 将调用

If you define a bean that implements the DisposableBean interface then Spring will call the

void destroy() throws Exception;

删除 bean 之前的方法.

method before destrying the bean.

这是一种方式,另一种是您的 bean 不必实现给定的接口.在你的一个 ConfigurationSupport 类中,你的 bean 必须被定义为带有 @Bean 注释的 pulic 方法.

That's one way, the other is when your bean doesn't have to implement the given interface. In one of yours ConfigurationSupport classes your bean has to be defined as as pulic method with the @Bean annotation.

   @Bean (destroyMethod="yourDestroyMethod")
   public YourBean yourBean() {
      YourBean yourBean = new YourBean();

      return yourBean;
   }

必须在 YourBean.class 中定义方法yourDestroyMethod",然后 Spring 会在销毁 bean 之前调用它.

The method "yourDestroyMethod" has to be defined in YourBean.class and then Spring will call it before destroying the bean.

有关更多信息,请参阅 Spring 文档:破坏回调

For more info see the Spring documentation: Destruction callbacks

更新

第三种方法... 我什至会说更好的方法是指定 bean 的init-method"和destroy-method"...像这样:mkyong.com/spring/spring-init-method-and-destroy-method-例子

The third way... I would even say the better way would be to specifiy "init-method" and "destroy-method" of your bean... like this: mkyong.com/spring/spring-init-method-and-destroy-method-example

解决了第三方依赖bean的问题,解放了代码不必要的Spring接口..

This solves the problem ot third-party dependency beans, and liberates the the code unnecessary Spring interfaces..

这篇关于春天有没有像@predestroy 这样的东西,就像在温莎城堡里一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆