Spring源码分析--IOC流程

代码地址:https://github.com/LoveWK/mySpring/tree/master/myIocDemo

1.创建MyAnnotation类

1 @Configuration//java配置类注解
2 @ComponentScan("com.wk")//设置扫描包的路径
3 public class MyAnnotation {
4 }

2.创建Test类

 1 /**
 2  * 测试类
 3  */
 4 public class Test {
 5     public static void main(String[] args) {
 6         //把spring所有的前提环境准备好了,包括spring容器还有类的实例化都完成了。
 7         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyAnnotation.class);
 8
 9         IndexService service = (IndexService)context.getBean("service");
10         service.query();
11     }
12 }

3.点击进入AnnotationConfigApplicationContext的构造方法:

 1     /**
 2      * 这个构造方法需要传入一个被Javaconfig注解过的配置类
 3      * 然后会把这个被注解了的配置类通过注解读取器读取后进行解析
 4      * @param annotatedClasses
 5      */
 6     public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
 7         //这里由于它有父类,故而会先调用父类的构造方法,然后才会调用自己的构造方法,
 8         //在自己的构造方法中初始化一个读取器和扫描器
 9         this();
10         //注册单个bean给容器,比如有新加的类可以使用这个方法
11         //但是注册之后需要手动调用refresh()方法去触发容器解析注解
12         register(annotatedClasses);
13         refresh();
14     }

  3.1先进入this()查看此类的构造方法:

 1 public AnnotationConfigApplicationContext() {
 2         /**
 3          * 创建一个读取注解的Bean定义读取器
 4          * 什么是Bean定义?BeanDefinition
 5          * BeanDefinition这个类是描述springBean对象的类。
 6          */
 7         this.reader = new AnnotatedBeanDefinitionReader(this);
 8         /**
 9          * 创建一个扫描器,扫描所有加了注解的类
10          * 可以用来扫描包或者类,继而转成BeanDefinition
11          * 但是我们在自己项目中使用自动扫描注解的时候,扫描包的工作不是在这个scanner对象中完成的
12          * 这里的scanner对象仅仅是为了程序员能够在外部调用AnnotationConfigApplicationContext对象的scan方法
13          */
14         this.scanner = new ClassPathBeanDefinitionScanner(this);
15     }

  其中this.reader = new AnnotatedBeanDefinitionReader(this);方法是比较重要的,它注册了spring中的6个BeanDefinition类,其中最重要的是ConfigurationClassPostProcessor类。

  AnnotatedBeanDefinitionReader(this)-->AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry)-->

this(registry, getOrCreateEnvironment(registry))-->AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment)-->AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry)-->

registerAnnotationConfigProcessors(BeanDefinitionRegistry registry)-->registerAnnotationConfigProcessors(registry, null)-->

 1 public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
 2             BeanDefinitionRegistry registry, @Nullable Object source) {
 3
 4         DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
 5         if (beanFactory != null) {
 6             if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
 7                 beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
 8             }
 9             if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
10                 beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
11             }
12         }
13
14         Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);
15
16         if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
17             RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
18             def.setSource(source);
19             beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
20         }
21
22         if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
23             RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
24             def.setSource(source);
25             beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
26         }
27
28         // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
29         if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
30             RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
31             def.setSource(source);
32             beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
33         }
34
35         // Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
36         if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
37             RootBeanDefinition def = new RootBeanDefinition();
38             try {
39                 def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
40                         AnnotationConfigUtils.class.getClassLoader()));
41             }
42             catch (ClassNotFoundException ex) {
43                 throw new IllegalStateException(
44                         "Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
45             }
46             def.setSource(source);
47             beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
48         }
49
50         if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
51             RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
52             def.setSource(source);
53             beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
54         }
55
56         if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
57             RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
58             def.setSource(source);
59             beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
60         }
61
62         return beanDefs;
63     }

4.调用register()方法

5.进入refresh()方法,这里才是最重要的:这个方法的实现在AbstractApplicationContext类中。

 1 public void refresh() throws BeansException, IllegalStateException {
 2         synchronized (this.startupShutdownMonitor) {
 3             // Prepare this context for refreshing.
 4             // 准备工作,包括设置启动时间,是否激活标识位,
 5             // 初始化属性源(property source)设置
 6             prepareRefresh();
 7
 8             // Tell the subclass to refresh the internal bean factory.
 9             // 通过子类来获取之前注册了bean的容器工厂beanFactory
10             // 这里我们是获得DefaultListableBeanFactory这个工厂类
11             ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
12
13             // Prepare the bean factory for use in this context.
14             // 准备工厂
15             prepareBeanFactory(beanFactory);
16
17             try {
18                 // Allows post-processing of the bean factory in context subclasses.
19                 postProcessBeanFactory(beanFactory);
20
21                 // Invoke factory processors registered as beans in the context.
22                 // 在spring的环境中去执行已经被注册的factory processors
23                 // 设置执行自定义的ProcessorsBeanFactory和spring内部自己定义的
24                 // 其实就是执行spring内部的ConfigurationClassPostProcessor这个类
25                 invokeBeanFactoryPostProcessors(beanFactory);
26
27                 // Register bean processors that intercept bean creation.
28                 registerBeanPostProcessors(beanFactory);
29
30                 // Initialize message source for this context.
31                 initMessageSource();
32
33                 // Initialize event multicaster for this context.
34                 initApplicationEventMulticaster();
35
36                 // Initialize other special beans in specific context subclasses.
37                 onRefresh();
38
39                 // Check for listener beans and register them.
40                 registerListeners();
41
42                 // Instantiate all remaining (non-lazy-init) singletons.
43                 finishBeanFactoryInitialization(beanFactory);
44
45                 // Last step: publish corresponding event.
46                 finishRefresh();
47             }
48
49             catch (BeansException ex) {
50                 if (logger.isWarnEnabled()) {
51                     logger.warn("Exception encountered during context initialization - " +
52                             "cancelling refresh attempt: " + ex);
53                 }
54
55                 // Destroy already created singletons to avoid dangling resources.
56                 destroyBeans();
57
58                 // Reset ‘active‘ flag.
59                 cancelRefresh(ex);
60
61                 // Propagate exception to caller.
62                 throw ex;
63             }
64
65             finally {
66                 // Reset common introspection caches in Spring‘s core, since we
67                 // might not ever need metadata for singleton beans anymore...
68                 resetCommonCaches();
69             }
70         }
71     }

  5.1点击prepareBeanFactory()方法

    这个方法中比较重要的是addBeanPostProcessor()方法,添加一个后置管理器。

 1 /**
 2      * 配置其标准特征,比如上下文的加载器,ClassLoader和post-processors回调
 3      * @param beanFactory
 4      */
 5     protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
 6         // Tell the internal bean factory to use the context‘s class loader etc.
 7         beanFactory.setBeanClassLoader(getClassLoader());
 8         // bean的表达式解析
 9         beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
10         // 注册属性编辑器,对象与string类型的转换
11         beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
12
13         // Configure the bean factory with context callbacks.
14         // 添加一个后置管理器
15         beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
16         // 忽略一些接口的实现,不自动注入
17         beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
18         beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
19         beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
20         beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
21         beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
22         beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
23
24         // BeanFactory interface not registered as resolvable type in a plain factory.
25         // MessageSource registered (and found for autowiring) as a bean.
26         beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
27         beanFactory.registerResolvableDependency(ResourceLoader.class, this);
28         beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
29         beanFactory.registerResolvableDependency(ApplicationContext.class, this);
30
31         // Register early post-processor for detecting inner beans as ApplicationListeners.
32         beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
33
34         // Detect a LoadTimeWeaver and prepare for weaving, if found.
35         if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
36             beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
37             // Set a temporary ClassLoader for type matching.
38             beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
39         }
40
41         // Register default environment beans.
42         // 意思是如果自定义的Bean中没有名为"systermProperties"和"systermEnvironment"的bean
43         // 则注册两个bean,key为"systermProperties"和"systermEnvironment",value为Map
44         // 这两个bean就是一些系统配置和系统环境信息
45         if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
46             beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
47         }
48         if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
49             beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
50         }
51         if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
52             beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
53         }
54     }

  5.2点击invokeBeanFactoryPostProcessors()方法,进入

 1 protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
 2         // 这个地方需要注意getBeanFactoryPostProcessors()是获取自己继承BeanFactoryPostProcessors接口实现的类
 3         // 需要我们在AnnotationConfigApplicationContext实例对象中通过context.addBeanFactoryPostProcessor()来添加自己定义的类
 4         PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
 5
 6         // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
 7         // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
 8         if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
 9             beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
10             beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
11         }
12     }

    5.2.1点击invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());方法进入

  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             //  自定义的BeanFactoryPostProcessors
 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             // 这个currentRegistryProcessors放的是spring内部自己实现了的BeanDefinitionRegistryPostProcessor
 29             List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
 30
 31             // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
 32             // getBeanNamesForType根据类的类型获取类的名字。
 33             String[] postProcessorNames =
 34                     beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 35             for (String ppName : postProcessorNames) {
 36                 if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
 37                     currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 38                     processedBeans.add(ppName);
 39                 }
 40             }
 41             // 进行排序
 42             sortPostProcessors(currentRegistryProcessors, beanFactory);
 43             // 合并list(为什么要合并,因为还有自己写的)
 44             registryProcessors.addAll(currentRegistryProcessors);
 45             // 这里很重要
 46             // currentRegistryProcessors 这个集合中只有ConfigurationClassPostProcessor这一个类
 47          invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 48
 49             // 这个list是一个临时变量,故而要删除
 50             currentRegistryProcessors.clear();
 51
 52             // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
 53             postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 54             for (String ppName : postProcessorNames) {
 55                 if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
 56                     currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 57                     processedBeans.add(ppName);
 58                 }
 59             }
 60             sortPostProcessors(currentRegistryProcessors, beanFactory);
 61             registryProcessors.addAll(currentRegistryProcessors);
 62             invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 63             currentRegistryProcessors.clear();
 64
 65             // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
 66             boolean reiterate = true;
 67             while (reiterate) {
 68                 reiterate = false;
 69                 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 70                 for (String ppName : postProcessorNames) {
 71                     if (!processedBeans.contains(ppName)) {
 72                         currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 73                         processedBeans.add(ppName);
 74                         reiterate = true;
 75                     }
 76                 }
 77                 sortPostProcessors(currentRegistryProcessors, beanFactory);
 78                 registryProcessors.addAll(currentRegistryProcessors);
 79                 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 80                 currentRegistryProcessors.clear();
 81             }
 82
 83             // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
 84             invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
 85             invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
 86         }
 87
 88         else {
 89             // Invoke factory processors registered with the context instance.
 90             invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
 91         }
 92
 93         // Do not initialize FactoryBeans here: We need to leave all regular beans
 94         // uninitialized to let the bean factory post-processors apply to them!
 95         String[] postProcessorNames =
 96                 beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
 97
 98         // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
 99         // Ordered, and the rest.
100         List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
101         List<String> orderedPostProcessorNames = new ArrayList<>();
102         List<String> nonOrderedPostProcessorNames = new ArrayList<>();
103         for (String ppName : postProcessorNames) {
104             if (processedBeans.contains(ppName)) {
105                 // skip - already processed in first phase above
106             }
107             else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
108                 priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
109             }
110             else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
111                 orderedPostProcessorNames.add(ppName);
112             }
113             else {
114                 nonOrderedPostProcessorNames.add(ppName);
115             }
116         }
117
118         // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
119         sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
120         invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
121
122         // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
123         List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
124         for (String postProcessorName : orderedPostProcessorNames) {
125             orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
126         }
127         sortPostProcessors(orderedPostProcessors, beanFactory);
128         invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
129
130         // Finally, invoke all other BeanFactoryPostProcessors.
131         List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
132         for (String postProcessorName : nonOrderedPostProcessorNames) {
133             nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
134         }
135         invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
136
137         // Clear cached merged bean definitions since the post-processors might have
138         // modified the original metadata, e.g. replacing placeholders in values...
139         beanFactory.clearMetadataCache();
140     }

    5.2.1.1点击invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);方法进入

1 private static void invokeBeanDefinitionRegistryPostProcessors(
2             Collection<? extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry) {
3         // 因为只有一条数据,所以实现的就是ConfigurationClassPostProcessor这个类中的postProcessBeanDefinitionRegistry方法
4         for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) {
5             postProcessor.postProcessBeanDefinitionRegistry(registry);
6         }
7     }

    5.2.1.1.1进入ConfigurationClassPostProcessor实现的postProcessBeanDefinitionRegistry(registry);方法

 1 public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
 2         int registryId = System.identityHashCode(registry);
 3         if (this.registriesPostProcessed.contains(registryId)) {
 4             throw new IllegalStateException(
 5                     "postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);
 6         }
 7         if (this.factoriesPostProcessed.contains(registryId)) {
 8             throw new IllegalStateException(
 9                     "postProcessBeanFactory already called on this post-processor against " + registry);
10         }
11         this.registriesPostProcessed.add(registryId);
12
13         processConfigBeanDefinitions(registry);
14     }

    继续点击processConfigBeanDefinitions(registry);方法查看具体实现:

  1 public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
  2         // app 提供的bean也就是我们程序内提供的bean
  3         List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
  4         // 获取容器中注册的所有bean的名字
  5         String[] candidateNames = registry.getBeanDefinitionNames();
  6
  7         for (String beanName : candidateNames) {
  8             BeanDefinition beanDef = registry.getBeanDefinition(beanName);
  9             if (beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE) != null) {
 10                 if (logger.isDebugEnabled()) {
 11                     logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);
 12                 }
 13             }
 14             else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
 15                 configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
 16             }
 17         }
 18
 19         // Return immediately if no @Configuration classes were found
 20         if (configCandidates.isEmpty()) {
 21             return;
 22         }
 23
 24         // Sort by previously determined @Order value, if applicable
 25         configCandidates.sort((bd1, bd2) -> {
 26             int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition());
 27             int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition());
 28             return Integer.compare(i1, i2);
 29         });
 30
 31         // Detect any custom bean name generation strategy supplied through the enclosing application context
 32         SingletonBeanRegistry sbr = null;
 33         if (registry instanceof SingletonBeanRegistry) {
 34             sbr = (SingletonBeanRegistry) registry;
 35             if (!this.localBeanNameGeneratorSet) {
 36                 BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(
 37                         AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR);
 38                 if (generator != null) {
 39                     this.componentScanBeanNameGenerator = generator;
 40                     this.importBeanNameGenerator = generator;
 41                 }
 42             }
 43         }
 44
 45         if (this.environment == null) {
 46             this.environment = new StandardEnvironment();
 47         }
 48
 49         // Parse each @Configuration class
 50         ConfigurationClassParser parser = new ConfigurationClassParser(
 51                 this.metadataReaderFactory, this.problemReporter, this.environment,
 52                 this.resourceLoader, this.componentScanBeanNameGenerator, registry);
 53
 54         Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
 55         Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
 56         do {
 57             parser.parse(candidates);
 58             parser.validate();
 59
 60             Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());
 61             configClasses.removeAll(alreadyParsed);
 62
 63             // Read the model and create bean definitions based on its content
 64             if (this.reader == null) {
 65                 this.reader = new ConfigurationClassBeanDefinitionReader(
 66                         registry, this.sourceExtractor, this.resourceLoader, this.environment,
 67                         this.importBeanNameGenerator, parser.getImportRegistry());
 68             }
 69             this.reader.loadBeanDefinitions(configClasses);
 70             alreadyParsed.addAll(configClasses);
 71
 72             candidates.clear();
 73             if (registry.getBeanDefinitionCount() > candidateNames.length) {
 74                 String[] newCandidateNames = registry.getBeanDefinitionNames();
 75                 Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames));
 76                 Set<String> alreadyParsedClasses = new HashSet<>();
 77                 for (ConfigurationClass configurationClass : alreadyParsed) {
 78                     alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());
 79                 }
 80                 for (String candidateName : newCandidateNames) {
 81                     if (!oldCandidateNames.contains(candidateName)) {
 82                         BeanDefinition bd = registry.getBeanDefinition(candidateName);
 83                         if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&
 84                                 !alreadyParsedClasses.contains(bd.getBeanClassName())) {
 85                             candidates.add(new BeanDefinitionHolder(bd, candidateName));
 86                         }
 87                     }
 88                 }
 89                 candidateNames = newCandidateNames;
 90             }
 91         }
 92         while (!candidates.isEmpty());
 93
 94         // Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
 95         if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
 96             sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
 97         }
 98
 99         if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {
100             // Clear cache in externally provided MetadataReaderFactory; this is a no-op
101             // for a shared cache since it‘ll be cleared by the ApplicationContext.
102             ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();
103         }
104     }
 

原文地址:https://www.cnblogs.com/wk-missQ1/p/12528033.html

时间: 2024-10-07 14:48:16

Spring源码分析--IOC流程的相关文章

spring源码分析---IOC(1)

我们都知道spring有2个最重要的概念,IOC(控制反转)和AOP(依赖注入).今天我就分享一下spring源码的IOC. IOC的定义:直观的来说,就是由spring来负责控制对象的生命周期和对象间的关系,将对象之间的关系抽象出来,通过spring容器控制对象生成时机,减少对象之间的耦合度. 更通俗一点的说就是,JAVA程序中,当你在代码中需要使用某个类提供的功能时,你首先需要new一个对象,给它传递必要的参数,然后才能使用它提供的功能:有了IOC之后,IOC的容器类似一个中介,所有的对象都

【spring源码分析】IOC容器初始化(一)

前言:spring主要就是对bean进行管理,因此IOC容器的初始化过程非常重要,搞清楚其原理不管在实际生产或面试过程中都十分的有用.在[spring源码分析]准备工作中已经搭建好spring的环境,并利用xml配置形式对类进行了实例化.在test代码中有一个非常关键的类ClassPathXmlApplicationContext,在这个类中实现了IOC容器的初始化,因此我们从ClassPathXmlApplicationContext入手开始研究IOC的初始化过程. 1.ClassPathXm

【spring源码分析】IOC容器初始化(总结)

前言:在经过前面十二篇文章的分析,对bean的加载流程大致梳理清楚了.因为内容过多,因此需要进行一个小总结. 经过前面十二篇文章的漫长分析,终于将xml配置文件中的bean,转换成我们实际所需要的真正的bean对象. 总结 [spring源码分析]IOC容器初始化(一):主要分析了Spring是如何解析占位符以及BeanFactory的最终实现类DefaultListableBeanFactory. [spring源码分析]IOC容器初始化(二):以loadBeanDefinitions函数为切

【Spring源码分析】Bean加载流程概览

代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. 很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事的都是Java Web的工作,对于程序员来说,一个Web项目用到Spring,只是配置一下配置文件而已,Spring的加载过程相对是不太透明的,不太好去找加载的代码入口. 下面有很简单的一段代码可以作为Spring代码加载的入口: 1 ApplicationContext ac = new Clas

【Spring源码分析】配置文件读取流程

前言 Spring配置文件读取流程本来是和http://www.cnblogs.com/xrq730/p/6285358.html一文放在一起的,这两天在看Spring自定义标签的时候,感觉对Spring配置文件读取流程还是研究得不够,因此将Spring配置文件读取流程部分从之前的文章拆出来单独成为一文. 为了看一下Spring配置文件加载流程,先定义一个bean.xml: 1 <?xml version="1.0" encoding="UTF-8"?>

【Spring源码分析】非懒加载的Bean实例化过程(下篇)

doCreateBean方法 上文[Spring源码分析]非懒加载的Bean实例化过程(上篇),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下AbstractAutowireCapableBeanFactory的doCreateBean方法代码: 1 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[]

Spring源码分析——BeanFactory体系之抽象类、类分析(一)

上一篇介绍了BeanFactory体系的所有接口——Spring源码分析——BeanFactory体系之接口详细分析,本篇就接着介绍BeanFactory体系的抽象类和接口. 一.BeanFactory的基本类体系结构(类为主): 上图可与 Spring源码分析——BeanFactory体系之接口详细分析 的图结合分析,一个以接口为主,一个以类为主(PS:Spring的体系结构要分析清楚,不得不曲线救国啊!不然27寸屏幕给我画估计都装不下.). 具体: 1.7层的类体系继承. 2.Abstrac

【Spring源码分析】原型Bean实例化过程、byName与byType及FactoryBean获取Bean源码实现

原型Bean加载过程 之前的文章,分析了非懒加载的单例Bean整个加载过程,除了非懒加载的单例Bean之外,Spring中还有一种Bean就是原型(Prototype)的Bean,看一下定义方式: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi=&qu

Spring源码分析专题——目录

Spring源码分析专题 -- 阅读指引 IOC容器 Spring源码分析专题 -- IOC容器启动过程(上篇) Spring源码分析专题 -- IOC容器启动过程(中篇) Spring源码分析专题 -- IOC容器启动过程(下篇) Spring源码分析专题 -- IOC容器依赖注入 SpringMVC Spring源码分析专题 -- SpringMVC IOC容器依赖注入 Spring源码分析专题 -- SpringMVC原理分析 Spring源码分析专题 -- SpringAOP源码分析 S