ApplicationContext具有發布事件的能力。這是因為該接口繼承了ApplicationEventPublisher接口。Spring中與事件有關的接口和類主要包括ApplicationEvent、ApplicationListener。
定義一個事件的類需要繼承ApplicationEvent或者ApplicationContextEvent抽象類,該抽象類中只有一個構造函數,並 且帶有一個Object類型的參數作為事件源,並且該事件源不能為null,因此我們需要在自己的構造函數中執行super(Object)。
針對一種事件,可能需要特定的監聽器,因此,監聽器需要實現ApplicationListener接口。當監聽器接收到一個事件的時候,就會執行它的 onApplicationEvent()方法。由於Spring IoC中的事件模型是一種簡單的、粗粒度的監聽模型,當有一個事件到達時,所有的監聽器都會接收到,並且作出響應,如果希望只針對某些類型進行監聽,需要 在代碼中進行控制。
至此便完成了事件的發布,當ApplicationContext接收到事件后,事件的廣播是Spring內部給我們做的,不需要了解具體的細節。其實在 Spring讀取配置文件之后,利用反射,將所有實現ApplicationListener的Bean找出來,注冊為容器的事件監聽器。當接收到事件的 時候,Spring會逐個調用事件監聽器。剩下要做的就是在配置文件中配置監聽器
一.基於xml方式
1.ApplicationEvent類的子類
public class UserEvent extends ApplicationEvent { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public UserEvent(Object source, int age, String name) { super(source); this.age = age; this.name = name; } }
2.監聽器類
public class UserListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if(event instanceof UserEvent) { UserEvent userEvent = (UserEvent)event; Integer age = userEvent.getAge(); String name = userEvent.getName(); System.out.println("age " +age +" name " +name); } } }
3.配置文件中注冊
<?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.xsd"> <bean id = "userListener" class="com.itheima.test.UserListener"></bean> </beans>
4.測試類
public class EveatTest { public static void main(String []args) { ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml"); // ApplicationContext context=new AnnotationConfigApplicationContext(EventConfig.class); System.out.println(context); UserEvent userEvent = new UserEvent("object",28,"張三"); context.publishEvent(userEvent); } }
二.基本注解方式
1.ApplicationEvent類的子類
public class UserEvent extends ApplicationEvent
{
private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public UserEvent(Object source, int age, String name) { super(source); this.age = age; this.name = name; } }
2.監聽器類
@Component
public class UserListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof UserEvent) {
UserEvent userEvent = (UserEvent)event;
Integer age = userEvent.getAge();
String name = userEvent.getName();
System.out.println("age " +age +" name " +name);
}
}
}
3.配置文件
@Configuration @ComponentScan("com.itheima.test") public class EventConfig { }
4.測試類
public class EveatTest {
public static void main(String []args) {
// ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext context=new AnnotationConfigApplicationContext(EventConfig.class);
System.out.println(context);
UserEvent userEvent = new UserEvent("object",28,"張三");
context.publishEvent(userEvent);
}
}