Spring事件机制的简单例子

本例子模拟一个给多个人发送内容(类似于报纸新闻)的例子。
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的事件机制去写相关代码会非常简单。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值