Bird
Raised Fist0
Spring Bootframework~20 mins

Event publishing with ApplicationEventPublisher in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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());
    }
}
AThe event triggers a restart of the Spring Boot application context.
BThe event is queued and processed asynchronously by default in a separate thread pool.
CThe event is published synchronously and all matching @EventListener methods are called immediately in the same thread.
DThe event is ignored unless explicitly registered in a configuration file.
Attempts:
2 left
💡 Hint
Think about how Spring handles events by default without extra configuration.
📝 Syntax
intermediate
1: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.
Apublisher.publishEvent(new CustomEvent(this));
Bpublisher.publish(new CustomEvent(this));
Cpublisher.fireEvent(new CustomEvent(this));
Dpublisher.sendEvent(new CustomEvent(this));
Attempts:
2 left
💡 Hint
Check the exact method name in ApplicationEventPublisher interface.
🔧 Debug
advanced
2: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)); } }
AThe listener method is missing the @EventListener annotation.
BThe event class CustomEvent is not annotated with @Component.
CThe ApplicationEventPublisher is not injected properly.
DThe event must be published asynchronously to be received.
Attempts:
2 left
💡 Hint
Check how Spring knows which methods listen to events.
🧠 Conceptual
advanced
2: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?
AThe event triggers a runtime exception due to missing ApplicationEvent superclass.
BThe event is published normally and listeners can receive the payload object directly.
CThe event is ignored because it does not extend ApplicationEvent.
DThe event is published but listeners must cast the event to ApplicationEvent manually.
Attempts:
2 left
💡 Hint
Spring Framework supports publishing any object as event since version 4.2.
❓ state_output
expert
2: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()); } }
ANo output because events are asynchronous
B
Received: Second
Received: First
CReceived: First
D
Received: First
Received: Second
Attempts:
2 left
💡 Hint
Events are published synchronously in order by default.

Practice

(1/5)
1. What is the primary role of ApplicationEventPublisher in a Spring Boot application?
easy
A. To manage HTTP requests and responses
B. To listen and handle events automatically
C. To configure database connections
D. To send or publish events within the application

Solution

  1. Step 1: Understand the purpose of ApplicationEventPublisher

    This interface is designed to publish events to listeners within the Spring application context.
  2. Step 2: Differentiate from listeners and other components

    Listeners handle events, but ApplicationEventPublisher only sends or publishes them.
  3. Final Answer:

    To send or publish events within the application -> Option D
  4. Quick Check:

    Event sending = ApplicationEventPublisher [OK]
Hint: Publisher sends events; listeners receive them [OK]
Common Mistakes:
  • Confusing publisher with listener
  • Thinking it manages HTTP or database
  • Assuming it handles event processing
2. Which of the following is the correct way to inject ApplicationEventPublisher in a Spring Boot component?
easy
A. @Autowired private ApplicationEventPublisher publisher;
B. private ApplicationEventPublisher publisher = new ApplicationEventPublisher();
C. @Inject ApplicationEventPublisher publisher = null;
D. ApplicationEventPublisher publisher = ApplicationEventPublisher.getInstance();

Solution

  1. Step 1: Recall Spring dependency injection syntax

    Spring uses @Autowired to inject beans automatically into components.
  2. Step 2: Check each option for correctness

    @Autowired private ApplicationEventPublisher publisher; uses @Autowired correctly. private ApplicationEventPublisher publisher = new ApplicationEventPublisher(); tries to instantiate interface (not allowed). @Inject ApplicationEventPublisher publisher = null; mixes @Inject with assignment (not typical in Spring). ApplicationEventPublisher publisher = ApplicationEventPublisher.getInstance(); uses a non-existent getInstance method.
  3. Final Answer:

    @Autowired private ApplicationEventPublisher publisher; -> Option A
  4. Quick Check:

    @Autowired injects beans properly [OK]
Hint: Use @Autowired for Spring bean injection [OK]
Common Mistakes:
  • Trying to instantiate ApplicationEventPublisher directly
  • Mixing injection annotations incorrectly
  • Using static getInstance() which doesn't exist
3. Given the following code snippet, what will be printed when publishEvent() is called?
public class MyEvent extends ApplicationEvent {
    public MyEvent(Object source) { super(source); }
}

@Component
public class MyPublisher {
    @Autowired
    private ApplicationEventPublisher publisher;

    public void publishEvent() {
        publisher.publishEvent(new MyEvent(this));
    }
}

@Component
public class MyListener {
    @EventListener
    public void handleMyEvent(MyEvent event) {
        System.out.println("Event received");
    }
}
medium
A. No output
B. Compilation error
C. Event received
D. Runtime exception

Solution

  1. Step 1: Understand event publishing and listening

    The publisher sends a MyEvent instance. The listener listens for MyEvent and prints "Event received".
  2. Step 2: Confirm no errors in code

    Code uses correct annotations and event class extending ApplicationEvent, so no errors or exceptions.
  3. Final Answer:

    Event received -> Option C
  4. Quick Check:

    Listener prints message on event [OK]
Hint: Listener prints message when event is published [OK]
Common Mistakes:
  • Forgetting @EventListener annotation
  • Not extending ApplicationEvent in event class
  • Expecting output without publishing event
4. Identify the error in the following Spring Boot event publishing code:
@Component
public class EventSender {
    private ApplicationEventPublisher publisher;

    public void send() {
        publisher.publishEvent(new CustomEvent(this));
    }
}

public class CustomEvent extends ApplicationEvent {
    public CustomEvent(Object source) { super(source); }
}
medium
A. send method should be static
B. ApplicationEventPublisher is not injected, so publisher is null
C. publishEvent method does not exist
D. CustomEvent does not extend ApplicationEvent

Solution

  1. Step 1: Check ApplicationEventPublisher usage

    The publisher field is declared but never injected or assigned, so it remains null.
  2. Step 2: Consequence of null publisher

    Calling publishEvent on null causes NullPointerException at runtime.
  3. Final Answer:

    ApplicationEventPublisher is not injected, so publisher is null -> Option B
  4. Quick Check:

    Missing injection causes null publisher [OK]
Hint: Always inject ApplicationEventPublisher before use [OK]
Common Mistakes:
  • Forgetting @Autowired on publisher
  • Assuming interface can be instantiated
  • Thinking method must be static
5. You want to publish a custom event UserCreatedEvent and ensure multiple listeners react differently. Which approach correctly implements this in Spring Boot?
hard
A. Create UserCreatedEvent extending ApplicationEvent, inject ApplicationEventPublisher, publish event, and annotate multiple methods with @EventListener for UserCreatedEvent
B. Create UserCreatedEvent as a simple POJO, call listeners manually without publishing events
C. Use ApplicationEventPublisher to publish String messages instead of event objects
D. Create UserCreatedEvent but do not extend ApplicationEvent; rely on @EventListener to catch any object

Solution

  1. Step 1: Define event class properly

    Custom events should extend ApplicationEvent for Spring to recognize them.
  2. Step 2: Publish event and create multiple listeners

    Inject ApplicationEventPublisher to publish event. Use @EventListener on multiple methods to react differently.
  3. Step 3: Evaluate other options

    Create UserCreatedEvent as a simple POJO, call listeners manually without publishing events skips event system, losing decoupling. Use ApplicationEventPublisher to publish String messages instead of event objects misuses publisher with Strings. Create UserCreatedEvent but do not extend ApplicationEvent; rely on @EventListener to catch any object breaks event contract, listeners may not work.
  4. Final Answer:

    Create UserCreatedEvent extending ApplicationEvent, inject ApplicationEventPublisher, publish event, and annotate multiple methods with @EventListener for UserCreatedEvent -> Option A
  5. Quick Check:

    Proper event class + publisher + listeners = correct [OK]
Hint: Extend ApplicationEvent and use @EventListener for multiple reactions [OK]
Common Mistakes:
  • Not extending ApplicationEvent for custom events
  • Calling listeners manually instead of publishing
  • Publishing wrong object types like String
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard