I have already found a previous SF question regarding this issue and I believe setting the init-method
and the destroy-method
properties will be sufficient. But I wanted to hopefully ask the question a di开发者_如何学JAVAfferent way to further my understanding.
If I understand things correctly, a class that implements org.springframework.context.Lifecycle
will behave different in a web app context (namely org.springframework.web.context.support.XmlWebApplicationContext
) than in other application contexts? It will do this because the start()
of the XmlWebApplicationContext
(which will start the contained Lifecycle beans) will have been performed before the context configuration files are loaded.
Is this correct?
The Lifecycle
interface should be implemented by beans that want to participate in the container's lifecycle. This is primarily intended to be implemented by the containers themselves (see docs here), although beans inside those containers can implement it also if they choose, and the start/stop signals will be propagated to them.
The start()
and stop()
methods are essentially notifications that the container has just started start, or is about to stop.
I'm struggling to find a good use case for this, for application components. Business objects should only be concerned with their own lifecycle, rather than with the container's lifecycle. One good reason why is when you use non-singleton scopes (e.g. request-scope) where the bean's lifecycle is independent of the container's.
I don't think so. The start()
method is called by doStart()
and both are in AbstractApplicationContext
, which is the superclass of all application contexts. So there should be no difference.
精彩评论