spring启动过程之源码跟踪(中)spring Debug

上节我们debug到

1 // Tell the subclass to refresh the internal bean factory.
2 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

我们继续debug 下一步:配置beanfactory的上下文

 1     /**
 2      * Configure the factory's standard context characteristics,
 3      * such as the context's ClassLoader and post-processors.
 4      * @param beanFactory the BeanFactory to configure
 5      */
 6     protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
 7         // Tell the internal bean factory to use the context's class loader.
 8         beanFactory.setBeanClassLoader(getClassLoader());
 9 
10         // Populate the bean factory with context-specific resource editors.
11         beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));
12 
13         // Configure the bean factory with context callbacks.
14         beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
15         beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
16         beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
17         beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
18         beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
19 
20         // BeanFactory interface not registered as resolvable type in a plain factory.
21         // MessageSource registered (and found for autowiring) as a bean.
22         beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
23         beanFactory.registerResolvableDependency(ResourceLoader.class, this);
24         beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
25         beanFactory.registerResolvableDependency(ApplicationContext.class, this);
26 
27         // Detect a LoadTimeWeaver and prepare for weaving, if found.
28         if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME) && JdkVersion.isAtLeastJava15()) {
29             // Register the (JDK 1.5 specific) LoadTimeWeaverAwareProcessor.
30             try {
31                 Class ltwapClass = ClassUtils.forName(
32                         "org.springframework.context.weaving.LoadTimeWeaverAwareProcessor",
33                         AbstractApplicationContext.class.getClassLoader());
34                 BeanPostProcessor ltwap = (BeanPostProcessor) BeanUtils.instantiateClass(ltwapClass);
35                 ((BeanFactoryAware) ltwap).setBeanFactory(beanFactory);
36                 beanFactory.addBeanPostProcessor(ltwap);
37             }
38             catch (ClassNotFoundException ex) {
39                 throw new IllegalStateException("Spring's LoadTimeWeaverAwareProcessor class is not available");
40             }
41             // Set a temporary ClassLoader for type matching.
42             beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
43         }
44     }

下面是下一步要debug的内容:

                // 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();

初始化singletonbean(DefaultListableBeanFactory.java#preInstantiateSingletons())

 1     public void preInstantiateSingletons() throws BeansException {
 2         if (this.logger.isInfoEnabled()) {
 3             this.logger.info("Pre-instantiating singletons in " + this);
 4         }
 5 
 6         synchronized (this.beanDefinitionMap) {
 7             for (Iterator it = this.beanDefinitionNames.iterator(); it.hasNext();) {
 8                 String beanName = (String) it.next();
 9                 RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
10                 if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
11                     if (isFactoryBean(beanName)) {
12                         FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
13                         if (factory instanceof SmartFactoryBean && ((SmartFactoryBean) factory).isEagerInit()) {
14                             getBean(beanName);
15                         }
16                     }
17                     else {
18                         getBean(beanName);
19                     }
20                 }
21             }
22         }
23     }

结束动作:发布事件

 1     public void publishEvent(ApplicationEvent event) {
 2         Assert.notNull(event, "Event must not be null");
 3         if (logger.isTraceEnabled()) {
 4             logger.trace("Publishing event in context [" + getId() + "]: " + event);
 5         }
 6         getApplicationEventMulticaster().multicastEvent(event);
 7         if (this.parent != null) {
 8             this.parent.publishEvent(event);
 9         }
10     }
原文地址:https://www.cnblogs.com/davidwang456/p/2955025.html