事件监听机制(四)从Java事件监听到Spring事件监听

ゝ一纸荒年。 2022-03-25 10:26 538阅读 0赞

事件监听机制(四)从Java事件监听到Spring事件监听

java 事件监听

1. 发布订阅模式

java.util.Observable发布者

  1. public class Observable {
  2. private boolean changed = false;
  3. private Vector<Observer> obs;
  4. /** Construct an Observable with zero Observers. */
  5. public Observable() {
  6. obs = new Vector<>();
  7. }
  8. /** * 为该事件监听器添加事件监听者 */
  9. public synchronized void addObserver(Observer o) {
  10. if (o == null)
  11. throw new NullPointerException();
  12. if (!obs.contains(o)) {
  13. obs.addElement(o);
  14. }
  15. }
  16. /** *删除事件监听者 */
  17. public synchronized void deleteObserver(Observer o) {
  18. obs.removeElement(o);
  19. }
  20. /** * 事件通知 */
  21. public void notifyObservers() {
  22. notifyObservers(null);
  23. }
  24. /** *有参数的事件通知 */
  25. public void notifyObservers(Object arg) {
  26. /* * a temporary array buffer, used as a snapshot of the state of * current Observers. */
  27. Object[] arrLocal;
  28. synchronized (this) {
  29. /* *只有事件变化时监听者才会工作 */
  30. if (!changed)
  31. return;
  32. arrLocal = obs.toArray();
  33. clearChanged();
  34. }
  35. for (int i = arrLocal.length-1; i>=0; i--)
  36. ((Observer)arrLocal[i]).update(this, arg);
  37. }
  38. /** * 删除所有的监听者 */
  39. public synchronized void deleteObservers() {
  40. obs.removeAllElements();
  41. }
  42. /** * 事件变化通知方法,由于是protected 修饰所以在实现的过程中我们普遍采用子类继承回调的方式来完成通知操作 */
  43. protected synchronized void setChanged() {
  44. changed = true;
  45. }
  46. /** * 擦除通知信息 */
  47. protected synchronized void clearChanged() {
  48. changed = false;
  49. }
  50. /** * 判断事件是否有改变 */
  51. public synchronized boolean hasChanged() {
  52. return changed;
  53. }
  54. /** * 事件监听者的数目 */
  55. public synchronized int countObservers() {
  56. return obs.size();
  57. }
  58. }

java.util.Observer订阅者

  1. public interface Observer {
  2. /** *当事件变化时执行该方法 */
  3. void update(Observable o, Object arg);
  4. }

2.事件/监听模式

java.util.EventObject事件对象

  1. public class EventObject implements java.io.Serializable {
  2. private static final long serialVersionUID = 5516075349620653480L;
  3. /** * 事件源 */
  4. protected transient Object source;
  5. /** * 构造器初始化 */
  6. public EventObject(Object source) {
  7. if (source == null)
  8. throw new IllegalArgumentException("null source");
  9. this.source = source;
  10. }
  11. /** *事件到达时,获取Source */
  12. public Object getSource() {
  13. return source;
  14. }
  15. /** * 返回当前的事件对象 */
  16. public String toString() {
  17. return getClass().getName() + "[source=" + source + "]";
  18. }
  19. }

事件对象总是关联着事件源

java.util.EventListener事件监听接口(标记接口)

  1. public interface EventListener {
  2. }

该接口主要是作为标记接口,在Spring Spring Cloud中均有相应的实现:
在这里插入图片描述

Spring 事件/监听

org.springframework.context.ApplicationEvent

org.springframework.context.ApplicationListener

org.springframework.boot.context.config.ConfigFileApplicationListener管理配置文件如:application.properties application.yaml

java SPI

java.util.ServiceLoader

Spring SPI:

  1. # Application Listeners
  2. org.springframework.context.ApplicationListener=\
  3. org.springframework.boot.ClearCachesApplicationListener,\
  4. org.springframework.boot.builder.ParentContextCloserApplicationListener,\
  5. org.springframework.boot.context.FileEncodingApplicationListener,\
  6. org.springframework.boot.context.config.AnsiOutputApplicationListener,\
  7. org.springframework.boot.context.config.ConfigFileApplicationListener,\
  8. org.springframework.boot.context.config.DelegatingApplicationListener,\
  9. org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
  10. org.springframework.boot.context.logging.LoggingApplicationListener,\
  11. org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener

3.Spring监听模式

  1. @FunctionalInterface
  2. public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
  3. /** * 处理一个应用事件 * @param event the event to respond to */
  4. void onApplicationEvent(E event);
  5. }

如何控制顺序:
实现Ordered以及标记Order

在Spring中,数值越小越优先

更多文章

事件监听机制(一)Java事件监听
https://blog.csdn.net/shang\_xs/article/details/87911756
事件监听机制(二)Spring事件监听
https://blog.csdn.net/shang\_xs/article/details/88048545
事件监听机制(三)Spring Cloud Bus流程分析
https://blog.csdn.net/shang\_xs/article/details/88050196
事件监听机制(四)从Java事件监听到Spring事件监听
https://blog.csdn.net/shang\_xs/article/details/86560994
事件监听机制(五)再话Jdk事件监听到Spring框架事件监听
https://blog.csdn.net/shang\_xs/article/details/119794917

发表评论

表情:
评论列表 (有 0 条评论,538人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring事件监听机制

    前言 Spring中的事件机制其实就是设计模式中的观察者模式,主要由以下角色构成: 1. 事件 2. 事件监听器(监听并处理事件) 3. 事件发布者(发布事件...

    相关 事件监听机制

    ![这里写图片描述][20160420164724788]   Java中的事件监听是整个Java消息传递的基础和关键。牵涉到三类对象:事件源(Event S