经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Spring入门学习笔记(3)——事件处理类
来源:cnblogs  作者:NinWoo  时间:2018/10/15 9:30:13  对本文有异议

目录

Spring中的事件处理

ApplicationContext 是Spring的核心模块,管理着Beans完整的生命周期。当加载Bean时,ApplicationContext会发布特定类型的事件。
eg:当Context启动时ContextStartEvent被发布,当关闭时,ContextStoppedEvent被发布。

ApplicationContext事件处理被ApplicationEvent类和ApplicationListener接口提供。因此,实现了ApplicationListener的bean,每次ApplicationContext发布ApplicationEvent时,Bean将会被通知。

Spring内建事件

  • ContextRefreshedEvent : 当ApplicationContext被初始化或者刷新时被发布。也可以通过调用ConfigurableApplicationContext接口的refresh()函数发起。
  • ContextStartedEvent : 当Application使用ConfigurableApplicationContext的start()方法启动时被发布。您可以轮询您的数据库,也可以在收到此事件后重新启动任何已停止的应用程序。
  • ContextStoppedEvent : 当ApplicationContext在ConfigurableApplicationContext接口上使用stop()方法停止时,就会发布这个事件。你可以在收到这个活动后做家务。
  • ContextClosedEvent : 当使用ConfigurableApplicationContext接口上的close()方法关闭ApplicationContext时,将发布此事件。一个封闭的环境到达了生命的终点;不能刷新或重新启动。
  • RequestHandledEvent : 这是一个特定于web的事件,它告诉所有bean HTTP请求已经得到了服务。

Spring的事件处理是单线程的,因此如果发布了一个事件,直到并且除非所有接收者都得到消息,否则进程将被阻塞,线程将不会继续。因此,如果要使用事件处理,那么在设计应用程序时应该小心。

监听Context事件

要想监听一个context事件,bean需要实现仅有一个方法onApplicationEvent()的ApplicationListener接口

Example

HelloWorld.java

  1. public class HelloWorld {
  2. private String message;
  3. public void setMessage(String message){
  4. this.message = message;
  5. }
  6. public void getMessage(){
  7. System.out.println("Your Message : " + message);
  8. }
  9. }

CStartEventHandler.java

  1. public class CStartEventHandler
  2. implements ApplicationListener<ContextStartedEvent>{
  3. public void onApplicationEvent(ContextStartedEvent event) {
  4. System.out.println("ContextStartedEvent Received");
  5. }
  6. }

CStopEventHandler

  1. public class CStopEventHandler
  2. implements ApplicationListener<ContextStoppedEvent>{
  3. public void onApplicationEvent(ContextStoppedEvent event) {
  4. System.out.println("ContextStoppedEvent Received");
  5. }
  6. }

MainApp.java

  1. public class MainApp {
  2. public static void main(String[] args) {
  3. ConfigurableApplicationContext context =
  4. new ClassPathXmlApplicationContext("Beans.xml");
  5. // Let us raise a start event.
  6. context.start();
  7. HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  8. obj.getMessage();
  9. // Let us raise a stop event.
  10. context.stop();
  11. }
  12. }

Beans

  1. <?xml version = "1.0" encoding = "UTF-8"?>
  2. <beans xmlns = "http://www.springframework.org/schema/beans"
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation = "http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld">
  7. <property name = "message" value = "Hello World!"/>
  8. </bean>
  9. <bean id = "cStartEventHandler" class = "com.tutorialspoint.CStartEventHandler"/>
  10. <bean id = "cStopEventHandler" class = "com.tutorialspoint.CStopEventHandler"/>
  11. </beans>

输出:

  1. ContextStartedEvent Received
  2. Your Message : Hello World!
  3. ContextStoppedEvent Received

自定义Spring事件

下边的案例将讲述如何编写和发布你自己的自定义事件

添加自定义事件CustomEvent.java,继承ApplicationEvent类

  1. public class CustomEvent extends ApplicationEvent{
  2. public CustomEvent(Object source) {
  3. super(source);
  4. }
  5. public String toString(){
  6. return "My Custom Event";
  7. }
  8. }

添加自定义事件发布类CustomEventPublisher.java,实现ApplicationEventPublisherAware接口

  1. public class CustomEventPublisher implements ApplicationEventPublisherAware {
  2. private ApplicationEventPublisher publisher;
  3. public void setApplicationEventPublisher (ApplicationEventPublisher publisher) {
  4. this.publisher = publisher;
  5. }
  6. public void publish() {
  7. CustomEvent ce = new CustomEvent(this);
  8. publisher.publishEvent(ce);
  9. }
  10. }

自定义事件监听处理类,实现ApplicationListener

  1. public class CustomEventHandler implements ApplicationListener<CustomEvent> {
  2. public void onApplicationEvent(CustomEvent event) {
  3. System.out.println(event.toString());
  4. }
  5. }

MainApp.java

  1. public class MainApp {
  2. public static void main(String[] args) {
  3. ConfigurableApplicationContext context =
  4. new ClassPathXmlApplicationContext("Beans.xml");
  5. CustomEventPublisher cvp =
  6. (CustomEventPublisher) context.getBean("customEventPublisher");
  7. cvp.publish();
  8. cvp.publish();
  9. }
  10. }

Beans.xml

  1. <beans xmlns = "http://www.springframework.org/schema/beans"
  2. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation = "http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  5. <bean id = "customEventHandler" class = "com.tutorialspoint.CustomEventHandler"/>
  6. <bean id = "customEventPublisher" class = "com.tutorialspoint.CustomEventPublisher"/>
  7. </beans>

注意不要忘记添加customEventHandler,虽然在主函数中没有直接使用,但是context需要检查实现了ApplicationListener的
bean,所以需要在xml文件中,添加该bean。

输出:

  1. my Custom Event
  2. my Custom Event
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号