春天有像温莎城堡里的@predestroy 这样的东西吗
spring 框架中有类似 @PreDestroy
的东西吗?
Anything like @PreDestroy
in the spring-framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
spring 框架中有类似 @PreDestroy
的东西吗?
Anything like @PreDestroy
in the spring-framework?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
如果您定义一个实现 DisposableBean 接口的 bean,那么 Spring 将
在销毁该 bean 之前调用该方法。
这是一种方式,另一种方式是当您的 bean 不必实现给定的接口时。
在您的 ConfigurationSupport 类之一中,必须将 bean 定义为带有 @Bean 注释的公共方法。
方法“yourDestroyMethod”必须在YourBean.class中定义,然后Spring将在销毁bean之前调用它。
有关更多信息,请参阅 Spring 文档: 销毁回调
更新
第三种方法...我什至会说更好的方法是指定“init-method”和“destroy-method”的你的bean...像这样: mkyong.com/spring/spring -init-method-and-destroy-method-example
这解决了第三方依赖bean的问题,并解放了代码中不必要的Spring接口。
If you define a bean that implements the DisposableBean interface then Spring will call the
method before destrying the bean.
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.
The method "yourDestroyMethod" has to be defined in YourBean.class and then Spring will call it before destroying the bean.
For more info see the Spring documentation: Destruction callbacks
UPDATE
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
This solves the problem ot third-party dependency beans, and liberates the the code unnecessary Spring interfaces..
有 3 种方法可以做到这一点。
我最喜欢的是 @PreDestroy 方法。
为此,您需要:
在 application-context.xml 中添加以下架构:
context:annotation-config/ 使 @PreDestroy 和 @PostDestroy 标记可用。
现在假设您有 ShutDownBean,您希望在调用关闭回调时运行一些代码。
部分注册 bean。现在你完成了。
如果您有一个桌面应用程序,那么要使用
@PreDestroy
注释,您需要像这样关闭它:注意:
AbstractApplicationContext
具有registerShutdownHook()
的实现,因此这是您可以使用的最小类。您还可以对您不控制其实现的类使用
destroy-method
标记。例如,您可以将其添加到applcation-context.xml
中:destroy-method
值可以具有任何可见性,但不需要有任何参数。希望这有帮助!
There are 3 ways to do that.
My favorite is the @PreDestroy method.
To do that u need:
In application-context.xml add the following schema:
The context:annotation-config/ makes the @PreDestroy and the @PostDestroy tags available.
Now lets say that you have the ShutDownBean that you want to run some code when the shutdown callback is called.
The
<bean id="shutDownBean" class="spring.ShutDownBean" />
part registers the bean.Now you are done.
If you have a desktop application then to use the
@PreDestroy
annotation you need to close it like this:note:
AbstractApplicationContext
has the implementation ofregisterShutdownHook()
so this is the minimum class you can use.Also you can use the
destroy-method
tag for classes you do not control their implementation. For example you can add this in yourapplcation-context.xml
:The
destroy-method
value can have any visibility but needs to have no arguments.Hope this helps!
您的意思是使用标准 JDK
@PreDestroy
注释一个方法?这在 Spring 中很常见,通常比在 XML 中的 bean 声明上使用destroy-method
属性更好。您所要做的就是包含在您的配置文件中,Spring 会处理其余的事情。
You mean like annotating a method with the standard JDK
@PreDestroy
? That's common enough in Spring, and usually better than using adestroy-method
attribute on the bean declaration in XML. All you have to do is includeIn your configuration file and Spring handles the rest.
有标准的 .NET
IDisposable.Dispose()
方法。我不知道 Spring,但从快速谷歌搜索看来,@predestroy 几乎是相同的概念。there's standard .NET
IDisposable.Dispose()
method. I don't know Spring but from quick googling it seems that @predestroy is pretty much the same concept.