Assume this simple Boot application:
@SpringBootApplication
// @EnableTransactionManagement
public class TxEventListenerApplication {
public static void main(String[] args) {
var ctx = SpringApplication.run(TxEventListenerApplication.class, args);
ctx.publishEvent(new CustomEvent());
}
@TransactionalEventListener
public void onCustomEvent(CustomEvent event) {
System.out.println("Event received!");
}
record CustomEvent() {}
}
This will print Event received!
upon startup, despite the event listener being declared as @TransactionalEventListener
and no transaction being active. That is because without @EnableTransactionManagment
, the DefaultEventListenerFactory
will see @EventListener
present as meta-annotation and thus create a standard ApplicationListenerMethodAdapter
for the method. Activating @EnableTransactionManagement
, which is enabled in standard Boot applications as soon as any kind of data access technology is present, will cause the factory to be replaced by RestrictedTransactionalEventListenerFactory
which then causes the additional integration with the transaction infrastructure.