Spring init and destroy methods

Beans in spring has a lifecycle, we have methods in spring to do something before and after creating a bean. lets see how its done,

Address.java


package com.sourceforopensource.pojos;

public class Address {
private String street;
private String postcode;

public Address(String street, String postcode) {
this.street = street;
this.postcode = postcode;
}

public void init() {
System.out.println("Bean created: " + this);
}

@Override
public String toString() {
return "Address [street=" + street + ", postcode=" + postcode + "]";
}
}

Person.java


package com.sourceforopensource.pojos;

public class Person {

private int id;
private String name;
private int taxId;
private Address address;

public Person() {

}
public Person(int id, String name) {
this.id = id;
this.name = name;
}

public void onCreate() {
System.out.println("Person created: " + this);
}

public void onDestroy() {
System.out.println("Person destroyed.");
}

public void speak() {
System.out.println("Hello! I'm a person.");
}

public void setTaxId(int taxId) {
this.taxId = taxId;
}

public void setAddress(Address address) {
this.address = address;
}

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", taxId=" + taxId
+ ", address=" + address + "]";
}
}

beans.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-init-method="init" default-destroy-method="destroy">

//the above default init method and destroy method will set contract for bean.
<bean id="person" class="com.sourceforopensource.pojos.Person"
scope="singleton" init-method="onCreate" destroy-method="onDestroy">

//we have init and destroy methods above those methods will run before this person bean instantiation and before person bean destroying.
<constructor-arg value="777" name="id" type="int">
</constructor-arg>
<constructor-arg value="Mary" name="name" type="String"></constructor-arg>
<property value="321" name="taxId">
</property>
<property name="address" ref="address"></property>
</bean>

<bean id="address" class="com.sourceforopensource.pojos.Address">
<constructor-arg name="street" value="1 Church Lane"></constructor-arg>
<constructor-arg name="postcode" value="9876"></constructor-arg>
</bean>

</beans>

output:


Bean created: Address [street=1 Church Lane, postcode=9876]
Person created: Person [id=777, name=Mary, taxId=321, address=Address [street=1 Church Lane, postcode=9876]]
Person [id=777, name=Mary, taxId=666, address=Address [street=1 Church Lane, postcode=9876]]

Person destroyed.

Note: you can see destroy method running only in case of singleton scope only.

Balakrishna Pendyala

Author: Balakrishna Pendyala

Balakrishna is a Software Engineer working in Media Streaming Domain, and also worked on Banking and Health Care domains. His areas of interest include Object Oriented Design, SOLID Design principles, Restful Web Services and Open Source softwares including Spring, Spring Boot, and Hibernate.

If you found an error, highlight it and press Shift + Enter or click here to inform us.

Leave a Reply

Your email address will not be published. Required fields are marked *