« Previous | 1 | 2 | 3 | Next »

Spring ─ Custom Events

There are number of steps to be taken to write and publish your own custom events. Follow the instructions given in this chapter to write, publish and handle Custom Spring Events.
1Create a project with a name SpringExample and create a packagecom.jtc under the src folder in the created project. All the classes will be created under this package.
2Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3Create an event class, CustomEvent by extending the ApplicationEvent. This class must define a default constructor which should inherit the constructor from the ApplicationEvent class.
4Once your event class is defined, you can publish it from any class, let us say EventClassPublisher which implementsApplicationEventPublisherAware. You will also need to declare this class in XML configuration file as a bean so that the container can identify the bean as an event publisher, because it implements the ApplicationEventPublisherAware interface.
5A published event can be handled in a class, let us say EventClassHandler which implements ApplicationListener interface and implements onApplicationEvent method for the custom event.
6Create beans configuration file Beans.xml under the src folder and aMainApp class, which will work as Spring application.
7The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.
Here is the content of CustomEvent.java file

package com.jtc;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent{
public CustomEvent(Object source) {
public String toString(){
return "My Custom Event";

Following is the content of the CustomEventPublisher.java file

package com.jtc;
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);

Following is the content of the CustomEventHandler.java file

package com.jtc;
import org.springframework.context.ApplicationListener;
public class CustomEventHandler
implements ApplicationListener<CustomEvent>{
public void onApplicationEvent(CustomEvent event) {

Following is the content of the MainApp.java file

package com.jtc;
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");

Following is the configuration file Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<bean id="customEventHandler"
<bean id="customEventPublisher"

Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message:

y Custom Event
y Custom Event
« Previous | 1 | 2 | 3 | Next »

copyright © 2014 - all rights riserved by javatechnologycenter.com