How do you publish custom events in Spring?
Define a custom event as a record, publish it with ApplicationEventPublisher, and handle it with @EventListener. Includes async, transactional and test examples.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Define a custom event as a record, publish it with ApplicationEventPublisher, and handle it with @EventListener. Includes async, transactional and test examples.
ApplicationEvent is the base class for Spring in-process events. See how to publish custom events, use @EventListener and handle container lifecycle events.
Publish custom events with ApplicationEventPublisher and react to them with @EventListener, async listeners and transaction-aware listeners.
@ComponentScan tells Spring which packages to search for stereotype beans. Learn its attributes, filters and how Spring Boot applies it by default.
See how to load Spring configuration from XML using ClassPathXmlApplicationContext and related classes, wire beans, and reuse XML in Spring Boot.
Compare the Spring ApplicationContext implementations, from annotation and XML contexts to web and reactive contexts, and see when to use each.
Use @PreDestroy, DisposableBean or destroyMethod to release resources when a Spring bean is destroyed, with callback order and common pitfalls.
@PreDestroy marks a cleanup method that Spring calls before a bean is destroyed. See when it runs, the jakarta import, ordering with DisposableBean, and common pitfalls.
@PostConstruct marks a method that runs once after a bean is constructed and its dependencies are injected. Learn the lifecycle, jakarta package and alternatives.
Create a custom Spring bean scope by implementing the Scope interface, registering it with CustomScopeConfigurer, and using scoped proxies for singleton injection.
The @Bean annotation marks a method that creates a Spring bean. See how to define, wire, name and scope beans in Java config and choose between @Bean and scanning.
Register Spring beans only when a property, profile, class or other bean is present, using @Conditional, custom conditions and the Spring Boot ConditionalOn annotations.
@Profile makes Spring register beans only when a named profile is active. See expressions, activation methods and Spring Boot profile files and groups.
Use a @Configuration class with @Bean methods to register beans in Spring. See dependency wiring, scopes, profiles and how it compares to component scanning.
@Configuration marks a Spring class as a source of @Bean definitions. See Java config examples, full versus lite mode and proxyBeanMethods.
Constructor injection hands required beans to a class through its constructor. See working examples, ambiguity fixes and how it compares to setter and field injection.
Where should @Autowired go in Spring? Compare constructor, setter and field injection with runnable examples and learn how Spring resolves ambiguous or missing beans.
Use WebClient to make non-blocking HTTP calls in Spring WebFlux. Learn configuration, GET and POST requests, error handling, retries and parallel calls.
subscribeOn sets the thread of the source and subscription; publishOn switches the thread for downstream operators. See examples, schedulers and common mistakes.
Reactor runs on the subscribing thread until you use subscribeOn or publishOn. See how each works, which scheduler to pick and how to handle blocking calls.