: In SpringBoot, example for ApplicationEvent
- @EventListener
- @TransactionalEventListener
- Create event class to handle as unit in ApplicationContext
- Create event publisher to publish an event to ApplicationContext
- Create event listener to handle an event
- Create environment to test ApplicationEvent
import org.springframework.context.ApplicationEvent;
public class TestEvent extends ApplicationEvent {
private final String message;
public TestEvent(Object source, String message) {
super(source);
this.message = message;
}
public String getMessage() {
return message;
}
}
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
@Component
public class TestEventPublisher implements ApplicationEventPublisherAware {
private ApplicationEventPublisher eventPublisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.eventPublisher = applicationEventPublisher;
}
public void publish(TestEvent testEvent) {
this.eventPublisher.publishEvent(testEvent);
}
}
- It is required @Component, because event listener has to load it into Spring Container
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class TestEventListener {
@Async
@EventListener
public void handleTestEvent(TestEvent event) {
System.out.println(event.getMessage());
}
}
import com.example.springapplicationeventexample.event.TestEvent;
import com.example.springapplicationeventexample.event.TestEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestResource {
private final TestEventPublisher testEventPublisher;
public TestResource(TestEventPublisher testEventPublisher) {
this.testEventPublisher = testEventPublisher;
}
@GetMapping
public void publishEvent() {
TestEvent testEvent = new TestEvent(this, "Hello world!");
testEventPublisher.publish(testEvent);
}
}