Spring事件机制的简单例子

来源:互联网 发布:暖通空调设计常用数据 编辑:程序博客网 时间:2024/05/29 11:11

本例子模拟一个给多个人发送内容(类似于报纸新闻)的例子。
1、定义事件

package com.xvshu.hello;  import org.springframework.context.ApplicationEvent;  public class ContentEvent extends ApplicationEvent {      public ContentEvent(final String content) {          super(content);      }  }  



非常简单,如果用户发送内容,只需要通过构造器传入内容,然后通过getSource即可获取。
 
2、定义无序监听器
之所以说无序,类似于AOP机制,顺序是无法确定的。

package com.xvshu.hello;  import org.springframework.context.ApplicationEvent;  import org.springframework.context.ApplicationListener;  import org.springframework.stereotype.Component;  @Component  public class UserOneListener implements ApplicationListener<ApplicationEvent> {      @Override      public void onApplicationEvent(final ApplicationEvent event) {          if(event instanceof ContentEvent) {              System.out.println("[UserOneListener]msg:" + event.getSource());          }      }  }


  

1、使用@Compoent注册Bean即可;
2、在实现中需要判断event类型是ContentEvent才可以处理;
 
更简单的办法是通过泛型指定类型,如下所示
package com.xvshu.hello;  import org.springframework.context.ApplicationListener;  import org.springframework.stereotype.Component;  @Component  public class UserTwoListener implements ApplicationListener<ContentEvent> {      @Override      public void onApplicationEvent(final ContentEvent event) {          System.out.println("[UserTwoListener]msg:" + event.getSource());      }  } 


 
3、定义有序监听器 
实现SmartApplicationListener接口即可。

package com.xvshu.hello;  import org.springframework.context.ApplicationEvent;  import org.springframework.context.event.SmartApplicationListener;  import org.springframework.stereotype.Component;    @Component  public class UserThreeListener implements SmartApplicationListener {        @Override      public boolean supportsEventType(final Class<? extends ApplicationEvent> eventType) {          return eventType == ContentEvent.class;      }      @Override      public boolean supportsSourceType(final Class<?> sourceType) {          return sourceType == String.class;      }      @Override      public void onApplicationEvent(final ApplicationEvent event) {          System.out.println("UserThreeListener:" + event.getSource());      }      @Override      public int getOrder() {          return 1;      }  }  


package com.xvshu.hello;  import org.springframework.context.ApplicationEvent;  import org.springframework.context.event.SmartApplicationListener;  import org.springframework.stereotype.Component;    @Component  public class UserFourListener implements SmartApplicationListener {        @Override      public boolean supportsEventType(final Class<? extends ApplicationEvent> eventType) {          return eventType == ContentEvent.class;      }        @Override      public boolean supportsSourceType(final Class<?> sourceType) {          return sourceType == String.class;      }        @Override      public void onApplicationEvent(final ApplicationEvent event) {          System.out.println("[UserFourListener]msg:" + event.getSource());      }        @Override      public int getOrder() {          return 2;      }  }  




supportsEventType:用于指定支持的事件类型,只有支持的才调用onApplicationEvent;
supportsSourceType:支持的目标类型,只有支持的才调用onApplicationEvent;

getOrder:即顺序,越小优先级越高


4、测试 
4.1、配置文件
Java代码

<context:component-scan base-package="com.xvshu"/>  


 

就一句话,自动扫描注解Bean。

 
4.2、测试类

package com.xvshu;  import com.xvshu.hello.ContentEvent;  import org.junit.Test;  import org.junit.runner.RunWith;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.context.ApplicationContext;  import org.springframework.test.context.ContextConfiguration;  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;    @RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration(locations={"classpath:spring-config-hello.xml"})  public class HelloIT {        @Autowired      private ApplicationContext applicationContext;      @Test      public void testPublishEvent() {          applicationContext.publishEvent(new ContentEvent("news is  update"));      }    }  



接着会输出:
[UserFourListener]msg:news is updat[UserThreeListener]msg:news is update [UserTwoListener]msg:news is updat [UserOneListener]msg:news is updat 



一个简单的测试例子就演示完毕,而且我们使用spring的事件机制去写相关代码会非常简单。

0 0
原创粉丝点击