Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
In the Spring framework, you often need to perform certain actions upon the initialization or destruction of a bean. For such scenarios, Spring provides a way to define init
and destroy
callback methods. These methods are called respectively when the bean is fully constructed and when the bean is destroyed.
Example Bean:
Here's a simple bean that has init()
and destroy()
methods:
public class ExampleBean { public void init() { System.out.println("Bean is going through init."); } public void doSomething() { System.out.println("Bean is in use."); } public void destroy() { System.out.println("Bean will be destroyed now."); } }
XML Configuration:
<bean id="exampleBean" class="com.example.ExampleBean" init-method="init" destroy-method="destroy" />
Here, the init-method
and destroy-method
attributes specify the names of the methods to call on bean initialization and destruction respectively.
For Java-based configurations, you can use the @Bean
annotation with its initMethod
and destroyMethod
attributes.
Java Configuration:
@Configuration public class AppConfig { @Bean(initMethod = "init", destroyMethod = "destroy") public ExampleBean exampleBean() { return new ExampleBean(); } }
Another approach is to use the JSR-250 provided @PostConstruct
and @PreDestroy
annotations.
Example Bean with Annotations:
import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class ExampleBean { @PostConstruct public void init() { System.out.println("Bean is going through init."); } public void doSomething() { System.out.println("Bean is in use."); } @PreDestroy public void destroy() { System.out.println("Bean will be destroyed now."); } }
For this to work, you'll need the javax.annotation
dependency in your project.
Note:
init
method, Spring guarantees that it will be called after the bean has been constructed and all properties have been set.destroy
method, it's important to know that this is called only for beans in the singleton scope when the application context is being closed or shut down. It isn't called for prototype beans since Spring doesn't manage their complete lifecycle.Using init() and destroy() methods in Spring framework:
Description: Spring allows you to define initialization and destruction methods in your beans using init() and destroy(). The init() method is called after bean construction, and destroy() is called before the bean is removed from the container.
Code Example:
public class MyBean { // Initialization method public void init() { // Initialization logic } // Destruction method public void destroy() { // Cleanup logic } }
Lifecycle callbacks in Spring beans:
Description: Spring provides a comprehensive lifecycle for beans, including various callback methods. The init() and destroy() methods are part of this lifecycle, allowing you to hook into different phases of a bean's existence.
Code Example:
public class MyBean implements InitializingBean, DisposableBean { // Initialization callback @Override public void afterPropertiesSet() throws Exception { // Initialization logic } // Destruction callback @Override public void destroy() throws Exception { // Cleanup logic } }
How to implement custom initialization and destruction in Spring:
Description: You can implement custom initialization and destruction methods in your beans by defining methods with the appropriate annotations or by implementing InitializingBean and DisposableBean interfaces.
Code Example:
public class MyBean { // Custom initialization method public void customInit() { // Custom initialization logic } // Custom destruction method public void customDestroy() { // Custom cleanup logic } }
Bean lifecycle hooks with init() and destroy() in Spring:
Description: The init() and destroy() methods provide hooks into the bean lifecycle. They are useful for tasks such as resource allocation during initialization and resource cleanup during destruction.
Code Example:
public class MyBean { // Initialization method public void init() { // Initialization logic, e.g., opening a database connection } // Destruction method public void destroy() { // Cleanup logic, e.g., closing the database connection } }
Initializing and cleaning up resources in Spring beans:
Description: Spring beans often require resource initialization and cleanup. The init() and destroy() methods provide a clean way to handle such tasks, ensuring proper resource management.
Code Example:
public class DatabaseConnectionBean { // Initialization method public void init() { // Open a database connection } // Destruction method public void destroy() { // Close the database connection } }
Configuring bean lifecycle methods with annotations in Spring:
Description:
Spring allows you to use annotations like @PostConstruct
and @PreDestroy
to define initialization and destruction methods directly. This eliminates the need for explicit init() and destroy() method names.
Code Example:
public class MyBean { // Initialization method using @PostConstruct @PostConstruct public void customInit() { // Initialization logic } // Destruction method using @PreDestroy @PreDestroy public void customDestroy() { // Cleanup logic } }
Executing tasks before and after bean creation in Spring:
Description:
Spring provides BeanPostProcessor
interface, allowing you to execute tasks before and after the bean creation process. This is useful for customization and manipulation of beans.
Code Example:
public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // Tasks before bean initialization return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // Tasks after bean initialization return bean; } }
Code Example:
public class ResourceManager { // Initialization method public void init() { // Open resources (e.g., database connection) } // Destruction method public void destroy() { // Close resources } }