春天有像温莎城堡里的@predestroy 这样的东西吗

发布于 2024-11-05 18:47:46 字数 51 浏览 0 评论 0原文

spring 框架中有类似 @PreDestroy 的东西吗?

Anything like @PreDestroy in the spring-framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尴尬癌患者 2024-11-12 18:47:46

如果您定义一个实现 DisposableBean 接口的 bean,那么 Spring 将

void destroy() throws Exception;

在销毁该 bean 之前调用该方法。

这是一种方式,另一种方式是当您的 bean 不必实现给定的接口时。
在您的 ConfigurationSupport 类之一中,必须将 bean 定义为带有 @Bean 注释的公共方法。

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

      return yourBean;
   }

方法“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

void destroy() throws Exception;

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.

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

      return yourBean;
   }

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..

云之铃。 2024-11-12 18:47:46

有 3 种方法可以做到这一点。

@PreDestroy 标签

xml 中的销毁方法

如上所述的DisposableBean接口

我最喜欢的是 @PreDestroy 方法。

为此,您需要:

在 application-context.xml 中添加以下架构:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="shutDownBean" class="spring.ShutDownBean" />
    <context:annotation-config/>
</beans>

context:annotation-config/ 使 @PreDestroy@PostDestroy 标记可用。
现在假设您有 ShutDownBean,您希望在调用关闭回调时运行一些代码。
部分注册 bean。

import javax.annotation.PreDestroy;

public final  class ShutDownBean {

    @PreDestroy
    public static void shutDownMethod() {
        System.out.println("Shutting down!");
    }

}

现在你完成了。

如果您有一个桌面应用程序,那么要使用@PreDestroy注释,您需要像这样关闭它:

AbstractApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("application-context.xml");
applicationContext.registerShutdownHook();

注意:
AbstractApplicationContext 具有 registerShutdownHook() 的实现,因此这是您可以使用的最小类。

您还可以对您不控制其实现的类使用 destroy-method 标记。例如,您可以将其添加到 applcation-context.xml 中:

<bean id = "dataSource" 
      class = "org.apache.commons.dbcp.BasicDataSrouce"
      destroy-method = "close">

destroy-method 值可以具有任何可见性,但不需要有任何参数。

希望这有帮助!

There are 3 ways to do that.

@PreDestroy tag

destroy-method in xml

DisposableBean interface as stated above

My favorite is the @PreDestroy method.

To do that u need:

In application-context.xml add the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="shutDownBean" class="spring.ShutDownBean" />
    <context:annotation-config/>
</beans>

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.

import javax.annotation.PreDestroy;

public final  class ShutDownBean {

    @PreDestroy
    public static void shutDownMethod() {
        System.out.println("Shutting down!");
    }

}

Now you are done.


If you have a desktop application then to use the @PreDestroy annotation you need to close it like this:

AbstractApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("application-context.xml");
applicationContext.registerShutdownHook();

note:
AbstractApplicationContext has the implementation of registerShutdownHook() 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 your applcation-context.xml:

<bean id = "dataSource" 
      class = "org.apache.commons.dbcp.BasicDataSrouce"
      destroy-method = "close">

The destroy-method value can have any visibility but needs to have no arguments.

Hope this helps!

暗地喜欢 2024-11-12 18:47:46

您的意思是使用标准 JDK @PreDestroy 注释一个方法?这在 Spring 中很常见,通常比在 XML 中的 bean 声明上使用 destroy-method 属性更好。您所要做的就是包含

<context:annotation-config/> 

在您的配置文件中,Spring 会处理其余的事情。

You mean like annotating a method with the standard JDK @PreDestroy? That's common enough in Spring, and usually better than using a destroy-method attribute on the bean declaration in XML. All you have to do is include

<context:annotation-config/> 

In your configuration file and Spring handles the rest.

流年已逝 2024-11-12 18:47:46

有标准的 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文