Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Spring Bean Life Cycle Explained [With Coding Example]

Updated on 14 November, 2022

22.19K+ views
7 min read

Bean is an object in an application. A bean is created, used, and finally destroyed when its purpose is over. These are the different stages of a spring life cycle. The entire spring bean life cycle is supervised by the Spring IoC (Inversion of Control) container. That is why these beans are called spring beans.

Check out our free courses to get an edge over the competition.

The Life Cycle of a Spring Bean

source

In a spring bean life cycle, first of all, a bean is instantiated. After instantiation, a bean goes through a sequence of steps before being ready to be used. When a bean is no longer required for any function, it is destroyed.

Check out upGrad’s Java Bootcamp

Read: Top 18 Exciting Spring Projects Ideas & Topics For Beginners

Spring bean life cycle can be controlled in the following ways

  • Instantiation by using:
  • InitializingBean callback interface.
  • Custom init() method from the bean configuration file.
  • Aware interfaces for distinct actions.
  • PostConstruct and PreDestroy annotations.
  • Destruction
  • DisposableBean callback interface
  • Custom destroy() method from the bean configuration file.

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)

Instantiation of a bean

The first process in the spring bean life cycle is an instantiation. The creation of a bean rests on JAVA or XML bean configuration file. This can be done in two ways. 

They are:

  • InitializingBean callback interface: Instantiation in this way is done in a method named afterPropertiesSet(). This method is present in org.springframework.beans.factory.InitializingBean interface. In the program below, a class is created which implements this interface. This enables using the afterPropertiesSet()  method of that interface in the created class.

Following is the program depicting this instantiation process

import org.springframework.beans.factory.InitializingBean;

public class Creatingbean implements InitializingBean

{

@Override

public void afterPropertiesSet() throws Exception

{

// Bean is initialized

}

}
  • Custom created method of instantiation in the bean configuration file: In this process, an XML based configuration file is used. Init-method of this file is used to specifically name the instantiation method. This method is used in the class for bean instantiation. The local definition of a single bean is shown below. In this way, we can create a single bean.
beans.xml:

<beans>

<bean id=”creatingbean” class=”com.workinginjava.work.Creatingbean”

init-method=”createInit” ></bean>

</beans>

Check out: Top 7 Exciting Spring Boot Projects & Topics For Beginners

Following is the program depicting this instantiation process by loading beans.xml

package com.workinginjava.work;

public class Creatingbean 

{

public void createInit()

//Custom created init method is used for instantiation of a bean

}

Aware Interfaces: Spring Framework infrastructure provides several aware interfaces. These interfaces inculcate certain distinct behavior to a created bean. 

Some of the important Aware Interfaces include:

  • BeanFactoryAware: setBeanFactory() furnishes access to the bean factory that created the object.
  • BeanNameAware: setBeanName() method under BeanNameAware interface provides the name of the bean. 
  • ApplicationContextAware: setApplicationContext() method under this specific interface provides the ApplicationContext of the bean. 

PostConstruct and PreDestroy annotations: PostConstruct is an annotated method. It is called after bean construction and before requesting an object. PreDestroy is also an annotated method. It is called just before the destruction of a bean.

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

Following program depicts the usage of annotations

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy; 

public class BeanWork

{

   @PostConstruct

    public void createInit() 

    {

        //Initiation of bean(PostConstruct)

    }

   @PreDestroy

    public void createDestroy() 

    {

        //Destruction of bean(PreDestroy)

    }

}

The createInit() and createDestroy() are custom created initiation and destruction methods of a bean. This is done using the XML bean configuration file.

Destruction of a bean

The last process in the spring bean life cycle is the destruction process. It is the process of removing a bean. The removal of a bean rests on JAVA or XML bean configuration file. 

This can be done in two ways

  • DisposableBean callback interface: Disposal is done in a method named destroy(). This method is present in org.springframework.beans.factory.DisposableBean interface. In the program below, a class is created which implements this interface. This enables using the destroy() method of that interface in the created class.

Following is the program depicting this instantiation process

import org.springframework.beans.factory.DisposableBean;

public class Destroyingbean implements DisposableBean

{

@Override

public void destroy() throws Exception

{

// Bean is destroyed

}

}
  • Custom created method of destruction in the bean configuration file: XML based configuration file is used here. The destroy-method of this file is used to specifically name the destruction method. This method is then used in the class for the destruction of the bean. The local definition of a single bean and steps to destroy it is shown below.
Beans.xml:

<beans>

<bean id=”destroyingbean” class=”com.workinginjava.work.Destroyingbean”

destroy-method=”createDestroy” ></bean>

</beans>

Following is the program depicting this destruction process by loading the beans.xml:

package com.workinginjava.work;

public class Destroyingbean 

{

public void createDestroy()

//Custom destruction method is used for the destruction of a bean

}
  • Spring beans are created for a specific purpose. So, every bean goes through a distinct spring life cycle. There are two ways of beginning and ending the spring bean lifecycle. If InitializingBean and DisposableBean interfaces are used, it binds the code to Spring. A better way is to identify the init-method and destroy-method in the bean configuration file.
  • Also Read: Spring Developer Salary in India: For Freshers & Experienced  

Learn Software Development online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Wrapping up

These points about the spring bean life cycle might answer some questions. Still, they raise new ones for you – What are the resources for an aspiring Full Stack developer and the use of spring framework? What is the scope in this field? And, most importantly, how to build a career in this domain?

If you’re interested to learn more about full-stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Frequently Asked Questions (FAQs)

1. What is a spring bean in Java?

A bean is a reusable component or class that encapsulates several objects into one standardized object in the Java programming language. This allows programmers to access this object from several places and facilitates easy maintenance. A Spring bean in Java is an object that is instantiated or created and assembled by the Spring IoC container. Spring beans form the core of the Java application and can be defined either with the help of XML configuration or by using annotation. Beans can also be defined completely in Java class with the help of Java configuration. Beans defined in Spring come with one unique identifier and multiple aliases.

2. What are JavaBeans?

JavaBeans are actually classes in the Java language used to encapsulate multiple objects into a single object class, which is the bean. There are certain conventions that the JavaBeans class should adhere to. These conventions mandate that this class must be serializable. It should come with a public no-arg constructor. All of the properties in JavaBeans must be made private with only getters, and setter methods made public. Serialization is one of the most significant features of Java as it allows Java objects to be saved, i.e., it promotes the persistence of the objects. It also allows an object to travel over a network.

3. How is Spring different from Spring Boot?

Spring is an open-source Java framework that is lightweight and ideal for developing enterprise applications. Dependency injection is the most significant feature of this Spring framework. Spring boot is among the most popularly used programming frameworks meant for backend development. It is developed on top of the traditional spring framework, so it comes with all features of Spring but is significantly easier to use than Spring. Spring boot's architecture is based on microservices and helps develop release-ready applications in a much shorter time. So, while spring boot is effective in developing standalone apps, Spring helps develop more loosely coupled applications.

RELATED PROGRAMS