Bird
0
0

Given this Spring Boot event listener code, what will be printed when new MyEvent("Hello") is published?

medium📝 component behavior Q4 of 15
Spring Boot - Messaging
Given this Spring Boot event listener code, what will be printed when new MyEvent("Hello") is published?
@Component
public class MyListener {
  @EventListener
  public void onMyEvent(MyEvent event) {
    System.out.println("Event received: " + event.getMessage());
  }
}

public class MyEvent {
  private final String message;
  public MyEvent(String message) { this.message = message; }
  public String getMessage() { return message; }
}
ACompilation error due to missing annotation
BEvent received: Hello
CNo output, method not called
DNullPointerException at runtime
Step-by-Step Solution
Solution:
  1. Step 1: Understand event publishing and listener

    The listener method is annotated with @EventListener and prints the event message.
  2. Step 2: When MyEvent with message "Hello" is published, listener prints message

    The output will be "Event received: Hello".
  3. Final Answer:

    Event received: Hello -> Option B
  4. Quick Check:

    Listener prints event message = "Event received: Hello" [OK]
Quick Trick: Listener prints event message when event is published [OK]
Common Mistakes:
  • Assuming listener is not called without explicit registration
  • Confusing annotation usage
  • Expecting runtime errors without cause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes