Spring series of tutorials
- Spring Framework Introduction
- Spring Framework module
- Spring development environment to build (Eclipse)
- Create a simple Spring application
- Spring Inversion of Control container (Inversion of Control - IOC)
- Appreciated dependency injection (DI - Dependency Injection)
- Bean XML configuration (1) - through XML configuration loaded Bean
- Bean XML configuration (2) - Bean scope and lifecycle callback method configuration
- Bean XML configuration (3) - configuration dependency injection
- Bean XML configuration (4) - Automatic Assembly
- Bean annotation (Annotation) Configuration (1) - Load Bean via annotations
- Bean annotation (Annotation) Configuration (2) - Bean scope and lifecycle callback method configuration
- Bean Annotations (Annotation) configuration (3) - configuration dependency injection
- Bean Java Configuration
- Spring Aspect Oriented Programming (AOP)
- Spring event (1) - built-in event
- Spring event (2) - Custom Event
In addition to built-in event, Spring can also use custom events.
How to use custom events:
- Create an event class - Extension
ApplicationEvent
class, create event class. - Create a send class - Send the class get
ApplicationEventPublisher
instances send events. - Create a class monitor - implement
ApplicationListener
the interface, create a listener class.
Event Class
Event class is used to store event data. Create a simple event category below.
CustomEvent.java
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source, String message) {
super(source);
}
public String toString() {
return "我是自定义事件";
}
}
Send class
Send event object class is created and sent.
To send an event to introduce two methods here:
- Use
@autowired
annotations injectApplicationEventPublisher
instance.
CustomEventPublisher.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
public class CustomEventPublisher {
@Autowired
private ApplicationEventPublisher publisher;
public void publish() {
CustomEvent event = new CustomEvent(this);
publisher.publishEvent(event);
}
}
- Sending class implements
ApplicationEventPublisherAware
the interface, acquiredApplicationEventPublisher
instance.
CustomEventPublisher.java
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class CustomEventPublisher implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
// 必须重写这个方法获取ApplicationEventPublisher
public void setApplicationEventPublisher (ApplicationEventPublisher publisher){
this.publisher = publisher;
}
public void publish() {
CustomEvent event = new CustomEvent(this);
publisher.publishEvent(event);
}
}
If you send a class implements ApplicationEventPublisherAware
an interface, send the class must be declared as bean, Spring containers to identify it as an event sender.
<bean id="customEventPublisher" class="CustomEventPublisher"/>
Listener class
Listener class listens for events. Listener class must implement ApplicationListener
the interface, and is defined as Bean Spring container so that it can be loaded.
beans.xml
<bean id="customEventHandler" class="CustomEventHandler"/>
CustomEventHandler.java
import org.springframework.context.ApplicationListener;
public class CustomEventHandler implements ApplicationListener<CustomEvent> {
public void onApplicationEvent(CustomEvent event) {
System.out.println("收到事件:" + event.toString());
}
}
run
Test custom event.
Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher publisher = (CustomEventPublisher) context.getBean("customEventPublisher");
publisher.publish();
}
}