ApplicationListener< ContextClosedEvent>的注释版本;和类似的接口 [英] Annotation version of ApplicationListener<ContextClosedEvent> and similar interfaces

查看:494
本文介绍了ApplicationListener< ContextClosedEvent>的注释版本;和类似的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以使用以下代码基于关闭(或刷新/启动)Web服务的时间来执行一些事件。

You can use code like the following to do some events based on when your web service is shut down (or refreshed/started).

public class APIService implements ApplicationListener<ContextClosedEvent>
{
    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        //Do shutdown work.
    }
}

有人告诉我有一种注释驱动的方法这样做不需要您实现此接口。具体来说,您应该能够使用自己喜欢的任何名称定义函数并对其进行注释,以使其在服务启动或关闭时执行。

I was told that there is an annotation driven way to do this that does not require you to implement this interface. Specifically, you should be able to define a function with any name you like and annotate it so that it executes on service start-up or shut-down.

虽然在我的春季书或通过google中找不到对此的引用。有人可以通过单独的注释提供如何执行上述代码的示例吗?

I'm having trouble finding references to this in my spring book or via google though. Can someone provide an example of how to do the above code via annotations alone?

推荐答案

至于 Spring 4+文档很担心,没有您所描述的功能。

As far as the Spring 4+ documentation is concerned, there is no such feature exactly as you describe it.

您可以使用

@Component // defaults to eager initialization
class Setup {
    @Autowired
    private ApplicationContext context;

    @PostConstruct
    public void anyNameYouWant() {
        System.out.println("starting");
    }

    @PreDestroy
    public void hereToo() {
        System.out.println("closing");
    }
}

但是您将无法访问 ContextClosedEvent ApplicationContextEvent s的全部范围。

But you won't have access to the ContextClosedEvent, nor the full range of ApplicationContextEvents.

这篇关于ApplicationListener&lt; ContextClosedEvent&gt;的注释版本;和类似的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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