Spring - Bean lifecycle using annotations

Spring - Bean lifecycle using annotations

We have already discussed the bean lifecycle using an XML-based configuration. In the previous case, we have declared the init-method and destroy-method attributes in the bean-configuration file. Here, we are going to use annotations for the init-method and destroy-method.

The lifecycle of beans is created and managed by the Spring container and Spring bean, respectively. The bean lifecycle consists of two methods: post-initialization and pre-destruction

Following are the annotations applied to declare the methods:

@PostConstruct – When we use @PostConstruct annotation to annotate a method in the bean, it executes after the initialization of the Spring bean. In other words, it is used to define a method as an initialization method that executes after the dependency injection.

@PreDestroy – When we use @PreDestroy annotation to annotate a method in the bean, it will execute when the bean instances are getting removed from the Spring container. In other words, it is used to define a method as a destruction method that performs a task before the bean destruction.

For scope =” prototype,” Spring container does not call the @PreDestroy method. The Spring container does not completely manage the lifecycle of a prototype bean. The Spring container does not completely manage the lifecycle of a prototype bean.

To use the above annotations, we have to add the following dependency into pom.xml:


javax.annotation
javax.annotation-api
1.3
 

Example of bean lifecycle using annotations

Here, we are going to create an example using the @PostConstruct and @PreDestroy annotations for initialization and destruction callback methods, respectively. The Airtel_Sim class implements the Sim interface and its unimplemented method.

Sim.java

public interface Sim {
public String calling();
} 

Airtel_Sim.java

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;
@Component
public class Airtel_Sim implements Sim{
private Sim newsim;
public String calling() {
return "Calling via Airtel_Sim" ; 
}
// init method
@PostConstruct
public void doStartUp() {
System.out.println( "Inside the init method : doStartUp()" );
} 
// destroy method
@PreDestroy
public void doShutDown() {
System.out.println( "Inside the destroy method : doShutDown()" );
}
} 

In the above code, we have defined the @PostConstruct and @PreDestroy annotations just above the doStartUp() and doShutDown() method, respectively.

ApplicationContext.xml

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 



App.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext appcontext = new ClassPathXmlApplicationContext("applicationContext.xml");
Sim sim = appcontext.getBean("airtel_Sim", Sim.class);
System.out.println(sim.Calling()); 
appcontext.close();
}
} 

Output

Example of bean lifecycle using annotations



ADVERTISEMENT
ADVERTISEMENT