May 14, 2021 Spring
There are many steps to writing and publishing your own custom events. Follow the instructions given in this chapter to write, publish, and handle custom Spring events.
Steps | Describe |
---|---|
1 | Create a project named SpringExample, and create a package com.tutorialspoint in the src folder that created the project. |
2 | Use the Add External JARs option to add the Spring library you want, as explained in the Spring Hello World Example section. |
3 | Create an event class CustomEvent by extending ApplicationEvent. This class must define a default constructor, which it should inherit from the ApplicationEvent class. |
4 | Once you define an event class, you can publish it from any class, assuming that EventClassPublisher implements ApplicationEventPublisherAware. You also need to declare this class as a bean in an XML profile, and the container recognizes the bean as the event publisher because it implements the ApplicationEventPublisherAware interface. |
5 | Published events can be handled in a class, assuming that EventClassHandler implements the ApplicationListener interface and implements the onApplicationEvent method for custom events. |
6 | Create bean profile Beans and MainApp classes .xml the src folder, which can run as a Spring application. |
7 | The final step is to create the contents of all Java files and bean profiles and run the application as explained below. |
This is the contents of .java CustomEvent file:
package com.tutorialspoint;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent{
public CustomEvent(Object source) {
super(source);
}
public String toString(){
return "My Custom Event";
}
}
Here's what CustomEventPublisher .java file:
package com.tutorialspoint;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class CustomEventPublisher
implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
public void setApplicationEventPublisher
(ApplicationEventPublisher publisher){
this.publisher = publisher;
}
public void publish() {
CustomEvent ce = new CustomEvent(this);
publisher.publishEvent(ce);
}
}
Here's what CustomEventHandler .java the file:
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
public class CustomEventHandler
implements ApplicationListener<CustomEvent>{
public void onApplicationEvent(CustomEvent event) {
System.out.println(event.toString());
}
}
Here's what .java mainApp files are:
package com.tutorialspoint;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
cvp.publish();
cvp.publish();
}
}
Here's
the profile .xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customEventHandler"
class="com.tutorialspoint.CustomEventHandler"/>
<bean id="customEventPublisher"
class="com.tutorialspoint.CustomEventPublisher"/>
</beans>
Once you've finished creating the profile for the source and bean, we're ready to run the application. If everything is fine with your application, the following information will be output:
My Custom Event
My Custom Event