Spring boot with RefreshScope PostConstruct PreDestroy
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Spring Boot, @PostConstruct and @PreDestroy are lifecycle callbacks that run after a bean is initialized and before it is destroyed. @RefreshScope (from Spring Cloud) allows beans to be recreated at runtime when configuration changes, without restarting the application. When these annotations are combined, @PostConstruct and @PreDestroy run every time the bean is refreshed, not just on application startup and shutdown. Understanding this interaction is critical for managing resources like database connections, caches, and scheduled tasks in dynamically-refreshable beans.
@PostConstruct and @PreDestroy Basics
@PostConstruct runs once after the constructor and all @Autowired fields are set. @PreDestroy runs when the application context shuts down. Both are part of the Jakarta (formerly javax) annotations specification.
@RefreshScope
@RefreshScope marks a bean for dynamic recreation when a refresh event occurs. This is typically triggered by calling the /actuator/refresh endpoint or through Spring Cloud Bus:
When you update api.base-url in your config server and hit /actuator/refresh, Spring destroys the old ApiClient bean and creates a new one with the updated @Value fields.
@RefreshScope with @PostConstruct and @PreDestroy
This is where the behavior becomes important. When a @RefreshScope bean is refreshed, the full lifecycle runs:
Refresh sequence:
/actuator/refreshis called@PreDestroy shutdown()runs on the old bean instance- The old bean is destroyed
- A new bean is created with updated
@Valuefields @PostConstruct init()runs on the new bean instance
Triggering a Refresh
Enable the refresh endpoint in application.yml:
Dependencies required in pom.xml:
Handling Expensive Initialization
Since @PostConstruct runs on every refresh, avoid expensive operations that do not need to repeat:
The scheduler is properly shut down in @PreDestroy before being recreated in @PostConstruct on refresh. Without the @PreDestroy cleanup, each refresh would leak a scheduler thread.
Common Pitfalls
- Resource leaks on refresh: If
@PreDestroydoes not release resources (connections, threads, file handles), each refresh leaks them. Always clean up in@PreDestroyanything that@PostConstructcreates. - Expecting
@RefreshScopeto update non-bean values: Only Spring-managed beans annotated with@RefreshScopeare recreated. Static fields, constants, and beans without@RefreshScopekeep their original values after a config change. - Blocking operations in
@PostConstruct: Long-running initialization in@PostConstructblocks the refresh. Other requests to this bean will wait. Keep initialization fast or move heavy work to a background thread started in@PostConstruct. - Missing Spring Cloud dependencies:
@RefreshScoperequiresspring-cloud-contexton the classpath. Without it, the annotation is silently ignored and beans are never refreshed. - Injecting
@RefreshScopebeans into singleton beans: A singleton bean holds a reference to the original instance. After refresh, the singleton still points to the old bean. Inject@RefreshScopebeans asObjectProvider<T>or use@Lazyproxy injection to always get the current instance.
Summary
@PostConstructruns after dependency injection;@PreDestroyruns before bean destruction@RefreshScopebeans are destroyed and recreated when/actuator/refreshis triggered- Both
@PostConstructand@PreDestroyrun on every refresh cycle, not just startup/shutdown - Always clean up resources in
@PreDestroyto prevent leaks during refresh - Inject
@RefreshScopebeans usingObjectProvideror@Lazyto avoid stale references in singleton beans
Related reading
- spring boot with spring security Error creating bean with name 'securityFilterChainRegistration
- Spring Boot without the web server
- Spring Boot YAML configuration for a list of strings
- Spring Cache Cacheable - not working while calling from another method of the same bean
- Spring Cancel Async Task
- Spring catch all route for index.html
- Spring Cloud - SQS - The specified queue does not exist for this wsdl version
- Spring Cloud AWS SQS fails to connect to service endpoint locally

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.