Spring

Spring - Bean Life Cycle

Spring - Bean Life Cycle

The existing pattern of a Spring bean is straightforward. When a bean is launched, it could be needed to play out some instatement to get it into a usable state. Also, some cleanup might be required when the bean is presently not needed and is taken out from the compartment. However, arrangements of the exercises happen behind the scene between the hour of bean Instantiation and its obliteration. This part will discuss just two significant bean life cycle callback techniques, which are needed at the time of bean introduction and its destruction. 

To characterize arrangement and teardown for a bean, we essentially pronounce the <bean> with the init method and annihilate strategy boundaries. The init-technique property indicates a strategy that will approach the bean promptly upon launch. Also, destroy method indicates a technique that is called not long before a bean is eliminated from the compartment. 

Initialization callbacks 

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

void afterPropertiesSet() throws Exception; 

In this way, you can execute the above interface, and instatement work should be possible inside the afterPropertiesSet() technique as follows − 

public class DemoBean implements InitializingBean { 
public void afterPropertiesSet() { 
} 
} 
On account of XML-based arrangement metadata, you can utilize the init-method property to determine the name of the strategy that has a void no-contention signature. For instance − 
<bean id = "demoBean" class = "examples.DemoBean" init-method = "init"/> 
Following is the class definition − 
public class DemoBean { 
public void init() { 
} 
} 

Destruction callbacks 

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

void destroy() throws Exception; 

Subsequently, you can execute the above interface and finish work should be possible inside obliterate() strategy as follows − 

public class DemoBean carries out DisposableBean { 
public void annihilate() { 
} 
} 
On account of XML-based setup metadata, you can utilize the destroy-method property to indicate the name of the strategy that has a void no-contention signature. For instance − 
<bean id = "DemoBean" class = "examples.DemoBean" destroy-method = "destroy"/> 
Following is the class definition − 
public class DemoBean { 
public void annihilate() { 
} 
} 

If you utilize Spring's IoC holder in a non-web application climate, for instance, in a rich customer work area climate, you register a closure snare with the JVM. Doing as such guarantees an effortless closure and calls the applicable obliterate techniques on your singleton beans with the goal that all assets are delivered. It is suggested that you don't utilize the InitializingBean or DisposableBean callbacks because XML arrangement gives a lot of adaptability as far as naming your technique.

Default initialization and destroy methods

Suppose you have an excessive number of beans having instatement and additionally annihilate techniques with a similar name. In that case, you don't have to proclaim init- method and destroy-method on every individual bean. All things being equal, the structure gives the adaptability to design such circumstances utilizing default-init- method and default- destroy-method credits on the <beans> component.