0
0
Spring Bootframework~10 mins

Event publishing with ApplicationEventPublisher in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to inject ApplicationEventPublisher in a Spring component.

Spring Boot
public class MyComponent {

    private final ApplicationEventPublisher [1];

    public MyComponent(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }
}
Drag options to blanks, or click blank then click option'
AeventPub
Bpublisher
CeventPublisher
DapplicationEventPublisher
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not match the constructor parameter.
Using a very long or unclear variable name.
2fill in blank
medium

Complete the code to publish a custom event using ApplicationEventPublisher.

Spring Boot
public void publishEvent(String message) {
    MyCustomEvent event = new MyCustomEvent(this, message);
    [1].publishEvent(event);
}
Drag options to blanks, or click blank then click option'
AeventPublisher
BapplicationEventPublisher
Cpublisher
DeventPub
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that was not declared.
Calling a wrong method instead of publishEvent.
3fill in blank
hard

Fix the error in the event class constructor to properly extend ApplicationEvent.

Spring Boot
public class MyCustomEvent extends ApplicationEvent {

    private final String message;

    public MyCustomEvent([1] source, String message) {
        super(source);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}
Drag options to blanks, or click blank then click option'
AObject
BString
CApplicationEvent
DMyCustomEvent
Attempts:
3 left
💡 Hint
Common Mistakes
Using String or other types instead of Object for source.
Not calling super(source) in constructor.
4fill in blank
hard

Fill both blanks to create and publish a new event with a message.

Spring Boot
public void sendMessage(String msg) {
    [1] event = new MyCustomEvent(this, msg);
    [2].publishEvent(event);
}
Drag options to blanks, or click blank then click option'
AMyCustomEvent
Bpublisher
Cevent
DApplicationEventPublisher
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class name for the event.
Trying to call publishEvent on the event object instead of the publisher.
5fill in blank
hard

Fill all three blanks to define a Spring component that publishes an event with a message.

Spring Boot
@Component
public class EventPublisher {

    private final [1] publisher;

    public EventPublisher([2]) {
        this.publisher = eventPublisher;
    }

    public void publish(String msg) {
        [3] event = new MyCustomEvent(this, msg);
        publisher.publishEvent(event);
    }
}
Drag options to blanks, or click blank then click option'
AApplicationEventPublisher publisher
BApplicationEventPublisher
CMyCustomEvent
DApplicationEventPublisher eventPublisher
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field declaration and constructor parameter syntax.
Using wrong class names or missing variable names.