Bean Life-Cycle Management
An important part of any IoC container, Spring included, is that beans can be constructed in such a way
that they receive notifications at certain points in their life cycle. This enables your beans to perform
relevant processing at certain points throughout their life. In general, two life-cycle events are
particularly relevant to a bean: post-initialization and pre-destruction.
In the context of Spring, the post-initialization event is raised as soon as Spring finishes setting all
the property values on the bean and finishes any dependency checks that you configured it to perform.
The pre-destruction event is fired just before Spring destroys the bean instance. However, for beans with
prototype scope, the pre-destruction event will not be fired by Spring. The design of Spring is that the
initialization life-cycle callback methods will be called on objects regardless of bean scope, while for
beans with prototype scope, the destruction life-cycle callback methods will not be called. Spring
provides three mechanisms a bean can use to hook into each of these events and perform some
additional processing: interface-based, method-based, and annotation-based mechanisms.
Using the interface-based mechanism, your bean implements an interface specific to the type of
notification it wants to receive, and Spring notifies the bean via a callback method defined in the
interface. For the method-based mechanism, Spring allows you to specify, in your ApplicationContext
configuration, the name of a method to call when the bean is initialized and the name of a method to call
when the bean is destroyed. For the annotation mechanism, you can use JSR-250 annotations to specify
the method that Spring should call after construction or before destruction.
In the case of both events, the mechanisms achieve exactly the same goal. The interface mechanism
is used extensively throughout Spring so that you don't have to remember to specify the initialization or
destruction each time you use one of Spring's components. However, in your own beans, you may be
better served using the method-based or annotation mechanism because your beans do not need to
implement any Spring-specific interfaces. Although we stated that portability often isn't as important a
requirement as many books lead you to believe, this does not mean you should sacrifice portability
when a perfectly good alternative exists. That said, if you are coupling your application to Spring in other
ways, using the interface method allows you to specify the callback once and then forget about it. If you
are defining a lot of beans of the same type that need to take advantage of the life-cycle notifications,
then using the interface mechanism can avoid the need for specifying the life-cycle callback methods for
every bean in the XML configuration file. Using JSR-250 annotations is also another viable option, since
it's a standard defined by the JCP and you are also not coupled to Spring's specific annotations. Just
make sure that the IoC container you are running your application on supports the JSR-250 standard.
Overall, the choice of which mechanism you use for receiving life-cycle notifications depends on
your application requirements. If you are concerned about portability or you are just defining one or two
beans of a particular type that need the callbacks, then use the method-based mechanism. If you use
annotation-type configuration and certain that you are using an IoC container that supports JSR-250,
then use the annotation mechanism. If you are not too concerned about portability or you are defining
many beans of the same type that need the life-cycle notifications, then using the interface-based
mechanism is the best way to ensure that your beans always receive the notifications they are expecting.
If you plan to use a bean across many different Spring projects, then you almost certainly want the
functionality of that bean to be as self-contained as possible, so you should definitely use the interface-
based mechanism.
Figure 5-1 shows a high-level overview of how Spring manages the life cycle of the beans within its
container.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home