How does Lifecycle interface work in Spring? What are “top-level singleton beans”?

前端 未结 5 1657
無奈伤痛
無奈伤痛 2021-02-07 05:13

It is said in Spring javadoc, that \"Note that the Lifecycle interface is only supported on top-level singleton beans.\" Here URL

My LifecycleBeanTest.xml d

5条回答
  •  無奈伤痛
    2021-02-07 05:59

    You should use SmartLifecycle instead of Lifecycle. Only the former is working as you expected Lifecycle to work. Make sure you return true in your isRunning() implementation.

    I have used SmartLifecycle for asynchronous jobs for which it sounds like designed for. I suppose it will work for you but at the same time you may have a look at ApplicationListener and events like ContextStoppedEvent.

提交回复
热议问题