- Spring使用Application Event给bean之间的消息通讯提供了手段
- 应按照如下部分实现bean之间的消息通讯
- 继承ApplicationEvent类实现自己的事件
- 实现继承ApplicationListener接口实现监听事件
- 使用ApplicationContext发布消息
示例中的通讯两个bean分别为DemoListener和Main
package com.wisely.event;import org.springframework.context.ApplicationEvent;public class DemoEvent extends ApplicationEvent{ private static final long serialVersionUID = 1L; private String msg; public DemoEvent(Object source,String msg) { super(source); this.msg = msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}
package com.wisely.event;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component;@Componentpublic class DemoListener implements ApplicationListener{ public void onApplicationEvent(DemoEvent event) { String msg = ((DemoEvent) event).getMsg(); System.out.println("我监听到了pulisher发布的message为:"+msg); }}
package com.wisely.event;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.stereotype.Component;@Componentpublic class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wisely.event"); Main main =context.getBean(Main.class); main.pulish(context); context.close(); } public void pulish(AnnotationConfigApplicationContext context){ context.publishEvent(new DemoEvent(this, "22")); }}
输出结果
我监听到了pulisher发布的message为:22