spring源碼分析之啟動流程


 

 

spring源碼分析

 

1、 spring源碼中組件介紹:

  

2、spring啟動工廠創建和實例化bean的流程:

下圖是spring 容器的關系

 

 

分析是基於注解的方式,非解析spring.xml的方式

 

 

 

說明:

AnnotationConfigApplicationContext  是ApplicationContext的子類;也是BeanDefinitionRegistry的實現類,即 即時spring的ioc容器,也是bd的注冊器;

1、 創建AnnotationConfigApplicationContext  ,參數 AppConfig,調用AnnotationConfigApplicationContext  的構造方法,

  說明:Appconfig是基於注解的方式配置bean,功能和spring.xm相同;需要 @ComponentScan和@Configuration一起使用;假如不使用@Configuration注解,會生成多例bean;使用該注解,當引用bean的時候會從spring的單例工廠取出bean。

2、 AnnotationConfigApplicationContext(Class<?>... annotatedClasses){} 構造方法,主要有一下處理邏輯:

    

public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
        //1. 初始化bean定義讀取器和掃描器;
        // 2.調用父類GenericApplicationContext無參構造函數,初始化一個BeanFactory:DefaultListableBeanFactory
        // 3.注冊Spring自帶的bean,共5個  包括: ConfigurationClassPostProcessor
        //  AutowiredAnnotationBeanPostProcessor  CommonAnnotationBeanPostProcessor
        // EventListenerMethodProcessor  DefaultEventListenerFactory
        this();
        //  注冊AppConfig, ApplicationContext傳入的配置類
        //wl   此處只是注冊了 @Configuration 注釋的配置類,
        // wl   register方法 作用是 將對應的Bean生成BeanDefinition,放到spring容器中;容器是一個 map,key是beanName(xml<Bean>標簽里 id),value是BeanDefinition
        register(annotatedClasses);
        // wl  刷新容器,主要完成了 @Component 等相關注解注釋的bean的初始化工作,將bean加載到 spring容器管理
        refresh();// 啟動容器
    }

 

a、  this()方法

 

public AnnotationConfigApplicationContext() {
        // 注冊spring 自帶的bean  5個
        this.reader = new AnnotatedBeanDefinitionReader(this);
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }

    

this.reader = new AnnotatedBeanDefinitionReader(this);最終調用一下方法,注冊spring 自帶的5個后置處理器;
/**
     * Register all relevant annotation post processors in the given registry.
     * 注冊所有的后置處理器
     * @param registry the registry to operate on
     * @param source the configuration source element (already extracted)
     * that this registration was triggered from. May be {@code null}.
     * @return a Set of BeanDefinitionHolders, containing all bean definitions
     * that have actually been registered by this call
     */
    public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
            BeanDefinitionRegistry registry, @Nullable Object source) {

        DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
        if (beanFactory != null) {
            if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
                //此處為一個比較器,應該是處理優先級的
                beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
            }
            if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
                beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
            }
        }

        Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);

        if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
            def.setSource(source);
            // 注冊 ConfigurationClassPostProcessor
            beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
            def.setSource(source);
            // 注冊 AutowiredAnnotationBeanPostProcessor
            beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
        if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
            def.setSource(source);
            // 注冊 CommonAnnotationBeanPostProcessor
            beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        // Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
        if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition();
            try {
                def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
                        AnnotationConfigUtils.class.getClassLoader()));
            }
            catch (ClassNotFoundException ex) {
                throw new IllegalStateException(
                        "Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
            }
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
            def.setSource(source);
            // 注冊 EventListenerMethodProcessor
            beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
        }

        if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
            def.setSource(source);
            // 注冊 DefaultEventListenerFactory
            beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
        }

        return beanDefs;
    }

  AnnotationConfigApplicationContext的無參構造方法,用來初始化定義讀取器和掃描器

  調用父類GenericApplicationContext無參構造函數,初始化一個BeanFactory:DefaultListableBeanFactory

  這里的 AnnotatedBeanDefinitionReader注冊了spring自帶的5個bean,分別為:

                ConfigurationClassPostProcessor

                AutowiredAnnotationBeanPostProcessor

                CommonAnnotationBeanPostProcessor

                EventListenerMethodProcessor

                DefaultEventListenerFactory

ClassPathBeanDefinitionScanner :一個bean定義掃描器,它檢測類路徑上的bean候選對象;        

 

b、 register(annotatedClasses)方法,方法參數可能為多個,

  b.1  循環遍歷,注冊AppConfig, ApplicationContext傳入的配置類

  b.2  方法內部  主要調用BeanDefinitionReaderUtils. registerBeanDefinition()方法,將配置類 轉換為對應的 BeanDefination,注冊到spring容器中

        

 

    /**
     * Register one or more annotated classes to be processed.
     * <p>Calls to {@code register} are idempotent; adding the same
     * annotated class more than once has no additional effect.
     * @param annotatedClasses one or more annotated classes,
     * e.g. {@link Configuration @Configuration} classes
     */
    public void register(Class<?>... annotatedClasses) {
        for (Class<?> annotatedClass : annotatedClasses) {
            registerBean(annotatedClass);
        }
    }

    /**
     * Register a bean from the given bean class, deriving its metadata from
     * class-declared annotations.
     * @param annotatedClass the class of the bean
     */
    public void registerBean(Class<?> annotatedClass) {
        doRegisterBean(annotatedClass, null, null, null);
    }


<T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name, @Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) { BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName); definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); // 注冊bean AppConfig BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry); /** * Register the given bean definition with the given bean factory. * @param definitionHolder the bean definition including name and aliases * @param registry the bean factory to register with * @throws BeanDefinitionStoreException if registration failed */ public static void registerBeanDefinition( BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) throws BeanDefinitionStoreException { // Register bean definition under primary name. // 根據beanName注冊 (包括 id name) String beanName = definitionHolder.getBeanName(); // 注冊beanDefiniton registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition()); // Register aliases for bean name, if any. String[] aliases = definitionHolder.getAliases(); if (aliases != null) { for (String alias : aliases) { registry.registerAlias(beanName, alias); } } } }

 

 

 

c、  refresh() 屬於 AbstractApplicationContext方法,spring啟動時最終會調用該refresh()方法,

  c.1  refresh()方法主要完成bean加載到spring容器的工作(非@Configuratiion bean修飾的配置bean) 

@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.
      // 獲得刷新的beanFactory
      // 對於AnnotationConfigApplicationContext,作用:
      // 1.調用org.springframework.context.support.GenericApplicationContext.refreshBeanFactory,
      // 只是指定了SerializationId
      // 2.直接返回beanFactory(不用創建,容器中已存在)

      //  對於ClassPathXmlApplicationContext,作用:
      // 1.調用AbstractRefreshableApplicationContext.refreshBeanFactory
      // 2.如果存在beanFactory,先銷毀單例bean,關閉beanFactory,再創建beanFactory
      // 3.注冊傳入的spring的xml配置文件中配置的bean,注冊到beanFactory
      // 4.將beanFactory賦值給容器,返回beanFactory
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Prepare the bean factory for use in this context.
      // 准備bean工廠: 指定beanFactory的類加載器, 添加后置處理器,注冊缺省環境bean等
      // beanFactory添加了2個后置處理器 ApplicationContextAwareProcessor, ApplicationListenerDetector (new )
      prepareBeanFactory(beanFactory);

      try {
         // Allows post-processing of the bean factory in context subclasses.
         // 空方法
         // 允許在上下文的子類中對beanFactory進行后處理
         // 比如 AbstractRefreshableWebApplicationContext.postProcessBeanFactory
         postProcessBeanFactory(beanFactory);

         // Invoke factory processors registered as beans in the context.
         // 1.通過beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)
         //   拿到ConfigurationClassPostProcessor
         // 2.通過ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry,注冊所有注解配置的bean
         // 注冊的順序: @ComponentScan>實現ImportSelector>方法bean>@ImportResource("spring.xml")
         //  > 實現 ImportBeanDefinitionRegistrar  (相對的順序,都在同一個配置類上配置)
         // 3. 調用ConfigurationClassPostProcessor#postProcessBeanFactory
         //  增強@Configuration修飾的配置類  AppConfig--->AppConfig$$EnhancerBySpringCGLIB
         // (可以處理內部方法bean之間的調用,防止多例)
         //  添加了后置處理器 ConfigurationClassPostProcessor.ImportAwareBeanPostProcessor (new)
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         // 注冊攔截bean創建的后置處理器:
         // 1.添加Spring自身的:  BeanPostProcessorChecker (new)  以及注冊了beanDefinition的兩個
         //  CommonAnnotationBeanPostProcessor AutowiredAnnotationBeanPostProcessor
         //  重新添加ApplicationListenerDetector(new ) ,刪除舊的,移到處理器鏈末尾
         // 2.用戶自定義的后置處理器
         // 注冊了beanDefinition的會通過 beanFactory.getBean(ppName, BeanPostProcessor.class) 獲取后置處理器
         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.
         // 實例化所有剩余的(非懶加載)單例,此處才是真正地將 singleton類型 bean初始化spring的 單例bean對象池 singleObjects中。。。。
      //singleObjects 是一個map <beanName,singleBean>
 finishBeanFactoryInitialization(beanFactory);

         // Last step: publish corresponding event.   
//基於觀察者模式,事件多播器將 publish 到 事件多波器的事件 通知到 對應的 listener
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(); } } }

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM