Spring的refresh()方法研究学习,--prepareRefresh

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) {
    // Destroy already created singletons to avoid dangling resources.
    destroyBeans();
    // Reset 'active' flag.
    cancelRefresh(ex);
    // Propagate exception to caller.
    throw ex;
   }
  }
 }
  1. synchronized (this.startupShutdownMonitor);确保了同一时间startupShutdownMonitor的状态不发生改变。

  2. prepareRefresh();方法代码如下

/**
  * Prepare this context for refreshing, setting its startup date and
  * active flag as well as performing any initialization of property sources.
  */
 protected void prepareRefresh() {
  this.startupDate = System.currentTimeMillis();
  synchronized (this.activeMonitor) {
   this.active = true;
  }
  if (logger.isInfoEnabled()) {
   logger.info("Refreshing " + this);
  }
  // Initialize any placeholder property sources in the context environment
  initPropertySources();
  // Validate that all properties marked as required are resolvable
  // see ConfigurablePropertyResolver#setRequiredProperties
  this.environment.validateRequiredProperties();
 }

 

 记录日期,设置活动状态,保证activeMonitor的状态不发生改变,同一时间只允许一次实例化,打印Refeshing+本方法信息,调用initPropertySources,但该方法没有实现任何操作等待子类实现,调用ConfigurableEnvironment的validateRequiredProperties();该方法属于抽象方法,位于ConfigurablePropertyResolver接口内,等待子类实现该方法。

 

从上面这个方法可以管中窥豹,Spring框架的技巧之一就是接口编程,全部都是接口方法,所有的功能组装都是运行期编译,维护一个样子,但还未充实它。

 

转载于:https://my.oschina.net/stuspring/blog/470121

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值