Spring - Bean Life Cycle


The life cycle of a Spring bean is straightforward. At the point when a bean is instantiated, it might be required to play out some introduction to get it into a usable state. Correspondingly, when the bean is never again required and is expelled from the compartment, some cleanup might be required.

Though, there are arrangements of the exercises that occur behind the scene between the season of bean Instantiation and its demolition, this part will talk about just two critical bean life cycle callback strategies, which are required at the season of bean introduction and its destruction.

To characterize setup and teardown for a bean, we basically pronounce the <bean> with initmethod or potentially destroy-method parameters. The init-strategy characteristic determines a technique that will be approached the bean promptly upon instantiation. So also, destroymethod indicates a strategy that is called just before a bean is expelled from the container.

Initialization callbacks

The org.springframework.beans.factory.InitializingBean interface determines a solitary technique −

void afterPropertiesSet() tosses Exception; 

Thus, you can essentially execute the above interface and introduction work should be possible inside afterPropertiesSet() technique as pursues −

public class ExampleBean implements InitializingBean { 

public void afterPropertiesSet() { 

//do some introduction work 

} 

}

In the instance of XML-based arrangement metadata, you can utilize the init-method ascribe to determine the name of the technique that has a void no-contention signature. For instance −

<bean id = "exampleBean" class = "examples.ExampleBean" init-technique = "init"/> 

Following is the class definition −

public class ExampleBean { 

public void init() { 

//do some instatement work 

} 

}

Destruction callbacks

The org.springframework.beans.factory.DisposableBean interface indicates a solitary technique −

void obliterate() tosses Exception; 

Thus, you can essentially execute the above interface and finish work should be possible inside pulverize() strategy as pursues −

public class ExampleBean implements DisposableBean { 

public void destroy() { 

//do some devastation work 

} 

}

In the instance of XML-based setup metadata, you can utilize the destroy-method credit to determine the name of the strategy that has a void no-contention signature. For instance −

<bean id = "exampleBean" class = "examples.ExampleBean" decimate technique = "wreck"/> 

>

Following is the class definition −

public class ExampleBean { 

public void destroy() { 

//do some devastation work 

} 

}

If you are utilizing Spring's IoC holder in a non-web application condition; for instance, in a rich customer work area condition, you register a shutdown snare with the JVM. Doing as such guarantees a smooth shutdown and calls the significant obliterate strategies on your singleton beans with the goal that all assets are released.

It is prescribed that you don't utilize the InitializingBean or DisposableBean callbacks, in light of the fact that XML design gives much adaptability regarding naming your method.

Example

Let us have a working Eclipse IDE set up and find a way to make a Spring application −

Steps Description
1 Create a venture with a name SpringExample and make a bundle com.tutorialspoint under the src organizer in the made project.
2 Add required Spring libraries utilizing Add External JARs choice as clarified in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld and MainApp under the com.tutorialspoint package.
4 Create Beans setup record Beans.xml under the src folder.
5 The last advance is to make the substance of all the Java records and Bean Configuration document and run the application as clarified below.

Here is the substance of HelloWorld.java document −

package com.tutorialspoint; 

public class HelloWorld { 

private String message; 

public void setMessage(String message){ 

this.message = message; 

} 

public void getMessage(){ 

System.out.println("Your Message : " + message); 

} 

public void init(){ 

System.out.println("Bean is experiencing init."); 

} 

public void destroy() { 

System.out.println("Bean will obliterate now."); 

} 

}

Following is the substance of the MainApp.java record. Here you have to enlist a shutdown snare registerShutdownHook() technique that is announced on the AbstractApplicationContext class. This will guarantee a smooth shutdown and call the pertinent pulverize methods.

package com.tutorialspoint;
import org.springframework.context.support.AbstractApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp { 

public static void main(String[] args) { 

AbstractApplicationContext setting = new ClassPathXmlApplicationContext("Beans.xml"); 

HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); 

obj.getMessage(); 

context.registerShutdownHook(); 

} 

}

Following is the arrangement record Beans.xml required for init and decimate strategies −

<?xml adaptation = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/outline/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/outline/beans 

http://www.springframework.org/outline/beans/spring-beans-3.0.xsd"> 

<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" init-method = "init" 

destroy-method = "destroy"> 

<property name = "message" value = "Hello World!"/> 

</bean> 

</beans>

Once you are finished making the source and bean design records, let us run the application. On the off chance that all is well with your application, it will print the accompanying message −

Bean is experiencing init. 

Your Message : Hello World! 

Bean will devastate now.

Default introduction and pulverize methods

If you have such a large number of beans having instatement as well as obliterate strategies with a similar name, you don't have to pronounce init-method and destroy-method on every individual bean. Rather, the structure gives the adaptability to design such circumstance utilizing default-init-method and default-decimate method characteristics on the <beans> component as pursues −

<beans xmlns = "http://www.springframework.org/diagram/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/diagram/beans 

http://www.springframework.org/diagram/beans/spring-beans-3.0.xsd" 

default-init-method = "init" 

default-crush method = "destroy"> 

<bean id = "..." class = "..."> 

<!- - partners and design for this bean go here - > 

</bean> 

</beans>






© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3