How do you define a singleton bean in Spring?
Spring beans are singletons by default. See how to define one with stereotype annotations or @Bean, verify it, and keep it thread safe.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
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.
Annotate your form model, add @Valid with a BindingResult in the controller, and show field errors in Thymeleaf. Includes custom validators and central handling.
@ModelAttribute binds form and query parameters to a Java object and exposes it to the view in Spring MVC. See usage on parameters and methods.
Return objects as JSON in Spring MVC with @RestController, @ResponseBody and ResponseEntity, and control status codes, headers and serialisation.
@ResponseStatus changes the default 200 status of a Spring handler method. Learn 201 and 204 usage, exception handlers, pitfalls and testing.