Spring源碼解析 – AnnotationConfigApplicationContext容器建立過程

Spring在BeanFactory基礎上提供了一些列具體容器的實現,其中AnnotationConfigApplicationContext是一個用來管理註解bean的容器,從AnnotationConfigApplicationContext的實現結構圖中能夠看出:java

  • AnnotationConfigApplicationContext繼承GenericApplicationContext這個通用應用上下文,GenericApplicationContext內部定義了一個DefaultListableBeanFactory實例,GenericApplicationContext實現了BeanDefinitionRegistry接口,因此能夠經過AnnotationConfigApplicationContext實例註冊bean defintion,而後調用refresh()方法來初始化上下文。
  • AnnotationConfigApplicationContext繼承AbstractApplicationContext,AbstractApplicationContext提供了ApplicationContext的抽象實現。

下面經過一個示例分析AnnotationConfigApplicationContext的初始化過程:spring

 AnnotationConfigApplicationContext applicationContext  = new AnnotationConfigApplicationContext(ExtensionConfig.class);

構造函數:bootstrap

1 //1. 初始化bean讀取器和掃描器;
2     //調用父類GenericApplicationContext無參構造函數,初始化一個BeanFactory: DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory()
3    this();
4 //2.註冊bean配置類
5    register(annotatedClasses);
6 //3.刷新上下文
7    refresh();
8

1. this() 初始化bean讀取器和掃描器

1 public AnnotationConfigApplicationContext() {
2     //在IOC容器中初始化一個 註解bean讀取器AnnotatedBeanDefinitionReader
3    this.reader = new AnnotatedBeanDefinitionReader(this);
4    //在IOC容器中初始化一個 按類路徑掃描註解bean的 掃描器
5    this.scanner = new ClassPathBeanDefinitionScanner(this);

GenericApplicationContext部分代碼:數據結構

 1 public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
 2 private final DefaultListableBeanFactory beanFactory;
 3 
 4 //初始化一個BeanFactory
 5 public GenericApplicationContext() {
 6       this.beanFactory = new DefaultListableBeanFactory();
 7     }
 8 
 9 10 }

2. register(annotatedClasses)

註冊bean配置類, AnnotationConfigApplicationContext容器經過AnnotatedBeanDefinitionReader的register方法實現註解bean的讀取,具體源碼以下:app

 AnnotationConfigApplicationContext.java中register方法異步

 1 //按指定bean配置類讀取bean
 2 public void register(Class<?>... annotatedClasses) {
 3    for (Class<?> annotatedClass : annotatedClasses) {
 4       registerBean(annotatedClass);
 5    }
 6 }
 7 
 8 public void registerBean(Class<?> annotatedClass) {
 9    doRegisterBean(annotatedClass, null, null, null);
10 }
11 
12 //核心實現邏輯
13 <T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
14       @Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
15     //將Bean配置類信息轉成容器中AnnotatedGenericBeanDefinition數據結構, AnnotatedGenericBeanDefinition繼承自BeanDefinition做用是定義一個bean的數據結構,下面的getMetadata能夠獲取到該bean上的註解信息
16    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
17     //@Conditional裝配條件判斷是否須要跳過註冊
18    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
19       return;
20    }
21    //@param instanceSupplier a callback for creating an instance of the bean
22    //設置回調
23    abd.setInstanceSupplier(instanceSupplier);
24    //解析bean做用域(單例或者原型),若是有@Scope註解,則解析@Scope,沒有則默認爲singleton 
25    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
26   //做用域寫回BeanDefinition數據結構, abd中缺損的狀況下爲空,將默認值singleton從新賦值到abd
27    abd.setScope(scopeMetadata.getScopeName());
28   //生成bean配置類beanName
29    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
30    //通用註解解析到abd結構中,主要是處理Lazy, primary DependsOn, Role ,Description這五個註解
31    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
32   //@param qualifiers specific qualifier annotations to consider, if any, in addition to qualifiers at the bean class level
33   // @Qualifier特殊限定符處理,
34    if (qualifiers != null) {
35       for (Class<? extends Annotation> qualifier : qualifiers) {
36          if (Primary.class == qualifier) {
37     // 若是配置@Primary註解,則設置當前Bean爲自動裝配autowire時首選bean
38             abd.setPrimary(true);
39          }
40   else if (Lazy.class == qualifier) {
41   //設置當前bean爲延遲加載
42             abd.setLazyInit(true);
43          }
44          else {
45       //其餘註解,則添加到abd結構中
46             abd.addQualifier(new AutowireCandidateQualifier(qualifier));
47          }
48       }
49    }
50   //自定義bean註冊,一般用在applicationContext建立後,手動向容器中一lambda表達式的方式註冊bean, 
51   //好比:applicationContext.registerBean(UserService.class, () -> new UserService());
52    for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
53     //自定義bean添加到BeanDefinition
54       customizer.customize(abd);
55    }
56    //根據beanName和bean定義信息封裝一個beanhold,heanhold其實就是一個 beanname和BeanDefinition的映射
57    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
58   //建立代理對象
59    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
60   // BeanDefinitionReaderUtils.registerBeanDefinition 內部經過DefaultListableBeanFactory.registerBeanDefinition(String beanName, BeanDefinition beanDefinition)按名稱將bean定義信息註冊到容器中,
61   // 實際上DefaultListableBeanFactory內部維護一個Map<String, BeanDefinition>類型變量beanDefinitionMap,用於保存注bean定義信息(beanname 和 beandefine映射)
62    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
63 }

register方法重點完成了bean配置類自己的解析和註冊,處理過程能夠分爲如下幾個步驟:ide

  1. 根據bean配置類,使用BeanDefinition解析Bean的定義信息,主要是一些註解信息
  2. Bean做用域的處理,默認缺乏@Scope註解,解析成單例
  3. 藉助AnnotationConfigUtils工具類解析通用註解
  4. 將bean定義信息已beanname,beandifine鍵值對的形式註冊到ioc容器中

3. refresh()刷新上下文

   refresh方法在AbstractApplicationContext容器中實現,refresh()方法的做用加載或者刷新當前的配置信息,若是已經存在spring容器,則先銷燬以前的容器,從新建立spring容器,載入bean定義,完成容器初始化工做,因此能夠看出AnnotationConfigApplicationContext容器是經過調用其父類AbstractApplicationContext的refresh()函數啓動整個IoC容器完成對Bean定義的載入。     函數

AbstractApplicationContext.java中refresh方法的實現代碼以下:工具

 1 public void refresh() throws BeansException, IllegalStateException {
 2    synchronized (this.startupShutdownMonitor) {
 3       //1.刷新前的預處理
 4       prepareRefresh();
 5 
 6       //2.獲取刷新後的內部Bean工廠
 7       ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
 8 
 9       //3.BeanFactory的預準備工做
10       prepareBeanFactory(beanFactory);
11 
12       try {
13          // BeanFactory準備工做完成後,能夠作一些後置處理工做,
14       // 4.空方法,用於在容器的子類中擴展
15          postProcessBeanFactory(beanFactory);
16 
17          // 5. 執行BeanFactoryPostProcessor的方法,BeanFactory的後置處理器,在BeanFactory標準初始化以後執行的
18          invokeBeanFactoryPostProcessors(beanFactory);
19 
20          // 6. 註冊BeanPostProcessor(Bean的後置處理器),用於攔截bean建立過程
21          registerBeanPostProcessors(beanFactory);
22 
23          // 7. 初始化MessageSource組件(作國際化功能;消息綁定,消息解析)
24          initMessageSource();
25 
26          // 8. 初始化事件派發器
27          initApplicationEventMulticaster();
28 
29          // 9.空方法,能夠用於子類實如今容器刷新時自定義邏輯
30          onRefresh();
31 
32          // 10. 註冊時間監聽器,將全部項目裏面的ApplicationListener註冊到容器中來
33          registerListeners();
34 
35          // 11. 初始化全部剩下的單實例bean,單例bean在初始化容器時建立,原型bean在獲取時(getbean)時建立
36          finishBeanFactoryInitialization(beanFactory);
37 
38          // 12. 完成BeanFactory的初始化建立工做,IOC容器就建立完成;
39          finishRefresh();
40       }
41 
42       catch (BeansException ex) {
43          if (logger.isWarnEnabled()) {
44             logger.warn("Exception encountered during context initialization - " +
45                   "cancelling refresh attempt: " + ex);
46          }
47 
48          // Destroy already created singletons to avoid dangling resources.
49          destroyBeans();
50 
51          // Reset 'active' flag.
52          cancelRefresh(ex);
53 
54          // Propagate exception to caller.
55          throw ex;
56       }
57 
58       finally {
59          // Reset common introspection caches in Spring's core, since we
60          // might not ever need metadata for singleton beans anymore...
61          resetCommonCaches();
62       }
63    }
64 }

具體分析refresh中的函數邏輯:post

1.   刷新預處理:

AbstractApplicationContext. prepareRefresh ()方法:

 1 protected void prepareRefresh() {
 2   //設置容器啓動時間
 3    this.startupDate = System.currentTimeMillis();
 4   //啓動標識
 5    this.closed.set(false);
 6    this.active.set(true);
 7 
 8    if (logger.isInfoEnabled()) {
 9       logger.info("Refreshing " + this);
10    }
11 
12    //空方法,用於子容器自定義個性化的屬性設置方法
13    initPropertySources();
14    //檢驗屬性的合法等
15    getEnvironment().validateRequiredProperties();
16 
17    //保存容器中的一些早期的事件
18    this.earlyApplicationEvents = new LinkedHashSet<>();
19 }

2. 獲取刷新後的內部Bean工廠,obtainFreshBeanFactory方法爲內部bean工廠從新生成id,並返回bean工廠

  AbstractApplicationContext. obtainFreshBeanFactory()方法

 1 protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
 2   //爲beanfactory生成惟一序列化id,beanfactory已經在GenericApplicationContext構造函數中初始化了,refreshBeanFactory的邏輯在AbstractApplicationContext的實現類GenericApplicationContext中
 3  refreshBeanFactory();
 4   //獲取beanfactory
 5    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
 6    if (logger.isDebugEnabled()) {
 7       logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
 8    }
 9    return beanFactory;
10 }

GenericApplicationContext.refreshBeanFactory()實現代碼

1 protected final void refreshBeanFactory() throws IllegalStateException {
2    if (!this.refreshed.compareAndSet(false, true)) {
3       throw new IllegalStateException(
4             "GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
5    }
6   //生成一個序列化id
7    this.beanFactory.setSerializationId(getId());
8 }

這裏使用AbstractApplicationContext. refreshBeanFactory()在不一樣實現容器中有點區別,若是是以xml方式配置bean,會使用AbstractRefreshableApplicationContext容器中的實現,該容器中實現xml配置文件定位,並經過BeanDefinition載入和解析xml配置文件。

而若是是註解的方式,則並無解析項目包下的註解,而是經過在refresh()方法中執行ConfigurationClassPostProcessor後置處理器完成對bean的加載.

3.BeanFactory的預準備工做

 prepareBeanFactory主要完成beanFactory的一些屬性設置

 1 protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
 2    // Tell the internal bean factory to use the context's class loader etc.
 3    beanFactory.setBeanClassLoader(getClassLoader());  //設置類加載器
 4    beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); //bean表達式解析器
 5    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
 6 
 7    // Configure the bean factory with context callbacks.
 8    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));  //添加一個BeanPostProcessor實現ApplicationContextAwareProcessor
 9 //設置忽略的自動裝配接口,表示這些接口的實現類不容許經過接口自動注入
10    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
11    beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
12    beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
13    beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
14    beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
15    beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
16 
17    // BeanFactory interface not registered as resolvable type in a plain factory.
18    // MessageSource registered (and found for autowiring) as a bean.
19 //註冊能夠自動裝配的組件,就是能夠在任何組件中容許自動注入的組件
20    beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
21    beanFactory.registerResolvableDependency(ResourceLoader.class, this);
22    beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
23    beanFactory.registerResolvableDependency(ApplicationContext.class, this);
24 
25    // Register early post-processor for detecting inner beans as ApplicationListeners.
26    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
27 
28    //添加編譯時的AspectJ
29    if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
30       beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
31       // Set a temporary ClassLoader for type matching.
32       beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
33    }
34 
35    // 給beanfactory容器中註冊組件ConfigurableEnvironment、systemProperties、systemEnvironment
36    if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
37       beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
38    }
39    if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
40       beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
41    }
42    if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
43       beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
44    }
45 }

5.invokeBeanFactoryPostProcessors 執行bean工廠後置處理器

 AbstractApplicationContext. invokeBeanFactoryPostProcessors方法實現:

 1 protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
 2    PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
 3 
 4    // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
 5    // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
 6    if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
 7       beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
 8       beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
 9    }
10 }

invokeBeanFactoryPostProcessors方法內部執行實現了BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor這兩個接口的Processor,先獲取全部BeanDefinitionRegistryPostProcessor的實現,按優先級執行(是否實現PriorityOrdered優先級接口,是否實現Ordered順序接口);再以相同的策略執行全部BeanFactoryPostProcessor的實現。

PostProcessorRegistrationDelegate. invokeBeanFactoryPostProcessors實現:

  1 public static void invokeBeanFactoryPostProcessors(
  2       ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
  3 
  4    // Invoke BeanDefinitionRegistryPostProcessors first, if any.
  5    Set<String> processedBeans = new HashSet<>();
  6 
  7    if (beanFactory instanceof BeanDefinitionRegistry) {
  8       BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
  9       List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
 10       List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
 11 
 12       for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
 13          if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
 14             BeanDefinitionRegistryPostProcessor registryProcessor =
 15                   (BeanDefinitionRegistryPostProcessor) postProcessor;
 16             registryProcessor.postProcessBeanDefinitionRegistry(registry);
 17             registryProcessors.add(registryProcessor);
 18          }
 19          else {
 20             regularPostProcessors.add(postProcessor);
 21          }
 22       }
 23 
 24       // Do not initialize FactoryBeans here: We need to leave all regular beans
 25       // uninitialized to let the bean factory post-processors apply to them!
 26       // Separate between BeanDefinitionRegistryPostProcessors that implement
 27       // PriorityOrdered, Ordered, and the rest.
 28       List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
 29 
 30       // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
 31       String[] postProcessorNames =
 32             beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 33       for (String ppName : postProcessorNames) {
 34          if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
 35             currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 36             processedBeans.add(ppName);
 37          }
 38       }
 39       sortPostProcessors(currentRegistryProcessors, beanFactory);
 40       registryProcessors.addAll(currentRegistryProcessors);
 41       invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 42       currentRegistryProcessors.clear();
 43 
 44       // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
 45       postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 46       for (String ppName : postProcessorNames) {
 47          if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
 48             currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 49             processedBeans.add(ppName);
 50          }
 51       }
 52       sortPostProcessors(currentRegistryProcessors, beanFactory);
 53       registryProcessors.addAll(currentRegistryProcessors);
 54       invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 55       currentRegistryProcessors.clear();
 56 
 57       // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
 58       boolean reiterate = true;
 59       while (reiterate) {
 60          reiterate = false;
 61          postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 62          for (String ppName : postProcessorNames) {
 63             if (!processedBeans.contains(ppName)) {
 64                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 65                processedBeans.add(ppName);
 66                reiterate = true;
 67             }
 68          }
 69          sortPostProcessors(currentRegistryProcessors, beanFactory);
 70          registryProcessors.addAll(currentRegistryProcessors);
 71          invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 72          currentRegistryProcessors.clear();
 73       }
 74 
 75       // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
 76       invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
 77       invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
 78    }
 79 
 80    else {
 81       // Invoke factory processors registered with the context instance.
 82       invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
 83    }
 84 
 85    // Do not initialize FactoryBeans here: We need to leave all regular beans
 86    // uninitialized to let the bean factory post-processors apply to them!
 87    String[] postProcessorNames =
 88          beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
 89 
 90    // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
 91    // Ordered, and the rest.
 92    List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
 93    List<String> orderedPostProcessorNames = new ArrayList<>();
 94    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
 95    for (String ppName : postProcessorNames) {
 96       if (processedBeans.contains(ppName)) {
 97          // skip - already processed in first phase above
 98       }
 99       else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
100          priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
101       }
102       else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
103          orderedPostProcessorNames.add(ppName);
104       }
105       else {
106          nonOrderedPostProcessorNames.add(ppName);
107       }
108    }
109 
110    // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
111    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
112    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
113 
114    // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
115    List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
116    for (String postProcessorName : orderedPostProcessorNames) {
117       orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
118    }
119    sortPostProcessors(orderedPostProcessors, beanFactory);
120    invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
121 
122    // Finally, invoke all other BeanFactoryPostProcessors.
123    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
124    for (String postProcessorName : nonOrderedPostProcessorNames) {
125       nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
126    }
127    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
128 
129    // Clear cached merged bean definitions since the post-processors might have
130    // modified the original metadata, e.g. replacing placeholders in values...
131    beanFactory.clearMetadataCache();
132 }

這裏面在處理BeanDefinitionRegistryPostProcessors時有一個很是重要的過程,AnnotationConfigApplicationContext構造函數在初始化reader時爲內部beanFactory容器初始化了一個id爲org.springframework.context.annotation.internalConfigurationAnnotationProcessor的組件,這是一個ConfigurationClassPostProcessor組件,用來處理添加@Configuration註解的類,並將Bean定義註冊到BeanFactory中。

6.註冊BeanPostProcessor(Bean的後置處理器),用於攔截bean建立過程

 註冊後置處理器的大體邏輯是:

  1.獲取全部的 BeanPostProcessor

  2.根據處理器實現的接口區分出4中類型:

    a.實現PriorityOrdered接口的處理器

    b.實現Ordered接口的處理器,

    c.實現MergedBeanDefinitionPostProcessor接口的處理器,

    d.普通後置處理器

  3.按這個4中類型依次註冊到容器中

  4.註冊一個特殊的後置處理器ApplicationListenerDetector,ApplicationListenerDetector自己也實現了MergedBeanDefinitionPostProcessor接口,有個問題,這個爲何沒有在上面c,d之間註冊,而是放到最後?

AbstractApplicationContext .registerBeanPostProcessors(beanFactory);實現邏輯:

1 protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
2    PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
3 }
 1 public static void registerBeanPostProcessors(
 2       ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
 3 
 4    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
 5 
 6    // Register BeanPostProcessorChecker that logs an info message when
 7    // a bean is created during BeanPostProcessor instantiation, i.e. when
 8    // a bean is not eligible for getting processed by all BeanPostProcessors.
 9    int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
10    beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
11 
12    // Separate between BeanPostProcessors that implement PriorityOrdered,
13    // Ordered, and the rest.
14 //按優先級分類
15    List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
16    List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
17    List<String> orderedPostProcessorNames = new ArrayList<>();
18    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
19    for (String ppName : postProcessorNames) {
20       if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
21          BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
22          priorityOrderedPostProcessors.add(pp);
23          if (pp instanceof MergedBeanDefinitionPostProcessor) {
24             internalPostProcessors.add(pp);
25          }
26       }
27       else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
28          orderedPostProcessorNames.add(ppName);
29       }
30       else {
31          nonOrderedPostProcessorNames.add(ppName);
32       }
33    }
34 
35    //先註冊實現PriorityOrdered接口的處理器,添加到beanfactory容器中beanFactory.addBeanPostProcessor(postProcessor);
36    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
37    registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
38 
39    //註冊實現Ordered接口的處理器
40    List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
41    for (String ppName : orderedPostProcessorNames) {
42       BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
43       orderedPostProcessors.add(pp);
44       if (pp instanceof MergedBeanDefinitionPostProcessor) {
45          internalPostProcessors.add(pp);
46       }
47    }
48    sortPostProcessors(orderedPostProcessors, beanFactory);
49    registerBeanPostProcessors(beanFactory, orderedPostProcessors);
50 
51    // 註冊沒有實現Ordered或PriorityOrdered的處理器(nonOrderedPostProcessors)
52    List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
53    for (String ppName : nonOrderedPostProcessorNames) {
54       BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
55       nonOrderedPostProcessors.add(pp);
56       if (pp instanceof MergedBeanDefinitionPostProcessor) {
57          internalPostProcessors.add(pp);
58       }
59    }
60    registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
61 
62    // Finally, re-register all internal BeanPostProcessors.
63   //最後,從新註冊全部internal BeanPostProcessors(實現MergedBeanDefinitionPostProcessor接口的後置處理器
64 65    sortPostProcessors(internalPostProcessors, beanFactory);
66    registerBeanPostProcessors(beanFactory, internalPostProcessors);
67 
68    //註冊ApplicationListenerDetector,用於Bean建立完時檢查是不是ApplicationListener
69    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
70 }

7.初始化MessageSource組件(作國際化功能;消息綁定,消息解析)

 AbstractApplicationContext .initMessageSource()方法實現代碼:

 1 protected void initMessageSource() {
 2 //獲取beanFactory
 3    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
 4 //判斷是否已經存在id爲MESSAGE_SOURCE_BEAN_NAME的組件
 5    if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
 6       this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
 7       // Make MessageSource aware of parent MessageSource.
 8       if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
 9          HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
10          if (hms.getParentMessageSource() == null) {
11             // Only set parent context as parent MessageSource if no parent MessageSource
12             // registered already.
13             hms.setParentMessageSource(getInternalParentMessageSource());
14          }
15       }
16       if (logger.isDebugEnabled()) {
17          logger.debug("Using MessageSource [" + this.messageSource + "]");
18       }
19    }
20    else {
21       // Use empty MessageSource to be able to accept getMessage calls.
22       DelegatingMessageSource dms = new DelegatingMessageSource();
23       dms.setParentMessageSource(getInternalParentMessageSource());
24       this.messageSource = dms;
25       beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
26       if (logger.isDebugEnabled()) {
27          logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +
28                "': using default [" + this.messageSource + "]");
29       }
30    }
31 }

8.初始化事件派發器

 AbstractApplicationContext .initApplicationEventMulticaster()方法實現邏輯

 1 protected void initApplicationEventMulticaster() {
 2 //獲取BeanFactory
 3    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
 4 //若是有配置beanName爲applicationEventMulticaster的事件派發器,則將其賦給容器中的applicationEventMulticaster對象
 5    if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
 6       this.applicationEventMulticaster =
 7             beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
 8       if (logger.isDebugEnabled()) {
 9          logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
10       }
11    }
12    else {
13 //不存在,則建立一個SimpleApplicationEventMulticaster事件派發器,並註冊到beanfactory中
14       this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
15       beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
16       if (logger.isDebugEnabled()) {
17          logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
18                APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
19                "': using default [" + this.applicationEventMulticaster + "]");
20       }
21    }
22 }

10. 註冊時間監聽器,將項目裏面的ApplicationListener註冊到容器中來

  registerListeners方法主要實現將事件監聽器添加到IOC容器中的事件派發器中,並在最後作了一個事件發佈的邏輯(若是以前的步驟有產生事件,則將earlyApplicationEvents中保存的事件逐一發布)

 AbstractApplicationContext .registerListeners()方法實現邏輯:

 1 protected void registerListeners() {
 2    // Register statically specified listeners first.
 3    for (ApplicationListener<?> listener : getApplicationListeners()) {
 4       getApplicationEventMulticaster().addApplicationListener(listener);
 5    }
 6 
 7    // Do not initialize FactoryBeans here: We need to leave all regular beans
 8    // uninitialized to let post-processors apply to them!
 9    String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
10    for (String listenerBeanName : listenerBeanNames) {
11       getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
12    }
13 
14    // Publish early application events now that we finally have a multicaster...
15    Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
16    this.earlyApplicationEvents = null;
17    if (earlyEventsToProcess != null) {
18       for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
19          getApplicationEventMulticaster().multicastEvent(earlyEvent);
20       }
21    }
22 }

11. 初始化全部剩下的單實例bean,單例bean在初始化容器時建立,原型bean在獲取時(getbean)時建立

 AbstractApplicationContext.finishBeanFactoryInitialization(beanFactory);方法實現代碼:

 

 1 protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
 2    //組件轉換器相關
 3    if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
 4          beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
 5       beanFactory.setConversionService(
 6             beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
 7    }
 8 
 9    // Register a default embedded value resolver if no bean post-processor
10    // (such as a PropertyPlaceholderConfigurer bean) registered any before:
11    // at this point, primarily for resolution in annotation attribute values.
12    if (!beanFactory.hasEmbeddedValueResolver()) {
13       beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
14    }
15 
16    //aspectj相關.
17    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
18    for (String weaverAwareName : weaverAwareNames) {
19       getBean(weaverAwareName);
20    }
21 
22    // Stop using the temporary ClassLoader for type matching.
23    beanFactory.setTempClassLoader(null);
24 
25    // Allow for caching all bean definition metadata, not expecting further changes.
26    beanFactory.freezeConfiguration();
27 
28    // 初始化後剩下的單實例bean
29    beanFactory.preInstantiateSingletons();
30 }

DefaultListableBeanFactory. preInstantiateSingletons()方法實現邏輯:

 1 public void preInstantiateSingletons() throws BeansException {
 2    if (logger.isDebugEnabled()) {
 3       logger.debug("Pre-instantiating singletons in " + this);
 4    }
 5 
 6    // Iterate over a copy to allow for init methods which in turn register new bean definitions.
 7    // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
 8   //容器中全部bean名稱
 9    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
10 
11    // Trigger initialization of all non-lazy singleton beans...
12    for (String beanName : beanNames) {
13   //獲取Bean的定義信息;RootBeanDefinition
14       RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
15   //非抽象,單例,非延遲加載
16       if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
17   //是不是FactoryBean
18          if (isFactoryBean(beanName)) {
19     // 經過"&beanName"獲取工廠Bean實例
20             Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
21             if (bean instanceof FactoryBean) {
22                final FactoryBean<?> factory = (FactoryBean<?>) bean;
23                boolean isEagerInit;
24                if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
25                   isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
26                               ((SmartFactoryBean<?>) factory)::isEagerInit,
27                         getAccessControlContext());
28                }
29                else {
30                   isEagerInit = (factory instanceof SmartFactoryBean &&
31                         ((SmartFactoryBean<?>) factory).isEagerInit());
32                }
33                if (isEagerInit) {
34                   getBean(beanName);
35                }
36             }
37          }
38          else {
39       //不是FactoryBean,則利用getBean(beanName)實例化bean 
40             getBean(beanName);
41          }
42       }
43    }
44 
45    // Trigger post-initialization callback for all applicable beans...
46    for (String beanName : beanNames) {
47       Object singletonInstance = getSingleton(beanName);
48       if (singletonInstance instanceof SmartInitializingSingleton) {
49          final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
50          if (System.getSecurityManager() != null) {
51             AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
52                smartSingleton.afterSingletonsInstantiated();
53                return null;
54             }, getAccessControlContext());
55          }
56          else {
57             smartSingleton.afterSingletonsInstantiated();
58          }
59       }
60    }
61 }

12. 完成BeanFactory的初始化建立工做,IOC容器就建立完成

 AbstractApplicationContext.finishRefresh()實現邏輯:

 1 protected void finishRefresh() {
 2    // Clear context-level resource caches (such as ASM metadata from scanning).
 3    clearResourceCaches();
 4 
 5    //初始化和生命週期有關的後置處理器LifecycleProcessor,默認DefaultLifecycleProcessor
 6    initLifecycleProcessor();
 7 
 8    // 回調生命週期處理器
 9    getLifecycleProcessor().onRefresh();
10 
11    //發佈容器刷新完成事件:ContextRefreshedEvent
12    publishEvent(new ContextRefreshedEvent(this));
13 
14    LiveBeansView.registerApplicationContext(this);
15 }

  以上基本分析了AnnotationConfigApplicationContext容器的初始化過程, Spring容器在啓動過程當中,會先保存全部註冊進來的Bean的定義信息;Spring容器根據條件建立Bean實例,區分單例,仍是原型,後置處理器等(後置處理器會在容器建立過程當中經過getBean建立,並執行相應的邏輯);Spring容器在建立bean實例後,會使用多種後置處理器來增長bean的功能,好比處理自動注入,AOP,異步,這種後置處理器機制也豐富了bean的功能。

相關文章
相關標籤/搜索