Life Cycle of Bean

The life cycle of Bean is easy to understand When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required.

you can use init-method and destroy-method as an attribute in bean configuration file for the bean to perform certain actions upon initialization and destruction.

Initialization – org.springframework.beans.factory.InitializingBean interface  contains one single method “void afterPropertiesSet() throws Exception;”

Destruction – org.springframework.beans.factory.DisposableBean interface  contains one single method “void destroy() throws Exception;”

Lets Implement

Project structure be like below image

1

add the dependency in pom.xml file –

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>4.3.4.RELEASE</version>
</dependency>

bean class “Greetings.java

package com.spring.core;
public class Greetings {
    private String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public void init() {
        System.out.println("Init");
    }
    public void destroy() {
        System.out.println("Destroy");
    }
}

beans.xml file is in the classpath

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- use any one either type 1 or type 2 -->
<!-- type 1 -->     
<beans>
    <bean id="greet" class="com.spring.core.Greetings" init-method="init" destroy-method="destroy">
        <property name="message" value="Hello One"></property>
    </bean>
</beans>

<!-- type 2 -->
<beans default-init-method="init" default-destroy-method="destroy">
    <bean id="greet" class="com.spring.core.Greetings">
        <property name="message" value="Hello One"></property>
    </bean>
</beans>

Main class

package com.spring.core;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application { 
    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Greetings greet = (Greetings) context.getBean("greet"); 
        System.out.println(greet.getMessage());
        context.registerShutdownHook();
    }
}

Output –

Dec 28, 2016 12:29:18 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Wed Dec 28 12:29:18 IST 2016]; root of context hierarchy
Dec 28, 2016 12:29:18 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Init
Hello One
Dec 28, 2016 12:29:18 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Wed Dec 28 12:29:18 IST 2016]; root of context hierarchy
Destroy

For more details – watch video

Thanks for reading 🙂

Please Subscribe our you tube channel Almighty Java

Leave a comment