How do you configure a custom bean lifecycle in Spring?
Use @PostConstruct, @PreDestroy, @Bean initMethod and destroyMethod, or SmartLifecycle to run custom logic when Spring creates and destroys a bean.
2,887 posts about Java: questions with answers, tutorials and articles, newest first.
Use @PostConstruct, @PreDestroy, @Bean initMethod and destroyMethod, or SmartLifecycle to run custom logic when Spring creates and destroys a bean.
@Lazy defers Spring bean creation until first use. See where to place it, how lazy injection proxies work, and the trade-offs of lazy initialization.
Define Spring prototype beans, avoid the singleton-injection trap with ObjectProvider or @Lookup, and understand why destroy callbacks do not run.
@Scope controls the lifecycle and visibility of a Spring bean. See the built-in scopes, scoped proxies and how to inject short-lived beans into singletons.
Spring beans are singletons by default. See how to define one with stereotype annotations or @Bean, verify it, and keep it thread safe.
@Bean registers the return value of a method as a Spring bean. Learn when to use it instead of @Component, plus naming, scope, and lifecycle options.
Use Spring WebClient to call REST APIs without blocking. Covers requests, error handling, timeouts, retries, filters and how it compares to RestClient.
WebClient is Spring's non-blocking, reactive HTTP client. Learn how to configure it, handle errors and timeouts, and how it compares with RestTemplate and RestClient.
Stream real-time updates from Spring WebFlux with a Flux and ServerSentEvent, broadcast to many clients and keep connections alive.
In Spring WebFlux, RouterFunction maps requests to non-blocking handlers using predicates. See a full example, the predicates and how to test it.
Set up Spring WebFlux in a Spring Boot app with the reactive starter, controllers returning Mono and Flux, WebClient and WebTestClient tests.
Spring MVC is blocking and servlet-based, WebFlux is reactive and non-blocking. See code, a comparison table and how virtual threads change the choice.
Mono is the Reactor type for a single async result in Spring WebFlux. See controller usage, chaining, error handling and common pitfalls.
Learn to write a reactive REST controller in Spring WebFlux using Mono and Flux, functional routes, error handling, streaming and tests.
Build non-blocking Spring Boot services with WebFlux, Mono, Flux, R2DBC and WebClient, and learn when virtual threads with Spring MVC are the better fit.
@InitBinder lets a controller configure the WebDataBinder before request data is bound. See formatters, allowed fields, validators and ControllerAdvice examples.
Compare ConstraintValidator and Spring's Validator interface and build custom validators for fields, whole objects and services.
BindingResult holds binding and validation errors for a Spring MVC model object. Learn where it goes, how to use it, and how to show errors.
Learn four ways to customize Bean Validation messages in Spring: annotation attributes, properties files, MessageSource error codes and custom constraints.
@Valid tells Spring to run Jakarta Bean Validation on a request object and its nested fields. See how to set it up, handle errors and compare it with @Validated.