Spring Bean lifecycle events – method based

Last Updated: February 18, 2018

In this method we can define a method as initialization method so that Spring can call that method once setting all properies and checking the dependencies

I am creating init() method inside the Car bean

package com.kayak;

public class Car {

    private String name;

    public Car() {

        System.out.println("Constructor method called");

    public void init() {
        System.out.println("Init method called");

    public String getName() {
        return name;


Now you have to add init-method attaribute to the bean tag and set the value as name of the method init


public static void main(String[] args) {
   ApplicationContext ctx = new  ClassPathXmlApplicationContext("bean.xml");
   Car car=(Car)ctx.getBean("car");

How do you fire the event when your bean is destroyed

You can simply add mthod to your Bean and update your bean.xml


Now you can see the following output:

Feb 16, 2018 4:24:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [bean.xml]
Constructor method called
Init method called
BUILD SUCCESSFUL (total time: 2 seconds)