Spring POJOs are initialized and managed by Spring IoC Container. Sometimes, you may need perform some initialization code before a POJO is used. A typical example is to open a database or network connection. Similarly you may want to perform the corresponding destruction tasks at the end of their life cycle. Therefore, sometimes it is necessary to customize bean initialization and destruction in the Spring IoC container.

The POJO life cycle tasks can be created using callback methods. A typical life cycle of a bean can be explained as follows:

  1. Create the bean instance either by a constructor injection or by a factory method.
  2. Set the values and bean references to the bean properties.
  3. Call the bean initialization callback methods.
  4. The bean is ready to be used.
  5. When the container is shut down, call the bean destruction callback methods are called.

Spring initialization and destruction callback methods can be configured by setting the values for init-method and destroy-method attributes in the bean declaration. You need to specify the callback method names as follows.

<bean id="pizza" class="com.javatechig.spring.bean.Pizza"
	init-method="onStart" destroy-method="onDestroy">
	<property name="name" value="Italian Pizza" />
	<property name="price" value="5.0" />

Notice that in the above code snippet, we have specified onStart and onDestroy callback methods to control POJO initialization and destruction. Here is how, you can configure the callback methods to your POJO class.

public class Pizza {
	private String name;
	private double price;
	public Pizza() { }

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public double getPrice() {
		return price;

	public void setPrice(double price) {
		this.price = price;

	public void onStart() throws IOException {
		System.out.println("Inside onStart");

	public void onDestroy() throws IOException {
		System.out.println("Inside onDestroy");
This article is about: Code Sample, java,


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.


Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.