Challenge - 5 Problems
Spring Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when an event is published using ApplicationEventPublisher?
Consider a Spring Boot application where a custom event is published using ApplicationEventPublisher. What is the expected behavior after publishing the event?
Spring Boot
public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source) { super(source); } } @Component public class EventPublisher { private final ApplicationEventPublisher publisher; public EventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } public void publish() { publisher.publishEvent(new CustomEvent(this)); } } @Component public class EventListener { @EventListener public void handleCustomEvent(CustomEvent event) { System.out.println("Event received: " + event.getSource()); } }
Attempts:
2 left
💡 Hint
Think about how Spring handles events by default without extra configuration.
✗ Incorrect
By default, ApplicationEventPublisher publishes events synchronously in the same thread. All listeners annotated with @EventListener for that event type are called immediately.
📝 Syntax
intermediate1:30remaining
Which code snippet correctly publishes a custom event in Spring Boot?
Select the code snippet that correctly publishes a custom event using ApplicationEventPublisher in a Spring Boot component.
Attempts:
2 left
💡 Hint
Check the exact method name in ApplicationEventPublisher interface.
✗ Incorrect
The correct method to publish an event is publishEvent. Other method names do not exist in ApplicationEventPublisher.
🔧 Debug
advanced2:00remaining
Why does the event listener not receive the published event?
Given the following code, why does the event listener not receive the event?
@Component
public class MyListener {
public void onCustomEvent(CustomEvent event) {
System.out.println("Event received");
}
}
@Component
public class MyPublisher {
private final ApplicationEventPublisher publisher;
public MyPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void publish() {
publisher.publishEvent(new CustomEvent(this));
}
}
Attempts:
2 left
💡 Hint
Check how Spring knows which methods listen to events.
✗ Incorrect
Spring detects event listener methods by the @EventListener annotation. Without it, the method is not called.
🧠 Conceptual
advanced2:30remaining
What is the effect of publishing an event with a payload object instead of extending ApplicationEvent?
In Spring Framework 4.2 and later, you can publish any object as an event without extending ApplicationEvent. What is the effect of publishing a plain payload object instead of a subclass of ApplicationEvent?
Attempts:
2 left
💡 Hint
Spring Framework supports publishing any object as event since version 4.2.
✗ Incorrect
Since Spring 4.2, any object can be published as an event. Listeners receive the object directly without needing to extend ApplicationEvent.
❓ state_output
expert2:30remaining
What is the console output after publishing two events synchronously?
Given the following Spring Boot components, what will be printed to the console after calling publisher.publish()?
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source) { super(source); }
}
@Component
public class EventPublisher {
private final ApplicationEventPublisher publisher;
public EventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; }
public void publish() {
publisher.publishEvent(new CustomEvent("First"));
publisher.publishEvent(new CustomEvent("Second"));
}
}
@Component
public class EventListener {
@EventListener
public void onEvent(CustomEvent event) {
System.out.println("Received: " + event.getSource());
}
}
Attempts:
2 left
💡 Hint
Events are published synchronously in order by default.
✗ Incorrect
Events are published synchronously in the order they are called, so the listener prints both in sequence.