StackTips
 4 minutes

How to Customize POJO Initialization and Destruction in Spring Core

By Nilanchala @nilan, On Sep 17, 2023 Spring 2.52K Views

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.

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");
    }
}
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.