Skip to content

Spring Framework Blog

Menu
  • Home
  • Spring
  • Spring Boot
  • About
Menu

Bean extension points

Posted on November 21, 2023June 10, 2024 by Armando Marques

In Spring lifecycle callbacks are manages by BeanPostProcessor implementations and several methods for extension are provided:

Table of Contents

Toggle
  • Bean Lifecycle Extensions
    • JSR-250 Annotations
    • Init and Destroy methods

Bean Lifecycle Extensions

JSR-250 Annotations

Not a part of Spring core, must be included as a separate jar. Annotations @PostConstruct and @PreDestroy can be used to set lifecycle callbacks.

public class BeanLifecycleAnnotations extends BaseBean {
    @PostConstruct
    private void init() {
        System.out.println("BeanLifecycle: PostConstruct");
    }

    @PreDestroy
    private void destroy() {
        System.out.println("BeanLifecycle: PreDestroy");
    }
}

Init and Destroy methods

Spring specific configuration that can be used with XML and @Bean.

   @Bean(initMethod = "init", destroyMethod = "destroy")
    public BeanLifeCycleMethods beanLifeCycleMethods() {
    }

Initialization callbacks

These are not recommended because they couple code to Spring implementation.

InitializingBean interface performs initialization work after the container has set all the necessary properties on the bean.

public class AnotherExampleBean implements InitializingBean {
	@Override
	public void afterPropertiesSet() {
		// do some initialization work
	}
}

DisposableBean interface allows a callback to the bean when the bean container is destroyed.

public class ExampleBean {
	public void destroy() {
		// do some destruction work (like releasing pooled connections)
	}
}

Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:

  1. Methods annotated with @PostConstruct
  2. afterPropertiesSet() as defined by the InitializingBean callback interface
  3. A custom configured init() method

Destroy methods are called in the same order:

  1. Methods annotated with @PreDestroy
  2. destroy() as defined by the DisposableBean callback interface
  3. A custom configured destroy() method

Note: destroy callbacks are not invoked by prototype scope beans because there are not tracked by Application Context, their destruction and resource release bust be done manually.

References

https://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html

https://stackoverflow.com/questions/50681027/do-spring-prototype-beans-need-to-be-destroyed-manually

  • Spring
  • Documentation
  • References
  • Toc
  • Books
  • Certification
  • AOP
  • Config
  • Java
  • Java core
  • JDBC
  • JPA
  • Rest
  • Security
  • Spring
  • Spring Boot
  • Spring Core
  • Spring Data
  • Spring MVC
  • Spring Rest
  • Spring Security
  • Tests
  • Transactions
  • Uncategorized

Recent Posts

  • Spring Annotations
  • Java Tests
  • Java operators
  • Java versions
  • Java Oracle Licenses
  • Configuration properties
  • MockMvc
  • Spring Security III
  • MVC Controller Method Params
  • JPA Methods
  • Transaction propagation and isolation
  • Spring JDBC
  • Spring Boot Auto-Configuration
  • Spring Resource interface
  • JSR 330 Standard Annotations
  • Spring Aware Interfaces
  • Spring Transactions
  • Spring Boot Core
  • MVC Rest
  • Spring Boot JPA
©2025 Spring Framework Blog | Built using WordPress and Responsive Blogily theme by Superb