《Spring Recipes》第二章筆記:event-based communication

《Spring Recipes》第二章筆記:event-based communication


問題

在bean之間添加事件驅動的通訊。

解決方案

一、事件類須要繼承ApplicationEvent。實現構造函數,在事件類中添加用於傳遞信息的屬性。
二、事件發佈者須要實現ApplicationEventPublisherAware接口,實現setApplicationEventPublisher方法,讓容器注入ApplicationEventPublisher接口,並調用ApplicationEventPublisher接口的publish方法發佈事件。
三、事件監聽者須要實現ApplicationListener接口,實現onApplicationEvent方法接受發佈的事件。
四、讓容器管理事件發佈者和事件監聽者。

例:
事件類MyEvent:
public class MyEvent extends ApplicationEvent {

	private int i;
	
	public MyEvent(Object source,int i) {
		super(source);
		this.i = i;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}
	
	

}

事件發佈者MyEventPublisher:
public class MyEventPublisher implements ApplicationEventPublisherAware {

	private ApplicationEventPublisher publisher;
	
	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
		this.publisher = publisher;		
	}
	
	public void publish(ApplicationEvent event) {
		this.publisher.publishEvent(event);
	}
	

}

事件監聽者MyEventListener:
public class MyEventListener implements ApplicationListener<MyEvent> {

	private MyEvent event;
	
	@Override
	public void onApplicationEvent(MyEvent event) {
		this.event = event;
	}
	
	public void print() {
		System.out.println(this.event.getI());
	}

}

配置文件:
<bean name="publisher" class="com.ljm.springrecipses.event.MyEventPublisher" />
	
	<bean name="listener" class="com.ljm.springrecipses.event.MyEventListener" />


調用程序:
public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("com/ljm/springrecipses/event/beans.xml");
		MyEventPublisher pub = ctx.getBean(MyEventPublisher.class);
		//構造事件
                MyEvent e = new MyEvent(new MyEventCommunicationTest(), 10);
                //發佈事件
		pub.publish(e);
	 	MyEventListener lis = ctx.getBean(MyEventListener.class);
                //打印事件結果
	 	lis.print();
	}


Spring內部事件


Spring有不少自帶的內部事件,用戶能夠經過實現ApplicationListener進行監聽:

Table 4.7. Built-in Eventsjava

Event Explanation
ContextRefreshedEvent Published when the ApplicationContext is initialized or refreshed, for example, using the refresh() method on the ConfigurableApplicationContext interface. "Initialized" here means that all beans are loaded, post-processor beans are detected and activated, singletons are pre-instantiated, and the ApplicationContext object is ready for use. As long as the context has not been closed, a refresh can be triggered multiple times, provided that the chosen ApplicationContext actually supports such "hot" refreshes. For example, XmlWebApplicationContext supports hot refreshes, but GenericApplicationContext does not.
ContextStartedEvent Published when the ApplicationContext is started, using the start() method on the ConfigurableApplicationContext interface. "Started" here means that all Lifecycle beans receive an explicit start signal. Typically this signal is used to restart beans after an explicit stop, but it may also be used to start components that have not been configured for autostart , for example, components that have not already started on initialization.
ContextStoppedEvent Published when the ApplicationContext is stopped, using the stop() method on the ConfigurableApplicationContext interface. "Stopped" here means that all Lifecycle beans receive an explicit stop signal. A stopped context may be restarted through a start() call.
ContextClosedEvent Published when the ApplicationContext is closed, using the close() method on the ConfigurableApplicationContext interface. "Closed" here means that all singleton beans are destroyed. A closed context reaches its end of life; it cannot be refreshed or restarted.
RequestHandledEvent A web-specific event telling all beans that an HTTP request has been serviced. This event is published after the request is complete. This event is only applicable to web applications using Spring's DispatcherServlet.
相關文章
相關標籤/搜索