Table of Contents
This is 13 of 16 parts of tutorial series
Tutorial Content:
- Introduction to spring framework
- Spring interview questions
- Dependency injection(ioc) in spring
- Spring hello world example in eclipse
- Spring java based configuaration
- Dependency injection via setter method in spring
- Dependency injection via constructor in spring
- Spring Bean scopes with examples
- Initializing collections in spring
- Beans Autowiring in spring
- Inheritance in Spring
- Spring ApplicationContext
- Spring lifetime callbacks
- BeanPostProcessors in Spring
- Annotation based Configuration in spring
- Spring AOP tutorial
The Spring Framework provides several callback interfaces to change the behavior of your bean in the container; they include InitializingBean
and DisposableBean
.
The life cycle of a Spring bean is easy to understand. When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required.
Initialization callbacks:
Implementing 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 exactly one method:
1 2 3 |
void afterPropertiesSet() throws Exception; |
InitializingBean
interface can be avoided and is actually discouraged since it unnecessarily couples the code to Spring.You have to use afterPropertiesSet(),
you can not change name of method.There is alternative for this i.e. XML-based configuration metadata.This is done using the 'init-method'
attribute of tag.It provides flexibility of changing method name.
1 2 3 4 5 6 7 8 9 10 |
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country" init-method="init"/> public class Country{ public void init() { // do some initialization work } } |
…is exactly the same as…
1 2 3 4 5 6 7 8 9 10 |
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country"/> public class Country implements InitializingBean { public void afterPropertiesSet() { // do some initialization work } } |
… but does not couple the code to Spring.
Destruction callbacks:
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:
1 2 3 |
void destroy() throws Exception; |
DisposableBean
interface can be avoided and is actually discouraged since it unnecessarily couples the code to Spring.You have to use destroy(),
you can not change name of method.There is alternative for this i.e. XML-based configuration metadata.This is done using the 'destroy-method'
attribute of tag.It provides flexibility of changing method name.
1 2 3 4 5 6 7 8 9 10 |
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country" init-method="destroy"/> public class Country{ public void destroy() { // do some destruction work(like releasing pooled connections) } } |
1 2 3 4 5 6 7 8 9 10 |
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country"/> public class Country implements DisposableBean{ public void destroy() { // do some destruction work(like releasing pooled connections) } } |
Spring lifetime callbacks Example:
For configuring spring in your eclipse ide please refer hello world example
1.Country.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package org.arpit.javapostsforlearning; public class Country { String countryName ; public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public void init() { System.out.println("In init block of country"); } public void destroy() { System.out.println("In destroy block of country"); } } |
2.ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="country" class="org.arpit.javapostsforlearning.Country" init-method="init" destroy-method="destroy"> <property name="countryName" value="India"/> </bean> </beans> |
3.LifetimeCallbacksMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.javapostsforlearning; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class LifetimeCallbacksMain{ public static void main(String[] args) { AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("country"); System.out.println("Country Name: "+countryObj.getCountryName()); appContext.registerShutdownHook(); } } |
4.Run it
1 2 3 4 5 |
In init block of country Country Name: India In destroy block of country |
Default Initialization and destroy method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-init-method="init" default-destroy-method="destroy" > <bean id="country" class="org.arpit.javapostsforlearning.Country"> <property name="countryName" value="India"/> </bean> </beans> |
That’s all about Spring lifetime callbacks.