3.spring 启动过程源码解析一

spring 启动过程解析(一)

spring容器初始化

以AnnotationConfigApplicationContext为启动对象,对spring 启动过程进行解析,启动代码如下
spring 版本 5.2.4

public class MainTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MyMainConfig.class);
    }
}

1. 初始化beanfactory

一般情况下,beanfactory是通过实现了进行调用父类(GenericApplicationContext)无参构造函数进行声明,类型是DefaultListableBeanFactory

public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
    public GenericApplicationContext() {
        // 设置 customClassLoader false
        this.customClassLoader = false;
        // 初始化刷新状态
        this.refreshed = new AtomicBoolean();
        // 初始化beanfactory 默认为DefaultListableBeanFactory
        this.beanFactory = new DefaultListableBeanFactory();
    }
}

2.初始化beanDefinitionReader

一般情况调用自身对象自定义的beanDefinitionReader,例如:XmlServletWebServerApplicationContext,使用XmlBeanDefinitionReader

public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {
    public AnnotationConfigApplicationContext() {
        // 初始化AnnotatedBeanDefinitionReader
        this.reader = new AnnotatedBeanDefinitionReader(this);
        // 初始化其他bean对象扫描器
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }
}

AnnotatedBeanDefinitionReader对象

AnnotatedBeanDefinitionReader添加了基础配置,并且通过reader进行BeanDefinition的注册向beanfactory注册了一部分beanFactoryPostProcessor,具体内容如下:

  1. 初始化bean名称生成器
  2. 初始化头对象解析器
  3. 初始化条件解析器
  4. 设置Annotation配置相关的处理器,处理器包括:
    • 设置dependencyComparator:AnnotationAwareOrderComparator,可以获取某个对象上的Order注解或者通过实现Ordered接口所定义的值进行排序,日常开发中可以利用这个类来进行排序
    • 设置autowireCandidateResolver:ContextAnnotationAutowireCandidateResolver,用来解析某个Bean能不能进行自动注入
    • 添加ConfigurationClassPostProcessor对应的BeanDefinition,它是一个BeanDefinitionRegistryPostProcessor,并且实现了PriorityOrdered接口
    • 向BeanFactory中添加ConfigurationClassPostProcessor对应的BeanDefinition,它是一个BeanDefinitionRegistryPostProcessor,并且实现了PriorityOrdered接口
    • 向BeanFactory中添加AutowiredAnnotationBeanPostProcessor对应的BeanDefinition,AutowiredAnnotationBeanPostProcessor是一个InstantiationAwareBeanPostProcessorAdapter,MergedBeanDefinitionPostProcessor
    • 向BeanFactory中添加CommonAnnotationBeanPostProcessor对应的BeanDefinition,CommonAnnotationBeanPostProcessor是一个InstantiationAwareBeanPostProcessor,InitDestroyAnnotationBeanPostProcessor
    • 向BeanFactory中添加EventListenerMethodProcessor对应的BeanDefinition,EventListenerMethodProcessor是一个BeanFactoryPostProcessor,SmartInitializingSingleton
    • 向BeanFactory中添加DefaultEventListenerFactory对应的BeanDefinition,DefaultEventListenerFactory是一个EventListenerFactory
public class AnnotatedBeanDefinitionReader{
   public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
        this(registry, getOrCreateEnvironment(registry));
    }
   public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
        // 设置bean名称生成器
        this.beanNameGenerator = AnnotationBeanNameGenerator.INSTANCE;
        // 设置头对象解析器
        this.scopeMetadataResolver = new AnnotationScopeMetadataResolver();
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        Assert.notNull(environment, "Environment must not be null");
        this.registry = registry;
        // 设置条件解析器
        this.conditionEvaluator = new ConditionEvaluator(registry, environment, (ResourceLoader)null);
        // 设置Annotation配置相关的处理器
        AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
    }
}

ClassPathBeanDefinitionScanner对象

ClassPathBeanDefinitionScanner扫描、注册、获取beanDefinition,并且也设置了部分基础数据

public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateComponentProvider {
 public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
        this(registry, true);
    }

    public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
        this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
    }

    public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment) {
        this(registry, useDefaultFilters, environment, registry instanceof ResourceLoader ? (ResourceLoader)registry : null);
    }

    public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment, @Nullable ResourceLoader resourceLoader) {
        // 初始化默认的bean定义
        this.beanDefinitionDefaults = new BeanDefinitionDefaults();
        this.beanNameGenerator = AnnotationBeanNameGenerator.INSTANCE;
        this.scopeMetadataResolver = new AnnotationScopeMetadataResolver();
        this.includeAnnotationConfig = true;
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        this.registry = registry;
        // 设置默认过滤器
        if (useDefaultFilters) {
            this.registerDefaultFilters();
        }
        // 设置环境变量
        this.setEnvironment(environment);
        // 设置资源加载
        this.setResourceLoader(resourceLoader);
    }
    ...
}

3.注册指定配置文件为bean

利用第二步已生成的reader,扫描自定义的配置文件生成beanDefinition

public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {
   public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
        this();
        // 注册自定义配置文件
        this.register(componentClasses);
        this.refresh();
    }
    ...
}
        this();
        // 注册自定义配置文件
        this.register(componentClasses);
        this.refresh();
    }
    ...
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值