Application Events & Listeners — Concept Check
Questions on Application Events & Listeners. Spring fires lifecycle events (ApplicationStartedEvent, ApplicationReadyEvent, etc.) that you can handle with @EventListener to run custom startup logic.
Questions in this quiz
8 questions on Application Events & Listeners — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.During application startup, which event should you listen to for executing code after the full context is ready, including the embedded server?
- AApplicationStartedEvent
- BApplicationReadyEvent
- CContextRefreshedEvent
- DApplicationPreparedEvent
2.You have a service that needs to send an email only after a transaction is successfully committed. Which annotation should you use?
- A@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
- B@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
- C@Async @EventListener
- D@EventListener
3.A developer notices that their event listener is blocking the main thread, causing performance issues. Which annotation can they use to handle the event asynchronously?
- A@TransactionalEventListener
- B@Async @EventListener
- C@EventListener
- D@Async
4.An OrderService publishes an OrderPlacedEvent, but no listeners are reacting to it. What could be a potential issue?
- AListeners are not registered or annotated correctly.
- BOrderPlacedEvent is not a valid event type.
- CApplicationEventPublisher is not injected properly.
- DOrderService does not implement ApplicationEventPublisher.
5.Which event should you handle to perform actions when the application context is refreshed, even during development with DevTools?
- AApplicationReadyEvent
- BApplicationPreparedEvent
- CContextRefreshedEvent
- DApplicationStartedEvent
6.A developer wants to decouple event producers from consumers without using a message broker. What pattern should they use?
- ADirect method calls between components
- BUsing a message broker like RabbitMQ
- CSpring's event system with ApplicationEventPublisher
- DPolling for changes in a shared database
7.What happens if an exception is thrown in an @Async @EventListener method?
- AThe exception is logged, but it does not affect the publisher.
- BThe event is retried automatically.
- CThe publisher transaction rolls back.
- DThe application crashes.
8.What is the role of ApplicationEventPublisher in Spring?
- ATo configure the application context
- BTo handle HTTP requests
- CTo manage transactions in the application
- DTo publish events to registered listeners