spring源码解析bean初始化后的实现和其他入口

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

前言

本文转自“天河聊技术”微信公众号

本次主要介绍bean初始化、依赖注入后续的实现和其他入口

 

正文

找到这个方法

org.springframework.context.support.AbstractApplicationContext#refresh

这一行

finishRefresh();

进入

org.springframework.context.support.AbstractApplicationContext#finishRefresh

protected void finishRefresh() {
   // Clear context-level resource caches (such as ASM metadata from scanning).清除上下文级别的资源缓存(例如扫描的ASM元数据)。
   clearResourceCaches();

   // Initialize lifecycle processor for this context.为这个上下文初始化生命周期处理器。
   initLifecycleProcessor();

   // Propagate refresh to lifecycle processor first.首先传播刷新到生命周期处理器。
   getLifecycleProcessor().onRefresh();

   // Publish the final event.发布最后的事件。
   publishEvent(new ContextRefreshedEvent(this));

   // Participate in LiveBeansView MBean, if active.
   LiveBeansView.registerApplicationContext(this);
}

这一行

initLifecycleProcessor();

初始化生命周期处理器

进入

org.springframework.context.support.AbstractApplicationContext#initLifecycleProcessor初始化LifecycleProcessor。如果上下文没有定义,则使用DefaultLifecycleProcessor

protected void initLifecycleProcessor() {
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
      this.lifecycleProcessor =
            beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
      if (logger.isDebugEnabled()) {
         logger.debug("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
      }
   }
   else {
      DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
      defaultProcessor.setBeanFactory(beanFactory);
      this.lifecycleProcessor = defaultProcessor;
      beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
      if (logger.isDebugEnabled()) {
         logger.debug("Unable to locate LifecycleProcessor with name '" +
               LIFECYCLE_PROCESSOR_BEAN_NAME +
               "': using default [" + this.lifecycleProcessor + "]");
      }
   }
}

回到这个方法

org.springframework.context.support.AbstractApplicationContext#refresh

这一行

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;
}

异常处理

返回

org.springframework.web.context.ContextLoader#initWebApplicationContext这一行

//        把spring上下文放在servlet上下文上
         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

返回

@Override
public void contextInitialized(ServletContextEvent event) {
   initWebApplicationContext(event.getServletContext());
}

 

我跟踪的顺序是跟着servlet容器启动这个入口看来跟踪的,还有两个入口给大家介绍下

ClassPathXmlApplicationContext

这个方法

org.springframework.context.support.ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String)

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
   this(new String[] {configLocation}, true, null);
}

参数是传入一个bean定义配置文件

进入org.springframework.context.support.ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String[], boolean, org.springframework.context.ApplicationContext)这个方法

public ClassPathXmlApplicationContext(
      String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
      throws BeansException {

   super(parent);
   setConfigLocations(configLocations);
   if (refresh) {
      refresh();
   }
}

进入org.springframework.context.support.AbstractApplicationContext#refresh这个方法,剩下的以往的文章中都介绍过了

 

FileSystemXmlApplicationContext

找到org.springframework.context.support.FileSystemXmlApplicationContext#FileSystemXmlApplicationContext(java.lang.String)这个方法

public FileSystemXmlApplicationContext(String configLocation) throws BeansException {
   this(new String[] {configLocation}, true, null);
}

参数也是传入一个bean定义配置文件

进入

org.springframework.context.support.FileSystemXmlApplicationContext#FileSystemXmlApplicationContext(java.lang.String[], boolean, org.springframework.context.ApplicationContext)

public FileSystemXmlApplicationContext(
      String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
      throws BeansException {

   super(parent);
   setConfigLocations(configLocations);
   if (refresh) {
      refresh();
   }
}

进入org.springframework.context.support.AbstractApplicationContext#refresh这个方法,剩下的以往的文章都介绍过了,applicationContext的生涯新实现就解析完了

 

最后

 

本次介绍到这里,以上内容仅供参考。

转载于:https://my.oschina.net/u/3775437/blog/1813311

你可能感兴趣的:(java,python)