The Spring bean life cycle and its phases

In a Spring application, the term life cycle applies to any class of application--Standalone Java, Spring Boot application, or Integration/System Test. Also, life cycle applies to all three dependency injection styles--XML, Annotations, and Java configuration. You define the configuration for beans as per business goals. But Spring creates these beans and manages the life cycle of the Spring beans. Spring loads the bean configurations either in Java or XML through ApplicationContext. After loading these beans, the Spring container handles the creation and instantiation of these beans as per your configuration. Let's divide the Spring application life cycle into three phases as follows:

  • The initialization phase
  • The Use phase
  • The destruction phase

Please refer to the following diagram:

As you can see in the preceding diagram, each Spring bean goes through these three phases in the complete life cycle. Each phase has some set of operations to be performed for each Spring bean (depending on the configuration). Spring fits in to manage your application life cycle. It plays an important role in all three phases.

Now let's take a moment to see how Spring works in the first, initialization phase.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset