1. The containers instantiate every bean,populate their properties,and call the init method,which makes the bean ready to be used.During the shutdown of the application,the destroy method of the bean is invoked.
  2. The init and destroy methods are used to perform initialization and cleanup operations.
  3. Below diagram shows the Spring Bean life cycle.
    BeanLifeCycle

Example:

  1. Create the class which implements ApplicationContextAware,InitializingBean,DisposableBean interfaces
    package com.java2learn.core;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    public class SpringBeanDemo implements ApplicationContextAware,InitializingBean,DisposableBean {
    
    	private String message;
    	
    	
    	
    	public SpringBeanDemo() {
    		System.out.println("SpringBeanDemo is instantiating.");
    	}
    
    	public void setMessage(String message) {
    		this.message = message;
    		System.out.println("Inside setMessage method");
    	}
    	
    	public String getMessage() {
    		return message;
    	}
    
    	@Override
    	public void destroy() throws Exception {
    		System.out.println("Inside destroy method");
    		
    	}
    
    	@Override
    	public void afterPropertiesSet() throws Exception {
    		System.out.println("Inside afterPropertiesSet method");
    		
    	}
    
    	@Override
    	public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
    		System.out.println("Inside setApplicationContext method:");
    		
    	}
    }
    
  2. Create the Spring configuration file
    <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-3.0.xsd">
    
    	<bean id="springBean" class="com.java2learn.core.SpringBeanDemo">
    		<property name="message" value="Hello Spring Bean!!"></property>
    	</bean>
    
    </beans>
    
  3. Create the client and run it.
    package com.java2learn.client;
    
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    import com.java2learn.core.SpringBeanDemo;
    
    public class Client {
    
    	public static void main(String[] args) {
    
    		ConfigurableApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:Spring-Core.xml");
    		SpringBeanDemo bean = (SpringBeanDemo) applicationContext.getBean("springBean");
    		System.out.println("message ::" + bean.getMessage());
    		// shutdown the context
    		applicationContext.close();
    
    	}
    
    
  4. Final project structure.
    FinalProjectStructure

Output:

SpringBeanDemo is instantiating.
Inside setMessage method
Inside setApplicationContext method:
Inside afterPropertiesSet method
message ::Hello Spring Bean!!
Inside destroy method

Download Project: SpringCore