E2EHIRING Logo
Jobs
Jobs
courses
Courses
mentorship
Mentorship
more
Moredropdown
E2EHIRING Logo
more
Jobs
Jobs
courses
Courses
mentorship
Mentorship
HomeSepratorIconBlogsSepratorIconBean Life CycleSepratorIcon

Bean Life Cycle

Han SoloRahul Kumar
calendar19 Mar 2023
poster

Introduction

In Java, beans are objects that are managed by a framework such as Spring or JavaBeans. The bean life cycle refers to the stages that a bean goes through during its lifetime, from instantiation to destruction. Understanding the bean life cycle is crucial for developing robust and efficient applications. In this blog, we will dive deeper into the bean life cycle and explore its different stages.

Bean Life Cycle Stages

  • Instantiation:

The first stage of the bean life cycle is instantiation. This is where the bean is created, either by calling the constructor or through a factory method. The bean is not yet fully initialized at this stage.

  • Population of Properties:

After the bean is created, the framework sets its properties. The framework sets properties by calling the bean's setter methods, or by using reflection to directly access the bean's fields.

  • Initialization:

After the properties have been set, the bean goes through initialization. This stage is where any initialization code or validation checks are executed. The bean may also implement the InitializingBean interface, which provides a callback method to execute initialization code.

  • In Use:

At this stage, the bean is ready for use by the application. It can be accessed and used by the framework or the application code.

  • Destruction:

The final stage of the bean life cycle is destruction. This is where the bean is removed from the application context, and any cleanup or shutdown tasks are executed. The bean may implement the DisposableBean interface, which provides a callback method to execute cleanup code.

 Bean Scopes

In addition to the different life cycle stages, beans can also have different scopes. The scope of a bean determines how many instances of the bean are created and how they are managed. Some of the commonly used scopes are:

  • Singleton:

The default scope, where only one instance of the bean is created and shared across the application.

  • Prototype:

A new instance of the bean is created each time it is requested.

  • Request:

A new instance of the bean is created for each HTTP request.

  • Session:

A new instance of the bean is created for each HTTP session.

  • Custom:

A custom scope can be defined to create and manage beans in a specific way.

Conclusion

In this blog, we explored the different stages of the bean life cycle in more detail. Understanding the bean life cycle is essential for developing robust and efficient applications. In addition, we also looked at the different bean scopes that can be used to manage the creation and sharing of beans. By understanding the bean life cycle and scope, developers can write more efficient and maintainable applications.

Recent Posts

Accelerate Your Career with e2eInternship Program June 2023: Fast Track your Coding Skills!

Accelerate Your Career with e2eInternship Program June 2023: Fast Track your Coding Skills!

Secure Your Future with e2eHiring: Apply for the Ideal Internship Today

Secure Your Future with e2eHiring: Apply for the Ideal Internship Today

Upskilling: Key to Survival and Career Advancement amid Layoffs

Upskilling: Key to Survival and Career Advancement amid Layoffs

Decoding Dream Job: What makes it so difficult to get a dream job?

Decoding Dream Job: What makes it so difficult to get a dream job?

Improving Training Impact Through Effective Presentations

Improving Training Impact Through Effective Presentations

copycopycopycopy

Han Solo

Recent Posts

Accelerate Your Career with e2eInternship Program June 2023: Fast Track your Coding Skills!

Accelerate Your Career with e2eInternship Program June 2023: Fast Track your Coding Skills!

Secure Your Future with e2eHiring: Apply for the Ideal Internship Today

Secure Your Future with e2eHiring: Apply for the Ideal Internship Today

Upskilling: Key to Survival and Career Advancement amid Layoffs

Upskilling: Key to Survival and Career Advancement amid Layoffs

Decoding Dream Job: What makes it so difficult to get a dream job?

Decoding Dream Job: What makes it so difficult to get a dream job?

Improving Training Impact Through Effective Presentations

Improving Training Impact Through Effective Presentations