java 代码
import orgspringframeworkcontextApplicationEvent;
import orgspringframeworkcontextApplicationListener;
public class MessageEventListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// TODO Autogenerated method stub
if(event instanceof MessageEvent){
MessageEvent msEvent = (MessageEvent)event;
Systemoutprintln(Received: + msEventgetMessage());
}
}
}
在运行期ApplicationContext会自动在当前的所有Bean中寻找ApplicationListener接口的实现并将其作为事件接收对象当ApplicationpublishEvent方法调用时所有的ApplicationListener接口实现都会被激发每个ApplicationListener可根据事件的类型判断是否是自己需要处理的事件如上面的ActionListener只处理ActionEvent事件
[] [] [] []