Spring Core Basics
Spring Questions and Answers
Spring Useful Resources
Selected Reading
- Spring - Logging with Log4J
- Spring - Web MVC Framework
- Spring - Transaction Management
- Spring - JDBC Framework
- Spring - AOP with Spring Framework
- Spring - Custom Events in Spring
- Spring - Event Handling in Spring
- Spring - Java Based Configuration
- Annotation Based Configuration
- Spring - Beans Auto-Wiring
- Spring - Injecting Collection
- Spring - Injecting Inner Beans
- Spring - Dependency Injection
- Spring - Bean Definition Inheritance
- Spring - Bean Post Processors
- Spring - Bean Life Cycle
- Spring - Bean Scopes
- Spring - Bean Definition
- Spring - IoC Containers
- Spring - Hello World Example
- Spring - Environment Setup
- Spring - Architecture
- Spring - Overview
- Spring - Home
Spring Questions and Answers
Spring Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring - Custom Events in Spring
Custom Events in Spring
There are number of steps to be taken to write and pubpsh your own custom events. Follow the instructions given in this chapter to write, pubpsh and handle Custom Spring Events.
Steps | Description |
---|---|
1 | Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project. All the classes will be created under this package. |
2 | Add required Spring pbraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create an event class, CustomEvent by extending ApppcationEvent. This class must define a default constructor which should inherit constructor from ApppcationEvent class. |
4 | Once your event class is defined, you can pubpsh it from any class, let us say EventClassPubpsher which implements ApppcationEventPubpsherAware. 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 pubpsher because it implements the ApppcationEventPubpsherAware interface. |
5 | A pubpshed event can be handled in a class, let us say EventClassHandler which implements ApppcationListener interface and implements onApppcationEvent method for the custom event. |
6 | Create beans configuration file Beans.xml under the src folder and a MainApp class which will work as Spring apppcation. |
7 | The final step is to create the content of all the Java files and Bean Configuration file and run the apppcation as explained below. |
Here is the content of CustomEvent.java file
package com.tutorialspoint; import org.springframework.context.ApppcationEvent; pubpc class CustomEvent extends ApppcationEvent{ pubpc CustomEvent(Object source) { super(source); } pubpc String toString(){ return "My Custom Event"; } }
Following is the content of the CustomEventPubpsher.java file
package com.tutorialspoint; import org.springframework.context.ApppcationEventPubpsher; import org.springframework.context.ApppcationEventPubpsherAware; pubpc class CustomEventPubpsher implements ApppcationEventPubpsherAware { private ApppcationEventPubpsher pubpsher; pubpc void setApppcationEventPubpsher (ApppcationEventPubpsher pubpsher) { this.pubpsher = pubpsher; } pubpc void pubpsh() { CustomEvent ce = new CustomEvent(this); pubpsher.pubpshEvent(ce); } }
Following is the content of the CustomEventHandler.java file
package com.tutorialspoint; import org.springframework.context.ApppcationListener; pubpc class CustomEventHandler implements ApppcationListener<CustomEvent> { pubpc void onApppcationEvent(CustomEvent event) { System.out.println(event.toString()); } }
Following is the content of the MainApp.java file
package com.tutorialspoint; import org.springframework.context.ConfigurableApppcationContext; import org.springframework.context.support.ClassPathXmlApppcationContext; pubpc class MainApp { pubpc static void main(String[] args) { ConfigurableApppcationContext context = new ClassPathXmlApppcationContext("Beans.xml"); CustomEventPubpsher cvp = (CustomEventPubpsher) context.getBean("customEventPubpsher"); cvp.pubpsh(); cvp.pubpsh(); } }
Following is the configuration file Beans.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 = "customEventPubpsher" class = "com.tutorialspoint.CustomEventPubpsher"/> </beans>
Once you are done creating the source and bean configuration files, let us run the apppcation. If everything is fine with your apppcation, it will print the following message −
y Custom Event y Custom EventAdvertisements