本例子模拟一个给多个人发送内容(类似于报纸新闻)的例子。
1、定义事件
2、定义无序监听器
之所以说无序,类似于AOP机制,顺序是无法确定的。
2、在实现中需要判断event类型是ContentEvent才可以处理;
更简单的办法是通过泛型指定类型,如下所示
3、定义有序监听器
实现SmartApplicationListener接口即可。
supportsSourceType:支持的目标类型,只有支持的才调用onApplicationEvent;
4.1、配置文件
Java代码
4.2、测试类
1、定义事件
package com.xvshu.hello;
import org.springframework.context.ApplicationEvent;
public class ContentEvent extends ApplicationEvent {
public ContentEvent(final String content) {
super(content);
}
}
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;
}
}
supportsSourceType:支持的目标类型,只有支持的才调用onApplicationEvent;
getOrder:即顺序,越小优先级越高
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