《Spring Recipes》第二章笔记:event-based communication

《Spring Recipes》第二章笔记:event-based communication


问题

在bean之间添加事件驱动的通信。

解决方案

1、事件类需要继承ApplicationEvent。实现构造函数,在事件类中添加用于传递信息的属性。
2、事件发布者需要实现ApplicationEventPublisherAware接口,实现setApplicationEventPublisher方法,让容器注入ApplicationEventPublisher接口,并调用ApplicationEventPublisher接口的publish方法发布事件。
3、事件监听者需要实现ApplicationListener接口,实现onApplicationEvent方法接受发布的事件。
4、让容器管理事件发布者和事件监听者。

例:
事件类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 Events

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.



你可能感兴趣的:(spring)