模板模式在框架源码中的体现

了解模板方式

Java模板模式的详解icon-default.png?t=M276https://blog.csdn.net/qq_43799161/article/details/123635463

Spring中的体现

我们看到容器上下文AbstractApplicationContext中refresh()方法。

@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.
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

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

      try {
         // Allows post-processing of the bean factory in context subclasses.
         postProcessBeanFactory(beanFactory);

         // Invoke factory processors registered as beans in the context.
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         registerBeanPostProcessors(beanFactory);

         // Initialize message source for this context.
         initMessageSource();

         // Initialize event multicaster for this context.
         initApplicationEventMulticaster();

         // Initialize other special beans in specific context subclasses.
         onRefresh();

         // Check for listener beans and register them.
         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();
      }
   }
}

整个容器的启动就是走这个流程,所以这里为扩展使用到模板模式,在AbstractApplicationContext类中refresh方法定义了一套容器启动的流程,具体的每个步骤都是定义成方法,可以让子类不同的容器实现子类想要的细节,但是整体还是这个流程。不过在Spring中跟我们的案例是有区别的,他是有默认实现的,而我们的项目是一定要子类去实现。好比单独使用Spring就使用默认的容器,就走他的默认实现。但是目前为了效率都是使用Spring boot来开发,我们知道Spring boot也是基于Spring开发的,所以可以想象到Spring boot肯定是自己实现了Spring原有的容器,也就是Spring boot将refresh()方法里面的步骤给选择性重写(看需求,有默认实现)。猜测的验证如下:

AQS中的体现

 相信大家并不陌生AQS(AbstractQueuedSynchronizer) ,在java.util.current包下的技术都是基于到AQS这个技术,而AQS内部也就是维护了一个状态值,一个队列,使用CAS操作来保证并发原子性,这里并不对AQS过多的介绍。那么它哪里有模板模式呢?

我们看到ReentrantLock中lock上锁的操作。

 这仅仅只是AQS中模板方法的一处,在AQS中基本是处处都是模板方法!

总结

模板模式还是比较简单的,特征比较明显,大家学习完可以可以来看各种框架源码中对模板方式的使用。

最后,大家有不懂的地方可以在评论区留言。如果此文章对您有一定的帮助,希望能点赞+关注+收藏,您的支持是我最大的动力,后续一直再更新各种框架的使用和源码解读~!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员李哈

创作不易,希望能给与支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值