Custom Events in Spring


There are number of ventures to be taken to compose and distribute your own custom occasions. Adhere to the guidelines given in this section to compose, distribute and handle Custom Spring Events.

Steps Description
1 Create a venture with a name SpringExample and make a bundle com.javacodegeeks under the src organizer in the made undertaking. Every one of the classes will be made under this package.
2 Add required Spring libraries utilizing Add External JARs alternative as clarified in the Spring Hello World Example chapter.
3 Create an occasion class, CustomEvent by expanding ApplicationEvent. This class must characterize a default constructor which ought to acquire constructor from ApplicationEvent class.
4 Once your occasion class is characterized, you can distribute it from any class, let us state EventClassPublisher which executes ApplicationEventPublisherAware. You will likewise need to proclaim this class in XML arrangement record as a bean with the goal that the compartment can recognize the bean as an occasion distributer since it actualizes the ApplicationEventPublisherAware interface.
5 A distributed occasion can be taken care of in a class, let us state EventClassHandler which executes ApplicationListener interface and actualizes onApplicationEvent technique for the custom event.
6 Create beans setup document Beans.xml under the src envelope and a MainApp class which will fill in as Spring application.
7 The last advance is to make the substance of all the Java records and Bean Configuration document and run the application as clarified below.

Here is the substance of CustomEvent.java file

package com.javacodegeeks;
    
    
import org.springframework.context.ApplicationEvent; 

public class CustomEvent extends ApplicationEvent{ 

public CustomEvent(Object source) { 

super(source); 

} 

public String toString(){ 

return "My Custom Event"; 

} 

}

Following is the substance of the CustomEventPublisher.java file

package com.javacodegeeks; 

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); 

} 

}

Following is the substance of the CustomEventHandler.java file

package com.javacodegeeks;
    
        
import org.springframework.context.ApplicationListener; 

public class CustomEventHandler implements ApplicationListener<CustomEvent> { 

public void onApplicationEvent(CustomEvent event) { 

System.out.println(event.toString()); 

} 

}

Following is the substance of the MainApp.java file

package com.javacodegeeks; 

import org.springframework.context.ConfigurableApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp { 

public static void main(String[] args) { 

ConfigurableApplicationContext setting = 

new ClassPathXmlApplicationContext("Beans.xml"); 

CustomEventPublisher cvp = 

(CustomEventPublisher) context.getBean("customEventPublisher"); 

cvp.publish(); 

cvp.publish(); 

} 

}

Following is the setup record Beans.xml

<?xml variant = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/pattern/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/pattern/beans 

http://www.springframework.org/construction/beans/spring-beans-3.0.xsd"> 

<bean id = "customEventHandler" class = "com.javacodegeeks.CustomEventHandler"/> 

<bean id = "customEventPublisher" class = "com.javacodegeeks.CustomEventPublisher"/> 

</beans>

Once you are finished making the source and bean design records, let us run the application. On the off chance that all is well with your application, it will print the accompanying message −

y Custom Event 

y Custom Event 







© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3