springboot原始碼2--ApplicationContext物件初始化過程

weixin_34402408發表於2018-11-19

本文以AnnotationConfigEmbeddedWebApplicationContext為例來說明初始化過程。首先看下這個類的建構函式。

    public AnnotationConfigEmbeddedWebApplicationContext() {
        this.reader = new AnnotatedBeanDefinitionReader(this);
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }

(1)首先看下AnnotatedBeanDefinitionReader這個類的初始化過程,省略呼叫過程,主要是呼叫AnnotationConfigUtils這個類的registerAnnotationConfigProcessors方法。

    public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
            BeanDefinitionRegistry registry, Object source) {

        DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);

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

        if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
            def.setSource(source);
            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);
            beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, REQUIRED_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);
            beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
        }
......
        return beanDefs;
    }

這個方法很長,主要是將spring內部的一些類放到DefaultListableBeanFactory中beanDefinitionMap這個map中,供後面解析bean時使用,這裡給出一下注冊的bean。

    ConfigurationClassPostProcessor.class
        AutowiredAnnotationBeanPostProcessor.class
        RequiredAnnotationBeanPostProcessor.class
        CommonAnnotationBeanPostProcessor.class
        PersistenceAnnotationBeanPostProcessor.class
        EventListenerMethodProcessor.class
        DefaultEventListenerFactory.class

(2)例項化ClassPathBeanDefinitionScanner類。

 public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
         Environment environment, ResourceLoader resourceLoader) {

     Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
     this.registry = registry;

     if (useDefaultFilters) {
         registerDefaultFilters();
     }
     setEnvironment(environment);
     setResourceLoader(resourceLoader);
 }

相關文章