扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
import org.springframework.context.ApplicationEvent; /** * 定义事件信息 * @author new * */ public class MessageEvent extends ApplicationEvent { private String message; public void setMessage(String message){ this.message = message; } public String getMessage(){ return message; } public MessageEvent(Object source, String message) { super(source); this.message = message; // TODO Auto-generated constructor stub } private static final long serialVersionUID = 1L; } |
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Publisher implements ApplicationContextAware { private ApplicationContext context; @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { // TODO Auto-generated method stub this.context = arg0; } public void publish(String message){ context.publishEvent(new MessageEvent(this,message)); } public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml"); Publisher pub = (Publisher) ctx.getBean("publisher"); pub.publish("Hello World!"); pub.publish("The quick brown fox jumped over the lazy dog"); } } |
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; public class MessageEventListener implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { // TODO Auto-generated method stub if(event instanceof MessageEvent){ MessageEvent msEvent = (MessageEvent)event; System.out.println("Received: " + msEvent.getMessage()); } } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者