Spring Bean Life Cycle

Spread the love

The objects that form the backbone of your application and which are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

The goal of bean creation in spring is to facilitate loose coupling.

Important points related to bean creation:

1)Spring beans are defined using the Annotations or XML Bean Configurations.

2)The job of creating the beans is of the spring container. It creates the beans and loads into the Spring IOC Container.

<!-- A simple bean definition -->
   <bean id = "..." class = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <bean name = "..." class = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

3)If you see the above example of the bean definition, a bean is defined using an id or an name.

The only difference between an id and a name is that a name can contain multiple aliases separated by a comma, semicolon or whitespace, whereas an id must be a single value

4)The default scope of a Spring bean is singleton. That is only one copy of that bean is available in the container.

5)If there are multiple beans with same name or id for the same class, then the bean defined at the last in the xml overrides the other beans defined before for that id or name.

Using Spring @PostConstruct and @PreDestroy annotations:

Now after the bean is constructed, there may be a need to do certain actions post bean creation.

That is done with the help of @PostConstruct annotation. Consider the below example.

@Component
class Employee {
    @PostConstruct
    public void postEmployeeCreation() {
        System.out.println("Spring Bean Post 
                            Construct Annotation Method");
    }

}

Similarly before destroying the bean, certain steps may need to be done like releasing file handles or database connections etc.

That is done by Using @PreDestroy annotation with a method.

@Component
class Employee {
    @PreDestroy
    public void preDestroy() {
        System.out.println("Spring Bean Pre Destroy 
                            Annotation Method");
    }
}

Init and destroy method:

Init – It is called during initialization of the bean.

Destroy – It is called when the bean is destroyed.

@Component
public class Employee {

public void init() {
        System.out.println("Spring @Bean Initialization Method  
                            Call");
}

public void destroy() {
        System.out.println("Spring @Bean Destroy Method");
    }

}

If you see the above Employee class, we have 2 methods defined there but without any annotations. Then how those methods are registered to run on initialization and during destroying of bean?

@Configuration
public class EmployeeConfig {
@Bean(initMethod = "init", destroyMethod = "destroy")
    public Employee getEmpInstance() {
        return new Employee();
    }
}

It is registered as above using @Bean annotation. It has 2 properties.

initMethod = “init”, destroyMethod = “destroy”

Any method which is annotated with @bean annotation returns a bean of that class which is in the return type. In above case, it is the Employee class object.

@Bean annotation is used and works only when the class is annotated with @Configuration annotation. Kindly see above.

So when we run a application, first of all, the spring container gets started. After that, the container creates the instance of a bean as per the request and then dependencies are injected. And finally, the bean is destroyed when the spring container is closed.

Thanks for reading!!!!