【问题标题】:How to tie the Lifecycle for a Spring Bean to the webapps' lifecycle?如何将 Spring Bean 的生命周期与 webapps 的生命周期联系起来?
【发布时间】:2009-11-19 10:13:38
【问题描述】:

我想创建一个具有start()stop() 方法的bean。 当 webapp 的上下文处于活动状态时,start() 在 Spring 的运行时启动期间被调用。当 webapp 被取消部署或停止时,stop() 方法被调用。

这是正确的吗:我用@PostConstruct 注释我的start() 方法,用@PreDestroy 注释stop() 方法?

通常在 servlet 世界中,我编写一个 ServletContextListener。 我可以从 ServletContextListener 访问 ApplicationContext 吗?

【问题讨论】:

    标签: java spring tomcat jetty


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      您可以按照您的描述注释您的 start()stop() 方法,或者您可以告诉 Spring 显式调用它们,例如

      <bean class="MyClass" init-method="start" destroy-method="stop"/>
      

      至于ServletContextListener,它无法轻松访问 Spring 上下文。最好使用 Spring 自己的生命周期来进行 bean 初始化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-08
        • 2010-12-23
        • 2013-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多