In Spring Event handling in the ApplicationContext is provided through the ApplicationEvent class and ApplicationListener interface
In general spring has the following events
- ContextRefreshedEvent
- ContextStartedEvent
- ContextStoppedEvent
- ContextClosedEvent
- RequestHandledEvent
ApplicationEvent is the super class, all the above classes are extending this class, here is one example to log the spring events
Create a class which implements ApplicationListener
package com.vinod.test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyEventListener implements ApplicationListener<ApplicationEvent> {
public void onApplicationEvent(ApplicationEvent applicationEvent) {
System.out.println("Current Event is "+ applicationEvent.getClass().getName() + " received!");
}
}
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyEventListener implements ApplicationListener<ApplicationEvent> {
public void onApplicationEvent(ApplicationEvent applicationEvent) {
System.out.println("Current Event is "+ applicationEvent.getClass().getName() + " received!");
}
}
Create a Simple bean
package com.vinod.test;
import org.springframework.stereotype.Component;
@Component
public class Customer {
public String getCustomer() {
return "Thomas....Regular customer";
}
}
import org.springframework.stereotype.Component;
@Component
public class Customer {
public String getCustomer() {
return "Thomas....Regular customer";
}
}
Spring Configuration
package com.vinod.test;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.vinod.test")
public class MySpringConifg {
}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.vinod.test")
public class MySpringConifg {
}
Create a Test class
package com.vinod.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class EventTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(MySpringConifg.class);
context.refresh();
Customer cd = context.getBean(Customer.class);
System.out.println(cd.getCustomer());
}
}
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class EventTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(MySpringConifg.class);
context.refresh();
Customer cd = context.getBean(Customer.class);
System.out.println(cd.getCustomer());
}
}
Output
Current Event is org.springframework.context.event.ContextRefreshedEvent received!
Thomas....Regular customer
No comments:
Post a Comment