![](https://csdnimg.cn/release/download_crawler_static/88944534/bg6.jpg)
(2) 内置组件添加
调用无参构造函数 this()完成后,Spring 容器已经构造完毕,这时需要为容器添加一些内置
组件
调 用 方 法 :
org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigPr
ocessors
主要组件:
ConfigurationClassPostProcessor:beanFactory 后置处理器,用来完成 bean 的扫描与注入
工作
AutowiredAnnotationBeanPostProcessor:bean 后置处理器,用来完成 @AutoWired 自动注
入
源码跟踪:
容器有参构造 --> this()无参
new AnnotatedBeanDefinitionReader(this) --> 构造方法中调用
AnnotationConfigUtils --> registerAnnotationConfigProcessors() 方法添加内置组件
构造方法源码:渐进调用
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
this(registry, getOrCreateEnvironment(registry));
}
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment
environment) {
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, null);
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry); // 内置组件
添加
}
内置组件添加源码:
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry, @Nullable Object source) {
DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
if (beanFactory != null) {
if (!(beanFactory.getDependencyComparator() instanceof
AnnotationAwareOrderComparator)) {
beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE)