Spring核心系列之容器事件

栏目: Java · 发布时间: 8年前

内容简介:Spring核心系列之容器事件

Hello,大家好,前面几篇Spring的文章把Spring容器这一块大致分享完了,容器的创建,容器里的Bean,后面一篇文章会好好的讲一讲AOP,这一篇来点小菜吃吃,讲个Spring的容器事件,这个知识不是很常用。为什么呢? 因为一般稍微大型一点的系统都是分布式的,不会采用局部项目中的通讯,要通讯也是采用MQ ,但既然写了Spring系列的博客,还是和大家分享分享。OK,老讨论,文章结构:

  1. Spring中的内置事件
  2. 如何在Spring中自定义事件

1. Spring中的内置事件

谈到事件,其实很多框架组件都自带事件机制,宏观上讲,JDK里的notify其实也算是事件,能够通知wait的线程。这里给大家来一张图,然后直接切入主题的讲Spring里的内置事件:

Spring核心系列之容器事件
  • 事件源,具体到Spring中就是ApplicationContext了。
  • 事件,内置事件或者自己定义实现(实现ApplicationEvent接口来实现)。
  • 事件广播器: Spring自己实现,我们不关心。
  • 事件监听器注册表: Spring自己实现,我们不关心。
  • 监听器: 开发者实现,注入到Spring容器中就OK了。(实现AoolicationListener接口来实现)

好了,其实非常简单,然后给一下Spring内部的Spring事件继承图:

Spring核心系列之容器事件
Spring内部所有的事件都是继承自ApplicationEvent,

内置的事件有ContextClosedEvent,ContextRefreshEvent,ContextStartedEvent,ContextStoppedEvent和ServletRequestHandledEvent.分别表示容器启动,关闭,刷新,中止的事件和一次请求服务完成的事件。

然后给一个例子演示内置事件:

public class ContextStopListener 
   implements ApplicationListener<ContextStoppedEvent>{
   public void onApplicationEvent(ContextStoppedEvent event) {
      System.out.println("ContextStoppedEvent Received");
   }
}

直接把这个类注入到Spring中成为Bean即可。容器关闭时,就会调用onApplicationEvent...

2. 如何在Spring中自定义事件

上面可以看到,内置的事件,我们只需要自定义一个Listener放入Spring容器即可。那么要是自定义的事件呢,来看一个例子:

public class ZdyEvent extends ApplicationEvent {

    private String whatsHasspend;

    public ZdyEvent(Object source,String whatsHasspend) {
        super(source);
        this.whatsHasspend=whatsHasspend;
    }

    public String getWhatsHasspend() {
        return whatsHasspend;
    }

    public void setWhatsHasspend(String whatsHasspend) {
        this.whatsHasspend = whatsHasspend;
    }
}

public class ZdyListener implements ApplicationListener<ZdyEvent> {
    @Override
    public void onApplicationEvent(ZdyEvent zdyEvent) {
        System.out.println("事件接收器接收到了Event:"+zdyEvent.getWhatsHasspend());
    }
}

然后我们的Main:

public static void main( String[] args )
    {

        ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
        ZdyEvent event =new ZdyEvent(ac,"我发送了一个事件:我吃了一个苹果..");
        ac.publishEvent(event);
    }

打印:

事件接收器接收到了Event:我发送了一个事件:我吃了一个苹果..

注意,定义完事件和时间监听器后,需要把事件监听器(ZdyListener)放入到容器当中。而我们的事件类(ZdyEvent)是不用放入到容器中的.

结语

好了,其实前面已经提到过,Spring的事件机制厉害是厉害,但只停留的当前工程的Spring模块中,首先不适合集群,其次不适合分布式。所以,说白了,是个花架子。大家仅供了解。无论是集群还是分布式的通讯,肯定是不会用这个东东,一般都是Mq,zk,kafka这类分布式组件,做过系统架构的应该比较清楚。本文也是顺带的提了提,大致说了说。 Over,Have a good day .


以上所述就是小编给大家介绍的《Spring核心系列之容器事件》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ajax Design Patterns

Ajax Design Patterns

Michael Mahemoff / O'Reilly Media / 2006-06-29 / USD 44.99

Ajax, or Asynchronous JavaScript and XML, exploded onto the scene in the spring of 2005 and remains the hottest story among web developers. With its rich combination of technologies, Ajax provides a s......一起来看看 《Ajax Design Patterns》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具