查看构造函数

  1. public AnnotationConfigApplicationContext() {
  2. // 继续查看这步
  3. this.reader = new AnnotatedBeanDefinitionReader(this);
  4. this.scanner = new ClassPathBeanDefinitionScanner(this);
  5. }
  1. /**
  2. * 可以看出,将自身转换为BeanDefinitionRegistry,
  3. * 这说明AnnotationConfigApplicationContext实现了BeanDefinitionRegistry接口
  4. */
  5. public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
  6. this(registry, getOrCreateEnvironment(registry));
  7. }
  1. public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
  2. Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
  3. Assert.notNull(environment, "Environment must not be null");
  4. this.registry = registry;
  5. this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
  6. // 这一行是查看reader的关键,它将定义了最初的几个Bean的后置处理器
  7. AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
  8. }
  1. public static void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry) {
  2. registerAnnotationConfigProcessors(registry, null);
  3. }
  1. public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
  2. BeanDefinitionRegistry registry, @Nullable Object source) {
  3. // 获取BeanFacotry,即DefaultListableBeanFactory
  4. DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
  5. Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);
  6. if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
  7. //添加ConfigurationClassPostProcessor后置处理器,它是工厂级的后置处理器,它实现了BeanDefinitionRegistryPostProcessor,它继承了了BeanFactoryPostProcessor,后面的重点也会集中在这个后置处理器上。
  8. RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
  9. def.setSource(source);
  10. beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
  11. }
  12. if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
  13. // 名字是不是很熟悉,自动注入后置处理器,它是Bean级别的,实现了自动注入
  14. RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
  15. def.setSource(source);
  16. beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
  17. }
  18. if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
  19. // 也是Bean级别的后置处理器,为@PostConstruct和@PreDestroy等提供支持
  20. RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
  21. def.setSource(source);
  22. beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
  23. }
  24. // 如果存在JPA,则提供对JPA的支持,也是后置处理器
  25. if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
  26. RootBeanDefinition def = new RootBeanDefinition();
  27. def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
  28. AnnotationConfigUtils.class.getClassLoader()));
  29. }
  30. return beanDefs;
  31. }