老外的blog:http://howtodoinjava.com/
Spring Bean Life Cycle Tutorial
call back methods
- Post initialization call back methods
- Pre destruction call back methods
4 ways for controlling life cycle events
- InitializingBean and DisposableBean callback interfaces
- Other Aware interfaces for specific behavior
- custom init() and destroy() methods in bean configuration file
- @PostConstruct and @PreDestroy annotations
Spring Bean Life Cycle
Lets learn about them one by one.
InitializingBean and DisposableBean callback interfaces
The org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. The InitializingBean interface specifies a single method:
void afterPropertiesSet() throws Exception;
This is not a preferrable way to initialize the bean because it tightly couple your bean class with spring container. A better approach is to use “init-method” attribute in bean definition in applicationContext.xml file.
Similarly, implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed. The DisposableBean interface specifies a single method:
void destroy() throws Exception;
A sample bean implementing above interfaces would look like this:
Other Aware interfaces for specific behavior
Spring offers a range of Aware interfaces that allow beans to indicate to the container that they require a certain infrastructure dependency. Each interface will require you to implement a method to inject the dependency in bean.
These interfaces can be summarized as :
AWARE INTERFACE | METHOD TO OVERRIDE | PURPOSE |
ApplicationContextAware | void setApplicationContext(ApplicationContext applicationContext) throws BeansException; | Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in. |
ApplicationEventPublisherAware | void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher); | Set the ApplicationEventPublisher that this object runs in. |
BeanClassLoaderAware | void setBeanClassLoader(ClassLoader classLoader); | Callback that supplies the bean class loader to a bean instance. |
BeanFactoryAware | void setBeanFactory(BeanFactory beanFactory) throws BeansException; | Callback that supplies the owning factory to a bean instance. |
BeanNameAware | void setBeanName(String name); | Set the name of the bean in the bean factory that created this bean. |
BootstrapContextAware | void setBootstrapContext(BootstrapContext bootstrapContext); | Set the BootstrapContext that this object runs in. |
LoadTimeWeaverAware | void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver); | Set the LoadTimeWeaver of this object’s containing ApplicationContext. |
MessageSourceAware | void setMessageSource(MessageSource messageSource); | Set the MessageSource that this object runs in. |
NotificationPublisherAware | void setNotificationPublisher(NotificationPublisher notificationPublisher); | Set the NotificationPublisher instance for the current managed resource instance. |
PortletConfigAware | void setPortletConfig(PortletConfig portletConfig); | Set the PortletConfig this object runs in. |
PortletContextAware | void setPortletContext(PortletContext portletContext); | Set the PortletContext that this object runs in. |
ResourceLoaderAware | void setResourceLoader(ResourceLoader resourceLoader); | Set the ResourceLoader that this object runs in. |
ServletConfigAware | void setServletConfig(ServletConfig servletConfig); | Set the ServletConfig that this object runs in. |
ServletContextAware | void setServletContext(ServletContext servletContext); | Set the ServletContext that this object runs in. |
A sample implementation will look like this:
Custom init() and destroy() methods in bean configuration file
The default init and destroy methods in bean configuration file can be defined in two ways:
- Bean local definition applicable to a single bean
- Global definition applicable to all beans defined in beans context
Local definition is given as below.
Where as global definition is given as below. These methods will be invoked for all bean definitions given under tag. They are useful when you have a pattern of defining common method names such as init() and destroy() for all your beans consistently. This feature helps you in not mentioning the init and destroy method names for all beans independently.
A sample implementation for this type of life cycle will be:
@PostConstruct and @PreDestroy annotations
Spring 2.5 onwards, you can use annotations also for specifying life cycle methods using @PostConstruct and @PreDestroy annotations.
- @PostConstruct annotated method will be invoked after the bean has been constructed using default constructor and just before it’s instance is returned to requesting object.
- @PreDestroy annotated method is called just before the bean is about be destroyed inside bean container.
A sample implemetation will look like this:
So this is all about life cycle management of beans inside spring container. I hope that it has added some more knowledge in your kitty.
Happy Learning !!