标签:
一个事件:
package com; import org.springframework.context.ApplicationEvent; public class AddEvent extends ApplicationEvent { public AddEvent(Object source) { super(source); } }
一个监听:
package com; import org.springframework.context.ApplicationListener; public interface AddListener extends ApplicationListener<AddEvent> { }
订阅者1
package com; import org.springframework.stereotype.Component; @Component public class AddWorker implements AddListener { @Override public void onApplicationEvent(AddEvent addEvent) { System.out.println(AddWorker.class.getSimpleName() + "收到消息:" + addEvent.toString()); } }
订阅2
package com; import org.springframework.stereotype.Component; @Component public class AddWorker2 implements AddListener { @Override public void onApplicationEvent(AddEvent addEvent) { System.out.println(AddWorker2.class.getSimpleName() + "2收到消息:" + addEvent.toString()); } }
订阅3
package com; import org.springframework.stereotype.Component; @Component public class AddWorker3 implements AddListener { @Override public void onApplicationEvent(AddEvent addEvent) { System.out.println(AddWorker3.class.getSimpleName() + "3收到消息:" + addEvent.toString()); } }
主题及事件发布者是ApplicatonContext:
applicationContext.publishEvent(new AddEvent(msg));
applicationContext.publishEvent
标签:
原文地址:http://www.cnblogs.com/softidea/p/5721347.html