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(); }
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 . |