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:
- Create the bean instance either by a constructor injection or by a factory method.
- Set the values and bean references to the bean properties.
- Call the bean initialization callback methods.
- The bean is ready to be used.
- 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.
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"); } }