专栏首页后端技术学习设计模式之观察者模式

设计模式之观察者模式

观察者模式

观察者模式:Observer Pattern,又称为发布-订阅(Publish-Subscribe)模式,对象之间存在一对多或者一对一依赖,当一个对象改变状态,依赖它的对象会收到通知并自动更新。MQ其实就属于一致观察者模式,发布者发布消息,订阅者获取消息,订阅了就能收到信息,没有订阅的就收不到信息。其核心是将观察者和被观察者进行解耦,以类似消息/广播发送的机制联动两者,使被观察者的变动能通知到感兴趣的观察者们,从而做出响应。

特点

其优点是可以建立一对多的触发机制,但如果观察者的数量过多,则通知会耗时较长。

应用场景

比如初始化时将变动不是很大的数据加载进行,比如常用的配置信息、常用的字典数据、城市地区数据、相关商品分类、药品分类等.

扩展配置的方式

在ApplicationContext这个上下文中,进行初始化配置扩展的方法:

实现InitializingBean接口重写afterPropertiesSet方法
使用@PostConstruct
实现BeanPostProcessor后置处理器
采用FactoryBean
实现ApplicationListener接口

观察者模式例子

下面我们来看观察者模式中的一种:实现ApplicationListener接口

public class SpringApplicationTest {

    /*****
     * 1.创建ApplicationContext
     * 2.执行相关事件ApplicationEvent:容器初始化完成
     * 3.监听事件执行信息ApplicationListener
     */
    public static void main(String[] args) {
        //1.创建ApplicationContext,会立即加载Bean,加载完毕就相当于执行了事件
        ApplicationContext act = new ClassPathXmlApplicationContext("spring-event.xml");
    }

}

配置spring-event.xml信息:

<bean class="com.study.spring.ApplicationContextListener"></bean>

执行初始化,将数据放入到内存中,方便使用

public class ApplicationEventListener implements ApplicationListener<ContextRefreshedEvent> {

    //发生指定事件后会触发该方法执行
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ApplicationContext act = contextRefreshedEvent.getApplicationContext();
        System.out.println("监听到容器初始化完成!");

        //启动->从数据库加载对应的数据(省市区)、药品相关数据->填充Redis

    }
}

执行的结果:

从执行的结果上看,其确实是执行了监听方法,是不是感觉很神奇。

调用过程:

下面我们来看看实现的过程:首先通过ClassPathXmlApplicationContext将配置进行读取、转换,变成bean,此时会将bean放入到:BeanDefintionMap中,初始化initApplicationEventMulticaster,调用注册监听方法 registerListeners(),将监听bean进行注册。然后调用 finishRefresh(),此时会将我们需要发布事件publishEvent进行执行,调用publishEvent,最终会调用doInvokerListenr,调用我们写的onApplicationEvent。其实在Spring中有很多这样类似的观察者模式的代码,还有ContextLoadListener、ServletContextListener等。

流程如图

Spring的ApplicationEvent流程

下面看代码:

找到:

执行刷新操作,也是ClassPathXmlApplicationContext中最重要的方法。

@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      // Prepare this context for refreshing.
       //准备需要刷新的上下文
      prepareRefresh();

      // Tell the subclass to refresh the internal bean factory.
       //告诉子类去刷新bean工厂
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Prepare the bean factory for use in this context.
       //准备bean工厂
      prepareBeanFactory(beanFactory);

      try {
         // Allows post-processing of the bean factory in context subclasses.
         //后置处理器bean工厂 ,运行后置处理bean工厂在子类上下文中
         postProcessBeanFactory(beanFactory);

         // Invoke factory processors registered as beans in the context.
          //激活工厂处理器注册成bean在上下文中
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
          //注册拦截Bean创建的Bean处理器
         registerBeanPostProcessors(beanFactory);

         // Initialize message source for this context.
          //为此上下文初始化消息源
         initMessageSource();

         // Initialize event multicaster for this context.
          //为上下文初始化事件广播
         initApplicationEventMulticaster();

         // Initialize other special beans in specific context subclasses.
         //在特定上下文子类中初始化其他特殊bean。
         onRefresh();

         // Check for listener beans and register them.
          //检查监听器bean和注册它们
         registerListeners();

         // Instantiate all remaining (non-lazy-init) singletons.
          //实例化所有剩余的(非延迟初始化)单例。
         finishBeanFactoryInitialization(beanFactory);

         // Last step: publish corresponding event.
          //最后一步:发布相应的事件。
         finishRefresh();
      }

      catch (BeansException ex) {
         if (logger.isWarnEnabled()) {
            logger.warn("Exception encountered during context initialization - " +
                  "cancelling refresh attempt: " + ex);
         }

         // Destroy already created singletons to avoid dangling resources.
         destroyBeans();

         // Reset 'active' flag.
         cancelRefresh(ex);

         // Propagate exception to caller.
         throw ex;
      }

      finally {
         // Reset common introspection caches in Spring's core, since we
         // might not ever need metadata for singleton beans anymore...
          //最终放入缓存中
         resetCommonCaches();
      }
   }
}

在prepareRefresh中:此时会创建this.earlyApplicationListeners:ApplicationListener对象 数据结构:LinkedHashedSet。

/**
 * Prepare this context for refreshing, setting its startup date and
 * active flag as well as performing any initialization of property sources.
 */
protected void prepareRefresh() {
   // Initialize any placeholder property sources in the context environment.
   initPropertySources();

   // Store pre-refresh ApplicationListeners...
    //存储前置刷新的ApplicationListener,可以看到其数据结构是LinkedHashSet
   if (this.earlyApplicationListeners == null) {
      this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
   }
   else {
      // Reset local application listeners to pre-refresh state.
      this.applicationListeners.clear();
      this.applicationListeners.addAll(this.earlyApplicationListeners);
   }

   // Allow for the collection of early ApplicationEvents,
   // to be published once the multicaster is available...
    //ApplicationListener,可以看到其数据结构是LinkedHashSet
   this.earlyApplicationEvents = new LinkedHashSet<>();
}

执行初始化initApplicationEventMulticaster(),此时会将我们定义的监听进行注册:此时BeanDefintionMap中已经放入

com.study.spring.ApplicationContextListener。

/**
 * Initialize the ApplicationEventMulticaster.
 * Uses SimpleApplicationEventMulticaster if none defined in the context.
 * @see org.springframework.context.event.SimpleApplicationEventMulticaster
 */
protected void initApplicationEventMulticaster() {
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
      this.applicationEventMulticaster =
            beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
      if (logger.isTraceEnabled()) {
         logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
      }
   }
   else {
      this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
      beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
      if (logger.isTraceEnabled()) {
         logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
               "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
      }
   }
}

SimpleApplicationEventMulticaster

/**
     * Create a new SimpleApplicationEventMulticaster for the given BeanFactory.
     */
    public SimpleApplicationEventMulticaster(BeanFactory beanFactory) {
        setBeanFactory(beanFactory);
    }


@Override
public void setBeanFactory(BeanFactory beanFactory) {
   this.beanFactory = beanFactory;
   if (beanFactory instanceof ConfigurableBeanFactory) {
      ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
      if (this.beanClassLoader == null) {
         this.beanClassLoader = cbf.getBeanClassLoader();
      }
      this.retrievalMutex = cbf.getSingletonMutex();
   }
}

进行事件监听注册:

/**
     * Add beans that implement ApplicationListener as listeners.
     * Doesn't affect other listeners, which can be added without being beans.
     */
    protected void registerListeners() {
        // Register statically specified listeners first.
        for (ApplicationListener<?> listener : getApplicationListeners()) {
            getApplicationEventMulticaster().addApplicationListener(listener);
        }

        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let post-processors apply to them!
        String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
        for (String listenerBeanName : listenerBeanNames) {
            getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
        }

        // Publish early application events now that we finally have a multicaster...
        Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
        this.earlyApplicationEvents = null;
        if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
            for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
                getApplicationEventMulticaster().multicastEvent(earlyEvent);
            }
        }
    }

在执行FinshRefresh中会发布event:

/**
 * Publish the given event to all listeners.
 * <p>Note: Listeners get initialized after the MessageSource, to be able
 * to access it within listener implementations. Thus, MessageSource
 * implementations cannot publish events.
 * @param event the event to publish (may be application-specific or a
 * standard framework event)
 */
@Override
public void publishEvent(ApplicationEvent event) {
   publishEvent(event, null);
}

执行发布Event:

/**
 * Publish the given event to all listeners.
 * @param event the event to publish (may be an {@link ApplicationEvent}
 * or a payload object to be turned into a {@link PayloadApplicationEvent})
 * @param eventType the resolved event type, if known
 * @since 4.2
 */
protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
   Assert.notNull(event, "Event must not be null");

   // Decorate event as an ApplicationEvent if necessary
   ApplicationEvent applicationEvent;
   if (event instanceof ApplicationEvent) {
      applicationEvent = (ApplicationEvent) event;
   }
   else {
      applicationEvent = new PayloadApplicationEvent<>(this, event);
      if (eventType == null) {
         eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
      }
   }

   // Multicast right now if possible - or lazily once the multicaster is initialized
   if (this.earlyApplicationEvents != null) {
      this.earlyApplicationEvents.add(applicationEvent);
   }
   else {
       //重要:拿到通过ApplicationEventMulticaster调用multicastEvent方法
      getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
   }

   // Publish event via parent context as well...
   if (this.parent != null) {
      if (this.parent instanceof AbstractApplicationContext) {
         ((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
      }
      else {
         this.parent.publishEvent(event);
      }
   }
}

查看multicastEvent:

@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
   ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
   Executor executor = getTaskExecutor();
   for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
      if (executor != null) {
         executor.execute(() -> invokeListener(listener, event));
      }
      else {
         invokeListener(listener, event);
      }
   }
}

查看invokerListener:

/**
 * Invoke the given listener with the given event.
 * @param listener the ApplicationListener to invoke
 * @param event the current event to propagate
 * @since 4.1
 */
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
   ErrorHandler errorHandler = getErrorHandler();
   if (errorHandler != null) {
      try {
         doInvokeListener(listener, event);
      }
      catch (Throwable err) {
         errorHandler.handleError(err);
      }
   }
   else {
      doInvokeListener(listener, event);
   }
}

找到doInvokerListener:

private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
   try {
       //调用onApplicationEvent
      listener.onApplicationEvent(event);
   }
   catch (ClassCastException ex) {
      String msg = ex.getMessage();
      if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
         // Possibly a lambda-defined listener which we could not resolve the generic event type for
         // -> let's suppress the exception and just log a debug message.
         Log logger = LogFactory.getLog(getClass());
         if (logger.isTraceEnabled()) {
            logger.trace("Non-matching event type for listener: " + listener, ex);
         }
      }
      else {
         throw ex;
      }
   }
}

从而调用我们写的方法:

public class ApplicationContextListener implements ApplicationListener<ContextRefreshedEvent> {

    //监听  ContextRefreshedEvent:容器完成初始化加载事件
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("事件执行完成之后监听方法");
        ApplicationContext act = event.getApplicationContext();
        //初始化数据数据操作
    }
}

本文分享自微信公众号 - 后端技术学习(gh_9f5627e6cc61),作者:路行的亚洲

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-11-16

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • RocketMQ学习Broker流程、生产者和存储流程联系

    Broker作为代理,路由注册是通过Broker与nameServer的心跳功能实现的。除此之外,还联系了生产者和消费者、存储。因此可以知道Broker是非常重...

    路行的亚洲
  • pmq学习三-mq客户端启动的流程

    我们知道在RocketMQ中,服务端代表的是broker,而客户端才是我们的生产者和消费者。而pmq中,也是如此,服务端是broker,而客户端是生产者和消费者...

    路行的亚洲
  • Rocketmq学习一

    1.由于rocketmq需要依赖nameServer,类似于zookeeper。首先启动时,配置好NamesrvStartup的环境变量信息,也即rocketm...

    路行的亚洲
  • cssjshtml vue-echart 点击跳转传参和接收

    葫芦
  • Android自定义视图实现手指移动轨迹

    贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲线。一般的矢量图形软件通过它来精确画出曲线,贝兹曲线由线段与节...

    砸漏
  • 【Angular6+】事件绑定

    添加如下 onSelect() 方法,它会把模板中被点击的英雄赋值给组件的 selectedHero 属性。

    CloudCat
  • mongo实现自增id

    Mongo 的free schema,提供了灵活的数据结构,和快速开发的能力,但是也造成了松散的数据组织形式。比如说有些字段不允许为null的,需要符合一定格式...

    方丈的寺院
  • vue切换路由并且传递参数

    用户4344670
  • libevent源码深度剖析(五) libevent的核心:事件event

    (1)libevent源码深度剖析一 序 (2)libevent源码深度剖析二 Reactor模式 (3)libevent源码深度剖析三 libevent基...

    范蠡
  • eventEmitter3源码分析与学习

    事件监听在前端的开发过程中是一个很常见的情况。DOM上的事件监听方式,让我们看到了通过事件的方式来进行具体的业务逻辑的处理的便捷。

    黄Java

扫码关注云+社区

领取腾讯云代金券