Spring源码解析 – AnnotationConfigApplicationContext容器创建过程

Spring在BeanFactory基础上提供了一些列具体容器的实现,其中AnnotationConfigApplicationContext是一个用来管理注解bean的容器,从AnnotationConfigApplicationContext的实现结构图中可以看出:

  • AnnotationConfigApplicationContext继承GenericApplicationContext这个通用应用上下文,GenericApplicationContext内部定义了一个DefaultListableBeanFactory实例,GenericApplicationContext实现了BeanDefinitionRegistry接口,所以可以通过AnnotationConfigApplicationContext实例注册bean defintion,然后调用refresh()方法来初始化上下文。
  • AnnotationConfigApplicationContext继承AbstractApplicationContext,AbstractApplicationContext提供了ApplicationContext的抽象实现。

下面通过一个示例分析AnnotationConfigApplicationContext的初始化过程:

 AnnotationConfigApplicationContext applicationContext  = new AnnotationConfigApplicationContext(ExtensionConfig.class);

构造函数:

1 //1. 初始化bean读取器和扫描器;
2     //调用父类GenericApplicationContext无参构造函数,初始化一个BeanFactory: DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory()
3    this();
4 //2.注册bean配置类
5    register(annotatedClasses);
6 //3.刷新上下文
7    refresh();
8 } 

1. this() 初始化bean读取器和扫描器

1 public AnnotationConfigApplicationContext() {
2     //在IOC容器中初始化一个 注解bean读取器AnnotatedBeanDefinitionReader
3    this.reader = new AnnotatedBeanDefinitionReader(this);
4    //在IOC容器中初始化一个 按类路径扫描注解bean的 扫描器
5    this.scanner = new ClassPathBeanDefinitionScanner(this);

GenericApplicationContext部分代码:

 1 public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
 2 private final DefaultListableBeanFactory beanFactory;
 3
 4 //初始化一个BeanFactory
 5 public GenericApplicationContext() {
 6       this.beanFactory = new DefaultListableBeanFactory();
 7     }
 8
 9     …
10 }

2. register(annotatedClasses)

注册bean配置类, AnnotationConfigApplicationContext容器通过AnnotatedBeanDefinitionReader的register方法实现注解bean的读取,具体源码如下:

AnnotationConfigApplicationContext.java中register方法

 1 //按指定bean配置类读取bean
 2 public void register(Class<?>... annotatedClasses) {
 3    for (Class<?> annotatedClass : annotatedClasses) {
 4       registerBean(annotatedClass);
 5    }
 6 }
 7
 8 public void registerBean(Class<?> annotatedClass) {
 9    doRegisterBean(annotatedClass, null, null, null);
10 }
11
12 //核心实现逻辑
13 <T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
14       @Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
15     //将Bean配置类信息转成容器中AnnotatedGenericBeanDefinition数据结构, AnnotatedGenericBeanDefinition继承自BeanDefinition作用是定义一个bean的数据结构,下面的getMetadata可以获取到该bean上的注解信息
16    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
17     //@Conditional装配条件判断是否需要跳过注册
18    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
19       return;
20    }
21    //@param instanceSupplier a callback for creating an instance of the bean
22    //设置回调
23    abd.setInstanceSupplier(instanceSupplier);
24    //解析bean作用域(单例或者原型),如果有@Scope注解,则解析@Scope,没有则默认为singleton
25    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
26   //作用域写回BeanDefinition数据结构, abd中缺损的情况下为空,将默认值singleton重新赋值到abd
27    abd.setScope(scopeMetadata.getScopeName());
28   //生成bean配置类beanName
29    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
30    //通用注解解析到abd结构中,主要是处理Lazy, primary DependsOn, Role ,Description这五个注解
31    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
32   //@param qualifiers specific qualifier annotations to consider, if any, in addition to qualifiers at the bean class level
33   // @Qualifier特殊限定符处理,
34    if (qualifiers != null) {
35       for (Class<? extends Annotation> qualifier : qualifiers) {
36          if (Primary.class == qualifier) {
37     // 如果配置@Primary注解,则设置当前Bean为自动装配autowire时首选bean
38             abd.setPrimary(true);
39          }
40   else if (Lazy.class == qualifier) {
41   //设置当前bean为延迟加载
42             abd.setLazyInit(true);
43          }
44          else {
45       //其他注解,则添加到abd结构中
46             abd.addQualifier(new AutowireCandidateQualifier(qualifier));
47          }
48       }
49    }
50   //自定义bean注册,通常用在applicationContext创建后,手动向容器中一lambda表达式的方式注册bean,
51   //比如:applicationContext.registerBean(UserService.class, () -> new UserService());
52    for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
53     //自定义bean添加到BeanDefinition
54       customizer.customize(abd);
55    }
56    //根据beanName和bean定义信息封装一个beanhold,heanhold其实就是一个 beanname和BeanDefinition的映射
57    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
58   //创建代理对象
59    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
60   // BeanDefinitionReaderUtils.registerBeanDefinition 内部通过DefaultListableBeanFactory.registerBeanDefinition(String beanName, BeanDefinition beanDefinition)按名称将bean定义信息注册到容器中,
61   // 实际上DefaultListableBeanFactory内部维护一个Map<String, BeanDefinition>类型变量beanDefinitionMap,用于保存注bean定义信息(beanname 和 beandefine映射)
62    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
63 }

register方法重点完成了bean配置类本身的解析和注册,处理过程可以分为以下几个步骤:

  1. 根据bean配置类,使用BeanDefinition解析Bean的定义信息,主要是一些注解信息
  2. Bean作用域的处理,默认缺少@Scope注解,解析成单例
  3. 借助AnnotationConfigUtils工具类解析通用注解
  4. 将bean定义信息已beanname,beandifine键值对的形式注册到ioc容器中

3. refresh()刷新上下文

refresh方法在AbstractApplicationContext容器中实现,refresh()方法的作用加载或者刷新当前的配置信息,如果已经存在spring容器,则先销毁之前的容器,重新创建spring容器,载入bean定义,完成容器初始化工作,所以可以看出AnnotationConfigApplicationContext容器是通过调用其父类AbstractApplicationContext的refresh()函数启动整个IoC容器完成对Bean定义的载入。

AbstractApplicationContext.java中refresh方法的实现代码如下:

 1 public void refresh() throws BeansException, IllegalStateException {
 2    synchronized (this.startupShutdownMonitor) {
 3       //1.刷新前的预处理
 4       prepareRefresh();
 5
 6       //2.获取刷新后的内部Bean工厂
 7       ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
 8
 9       //3.BeanFactory的预准备工作
10       prepareBeanFactory(beanFactory);
11
12       try {
13          // BeanFactory准备工作完成后,可以做一些后置处理工作,
14       // 4.空方法,用于在容器的子类中扩展
15          postProcessBeanFactory(beanFactory);
16
17          // 5. 执行BeanFactoryPostProcessor的方法,BeanFactory的后置处理器,在BeanFactory标准初始化之后执行的
18          invokeBeanFactoryPostProcessors(beanFactory);
19
20          // 6. 注册BeanPostProcessor(Bean的后置处理器),用于拦截bean创建过程
21          registerBeanPostProcessors(beanFactory);
22
23          // 7. 初始化MessageSource组件(做国际化功能;消息绑定,消息解析)
24          initMessageSource();
25
26          // 8. 初始化事件派发器
27          initApplicationEventMulticaster();
28
29          // 9.空方法,可以用于子类实现在容器刷新时自定义逻辑
30          onRefresh();
31
32          // 10. 注册时间监听器,将所有项目里面的ApplicationListener注册到容器中来
33          registerListeners();
34
35          // 11. 初始化所有剩下的单实例bean,单例bean在初始化容器时创建,原型bean在获取时(getbean)时创建
36          finishBeanFactoryInitialization(beanFactory);
37
38          // 12. 完成BeanFactory的初始化创建工作,IOC容器就创建完成;
39          finishRefresh();
40       }
41
42       catch (BeansException ex) {
43          if (logger.isWarnEnabled()) {
44             logger.warn("Exception encountered during context initialization - " +
45                   "cancelling refresh attempt: " + ex);
46          }
47
48          // Destroy already created singletons to avoid dangling resources.
49          destroyBeans();
50
51          // Reset ‘active‘ flag.
52          cancelRefresh(ex);
53
54          // Propagate exception to caller.
55          throw ex;
56       }
57
58       finally {
59          // Reset common introspection caches in Spring‘s core, since we
60          // might not ever need metadata for singleton beans anymore...
61          resetCommonCaches();
62       }
63    }
64 }

具体分析refresh中的函数逻辑:

1.   刷新预处理:

AbstractApplicationContext. prepareRefresh ()方法:

 1 protected void prepareRefresh() {
 2   //设置容器启动时间
 3    this.startupDate = System.currentTimeMillis();
 4   //启动标识
 5    this.closed.set(false);
 6    this.active.set(true);
 7
 8    if (logger.isInfoEnabled()) {
 9       logger.info("Refreshing " + this);
10    }
11
12    //空方法,用于子容器自定义个性化的属性设置方法
13    initPropertySources();
14    //检验属性的合法等
15    getEnvironment().validateRequiredProperties();
16
17    //保存容器中的一些早期的事件
18    this.earlyApplicationEvents = new LinkedHashSet<>();
19 }

2. 获取刷新后的内部Bean工厂,obtainFreshBeanFactory方法为内部bean工厂重新生成id,并返回bean工厂

AbstractApplicationContext. obtainFreshBeanFactory()方法

 1 protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
 2   //为beanfactory生成唯一序列化id,beanfactory已经在GenericApplicationContext构造函数中初始化了,refreshBeanFactory的逻辑在AbstractApplicationContext的实现类GenericApplicationContext中
 3  refreshBeanFactory();
 4   //获取beanfactory
 5    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
 6    if (logger.isDebugEnabled()) {
 7       logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
 8    }
 9    return beanFactory;
10 }

GenericApplicationContext.refreshBeanFactory()实现代码

1 protected final void refreshBeanFactory() throws IllegalStateException {
2    if (!this.refreshed.compareAndSet(false, true)) {
3       throw new IllegalStateException(
4             "GenericApplicationContext does not support multiple refresh attempts: just call ‘refresh‘ once");
5    }
6   //生成一个序列化id
7    this.beanFactory.setSerializationId(getId());
8 }

这里使用AbstractApplicationContext. refreshBeanFactory()在不同实现容器中有点区别,如果是以xml方式配置bean,会使用AbstractRefreshableApplicationContext容器中的实现,该容器中实现xml配置文件定位,并通过BeanDefinition载入和解析xml配置文件。

而如果是注解的方式,则并没有解析项目包下的注解,而是通过在refresh()方法中执行ConfigurationClassPostProcessor后置处理器完成对bean的加载.

3.BeanFactory的预准备工作

prepareBeanFactory主要完成beanFactory的一些属性设置

 1 protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
 2    // Tell the internal bean factory to use the context‘s class loader etc.
 3    beanFactory.setBeanClassLoader(getClassLoader());  //设置类加载器
 4    beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); //bean表达式解析器
 5    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
 6
 7    // Configure the bean factory with context callbacks.
 8    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));  //添加一个BeanPostProcessor实现ApplicationContextAwareProcessor
 9 //设置忽略的自动装配接口,表示这些接口的实现类不允许通过接口自动注入
10    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
11    beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
12    beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
13    beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
14    beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
15    beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
16
17    // BeanFactory interface not registered as resolvable type in a plain factory.
18    // MessageSource registered (and found for autowiring) as a bean.
19 //注册可以自动装配的组件,就是可以在任何组件中允许自动注入的组件
20    beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
21    beanFactory.registerResolvableDependency(ResourceLoader.class, this);
22    beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
23    beanFactory.registerResolvableDependency(ApplicationContext.class, this);
24
25    // Register early post-processor for detecting inner beans as ApplicationListeners.
26    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
27
28    //添加编译时的AspectJ
29    if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
30       beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
31       // Set a temporary ClassLoader for type matching.
32       beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
33    }
34
35    // 给beanfactory容器中注册组件ConfigurableEnvironment、systemProperties、systemEnvironment
36    if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
37       beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
38    }
39    if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
40       beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
41    }
42    if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
43       beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
44    }
45 }

5.invokeBeanFactoryPostProcessors 执行bean工厂后置处理器

AbstractApplicationContext. invokeBeanFactoryPostProcessors方法实现:

 1 protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
 2    PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
 3
 4    // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
 5    // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
 6    if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
 7       beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
 8       beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
 9    }
10 }

invokeBeanFactoryPostProcessors方法内部执行实现了BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor这两个接口的Processor,先获取所有BeanDefinitionRegistryPostProcessor的实现,按优先级执行(是否实现PriorityOrdered优先级接口,是否实现Ordered顺序接口);再以相同的策略执行所有BeanFactoryPostProcessor的实现。

PostProcessorRegistrationDelegate. invokeBeanFactoryPostProcessors实现:

  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
 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       List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
 29
 30       // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
 31       String[] postProcessorNames =
 32             beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 33       for (String ppName : postProcessorNames) {
 34          if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
 35             currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 36             processedBeans.add(ppName);
 37          }
 38       }
 39       sortPostProcessors(currentRegistryProcessors, beanFactory);
 40       registryProcessors.addAll(currentRegistryProcessors);
 41       invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 42       currentRegistryProcessors.clear();
 43
 44       // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
 45       postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 46       for (String ppName : postProcessorNames) {
 47          if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
 48             currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 49             processedBeans.add(ppName);
 50          }
 51       }
 52       sortPostProcessors(currentRegistryProcessors, beanFactory);
 53       registryProcessors.addAll(currentRegistryProcessors);
 54       invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 55       currentRegistryProcessors.clear();
 56
 57       // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
 58       boolean reiterate = true;
 59       while (reiterate) {
 60          reiterate = false;
 61          postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 62          for (String ppName : postProcessorNames) {
 63             if (!processedBeans.contains(ppName)) {
 64                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 65                processedBeans.add(ppName);
 66                reiterate = true;
 67             }
 68          }
 69          sortPostProcessors(currentRegistryProcessors, beanFactory);
 70          registryProcessors.addAll(currentRegistryProcessors);
 71          invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 72          currentRegistryProcessors.clear();
 73       }
 74
 75       // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
 76       invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
 77       invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
 78    }
 79
 80    else {
 81       // Invoke factory processors registered with the context instance.
 82       invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
 83    }
 84
 85    // Do not initialize FactoryBeans here: We need to leave all regular beans
 86    // uninitialized to let the bean factory post-processors apply to them!
 87    String[] postProcessorNames =
 88          beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
 89
 90    // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
 91    // Ordered, and the rest.
 92    List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
 93    List<String> orderedPostProcessorNames = new ArrayList<>();
 94    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
 95    for (String ppName : postProcessorNames) {
 96       if (processedBeans.contains(ppName)) {
 97          // skip - already processed in first phase above
 98       }
 99       else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
100          priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
101       }
102       else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
103          orderedPostProcessorNames.add(ppName);
104       }
105       else {
106          nonOrderedPostProcessorNames.add(ppName);
107       }
108    }
109
110    // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
111    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
112    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
113
114    // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
115    List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
116    for (String postProcessorName : orderedPostProcessorNames) {
117       orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
118    }
119    sortPostProcessors(orderedPostProcessors, beanFactory);
120    invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
121
122    // Finally, invoke all other BeanFactoryPostProcessors.
123    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
124    for (String postProcessorName : nonOrderedPostProcessorNames) {
125       nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
126    }
127    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
128
129    // Clear cached merged bean definitions since the post-processors might have
130    // modified the original metadata, e.g. replacing placeholders in values...
131    beanFactory.clearMetadataCache();
132 }

这里面在处理BeanDefinitionRegistryPostProcessors时有一个非常重要的过程,AnnotationConfigApplicationContext构造函数在初始化reader时为内部beanFactory容器初始化了一个id为org.springframework.context.annotation.internalConfigurationAnnotationProcessor的组件,这是一个ConfigurationClassPostProcessor组件,用来处理添加@Configuration注解的类,并将Bean定义注册到BeanFactory中。

6.注册BeanPostProcessor(Bean的后置处理器),用于拦截bean创建过程

注册后置处理器的大致逻辑是:

  1.获取所有的 BeanPostProcessor

  2.根据处理器实现的接口区分出4中类型:

    a.实现PriorityOrdered接口的处理器

    b.实现Ordered接口的处理器,

    c.实现MergedBeanDefinitionPostProcessor接口的处理器,

    d.普通后置处理器

  3.按这个4中类型依次注册到容器中

  4.注册一个特殊的后置处理器ApplicationListenerDetector,ApplicationListenerDetector本身也实现了MergedBeanDefinitionPostProcessor接口,有个问题,这个为什么没有在上面c,d之间注册,而是放到最后?

AbstractApplicationContext .registerBeanPostProcessors(beanFactory);实现逻辑:

1 protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
2    PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
3 }
 1 public static void registerBeanPostProcessors(
 2       ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
 3
 4    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
 5
 6    // Register BeanPostProcessorChecker that logs an info message when
 7    // a bean is created during BeanPostProcessor instantiation, i.e. when
 8    // a bean is not eligible for getting processed by all BeanPostProcessors.
 9    int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
10    beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
11
12    // Separate between BeanPostProcessors that implement PriorityOrdered,
13    // Ordered, and the rest.
14 //按优先级分类
15    List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
16    List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
17    List<String> orderedPostProcessorNames = new ArrayList<>();
18    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
19    for (String ppName : postProcessorNames) {
20       if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
21          BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
22          priorityOrderedPostProcessors.add(pp);
23          if (pp instanceof MergedBeanDefinitionPostProcessor) {
24             internalPostProcessors.add(pp);
25          }
26       }
27       else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
28          orderedPostProcessorNames.add(ppName);
29       }
30       else {
31          nonOrderedPostProcessorNames.add(ppName);
32       }
33    }
34
35    //先注册实现PriorityOrdered接口的处理器,添加到beanfactory容器中beanFactory.addBeanPostProcessor(postProcessor);
36    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
37    registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
38
39    //注册实现Ordered接口的处理器
40    List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
41    for (String ppName : orderedPostProcessorNames) {
42       BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
43       orderedPostProcessors.add(pp);
44       if (pp instanceof MergedBeanDefinitionPostProcessor) {
45          internalPostProcessors.add(pp);
46       }
47    }
48    sortPostProcessors(orderedPostProcessors, beanFactory);
49    registerBeanPostProcessors(beanFactory, orderedPostProcessors);
50
51    // 注册没有实现Ordered或PriorityOrdered的处理器(nonOrderedPostProcessors)
52    List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
53    for (String ppName : nonOrderedPostProcessorNames) {
54       BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
55       nonOrderedPostProcessors.add(pp);
56       if (pp instanceof MergedBeanDefinitionPostProcessor) {
57          internalPostProcessors.add(pp);
58       }
59    }
60    registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
61
62    // Finally, re-register all internal BeanPostProcessors.
63   //最后,重新注册所有internal BeanPostProcessors(实现MergedBeanDefinitionPostProcessor接口的后置处理器
64 65    sortPostProcessors(internalPostProcessors, beanFactory);
66    registerBeanPostProcessors(beanFactory, internalPostProcessors);
67
68    //注册ApplicationListenerDetector,用于Bean创建完时检查是否是ApplicationListener
69    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
70 }

7.初始化MessageSource组件(做国际化功能;消息绑定,消息解析)

AbstractApplicationContext .initMessageSource()方法实现代码:

 1 protected void initMessageSource() {
 2 //获取beanFactory
 3    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
 4 //判断是否已经存在id为MESSAGE_SOURCE_BEAN_NAME的组件
 5    if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
 6       this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
 7       // Make MessageSource aware of parent MessageSource.
 8       if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
 9          HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
10          if (hms.getParentMessageSource() == null) {
11             // Only set parent context as parent MessageSource if no parent MessageSource
12             // registered already.
13             hms.setParentMessageSource(getInternalParentMessageSource());
14          }
15       }
16       if (logger.isDebugEnabled()) {
17          logger.debug("Using MessageSource [" + this.messageSource + "]");
18       }
19    }
20    else {
21       // Use empty MessageSource to be able to accept getMessage calls.
22       DelegatingMessageSource dms = new DelegatingMessageSource();
23       dms.setParentMessageSource(getInternalParentMessageSource());
24       this.messageSource = dms;
25       beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
26       if (logger.isDebugEnabled()) {
27          logger.debug("Unable to locate MessageSource with name ‘" + MESSAGE_SOURCE_BEAN_NAME +
28                "‘: using default [" + this.messageSource + "]");
29       }
30    }
31 }

8.初始化事件派发器

AbstractApplicationContext .initApplicationEventMulticaster()方法实现逻辑

 1 protected void initApplicationEventMulticaster() {
 2 //获取BeanFactory
 3    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
 4 //如果有配置beanName为applicationEventMulticaster的事件派发器,则将其赋给容器中的applicationEventMulticaster对象
 5    if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
 6       this.applicationEventMulticaster =
 7             beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
 8       if (logger.isDebugEnabled()) {
 9          logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
10       }
11    }
12    else {
13 //不存在,则创建一个SimpleApplicationEventMulticaster事件派发器,并注册到beanfactory中
14       this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
15       beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
16       if (logger.isDebugEnabled()) {
17          logger.debug("Unable to locate ApplicationEventMulticaster with name ‘" +
18                APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
19                "‘: using default [" + this.applicationEventMulticaster + "]");
20       }
21    }
22 }

10. 注册时间监听器,将项目里面的ApplicationListener注册到容器中来

registerListeners方法主要实现将事件监听器添加到IOC容器中的事件派发器中,并在最后做了一个事件发布的逻辑(如果之前的步骤有产生事件,则将earlyApplicationEvents中保存的事件逐一发布)

AbstractApplicationContext .registerListeners()方法实现逻辑:

 1 protected void registerListeners() {
 2    // Register statically specified listeners first.
 3    for (ApplicationListener<?> listener : getApplicationListeners()) {
 4       getApplicationEventMulticaster().addApplicationListener(listener);
 5    }
 6
 7    // Do not initialize FactoryBeans here: We need to leave all regular beans
 8    // uninitialized to let post-processors apply to them!
 9    String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
10    for (String listenerBeanName : listenerBeanNames) {
11       getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
12    }
13
14    // Publish early application events now that we finally have a multicaster...
15    Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
16    this.earlyApplicationEvents = null;
17    if (earlyEventsToProcess != null) {
18       for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
19          getApplicationEventMulticaster().multicastEvent(earlyEvent);
20       }
21    }
22 }

11. 初始化所有剩下的单实例bean,单例bean在初始化容器时创建,原型bean在获取时(getbean)时创建

AbstractApplicationContext.finishBeanFactoryInitialization(beanFactory);方法实现代码:

 1 protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
 2    //组件转换器相关
 3    if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
 4          beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
 5       beanFactory.setConversionService(
 6             beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
 7    }
 8
 9    // Register a default embedded value resolver if no bean post-processor
10    // (such as a PropertyPlaceholderConfigurer bean) registered any before:
11    // at this point, primarily for resolution in annotation attribute values.
12    if (!beanFactory.hasEmbeddedValueResolver()) {
13       beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
14    }
15
16    //aspectj相关.
17    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
18    for (String weaverAwareName : weaverAwareNames) {
19       getBean(weaverAwareName);
20    }
21
22    // Stop using the temporary ClassLoader for type matching.
23    beanFactory.setTempClassLoader(null);
24
25    // Allow for caching all bean definition metadata, not expecting further changes.
26    beanFactory.freezeConfiguration();
27
28    // 初始化后剩下的单实例bean
29    beanFactory.preInstantiateSingletons();
30 }

DefaultListableBeanFactory. preInstantiateSingletons()方法实现逻辑:

 1 public void preInstantiateSingletons() throws BeansException {
 2    if (logger.isDebugEnabled()) {
 3       logger.debug("Pre-instantiating singletons in " + this);
 4    }
 5
 6    // Iterate over a copy to allow for init methods which in turn register new bean definitions.
 7    // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
 8   //容器中所有bean名称
 9    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
10
11    // Trigger initialization of all non-lazy singleton beans...
12    for (String beanName : beanNames) {
13   //获取Bean的定义信息;RootBeanDefinition
14       RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
15   //非抽象,单例,非延迟加载
16       if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
17   //是否是FactoryBean
18          if (isFactoryBean(beanName)) {
19     // 通过"&beanName"获取工厂Bean实例
20             Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
21             if (bean instanceof FactoryBean) {
22                final FactoryBean<?> factory = (FactoryBean<?>) bean;
23                boolean isEagerInit;
24                if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
25                   isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
26                               ((SmartFactoryBean<?>) factory)::isEagerInit,
27                         getAccessControlContext());
28                }
29                else {
30                   isEagerInit = (factory instanceof SmartFactoryBean &&
31                         ((SmartFactoryBean<?>) factory).isEagerInit());
32                }
33                if (isEagerInit) {
34                   getBean(beanName);
35                }
36             }
37          }
38          else {
39       //不是FactoryBean,则利用getBean(beanName)实例化bean
40             getBean(beanName);
41          }
42       }
43    }
44
45    // Trigger post-initialization callback for all applicable beans...
46    for (String beanName : beanNames) {
47       Object singletonInstance = getSingleton(beanName);
48       if (singletonInstance instanceof SmartInitializingSingleton) {
49          final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
50          if (System.getSecurityManager() != null) {
51             AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
52                smartSingleton.afterSingletonsInstantiated();
53                return null;
54             }, getAccessControlContext());
55          }
56          else {
57             smartSingleton.afterSingletonsInstantiated();
58          }
59       }
60    }
61 }

12. 完成BeanFactory的初始化创建工作,IOC容器就创建完成

AbstractApplicationContext.finishRefresh()实现逻辑:

 1 protected void finishRefresh() {
 2    // Clear context-level resource caches (such as ASM metadata from scanning).
 3    clearResourceCaches();
 4
 5    //初始化和生命周期有关的后置处理器LifecycleProcessor,默认DefaultLifecycleProcessor
 6    initLifecycleProcessor();
 7
 8    // 回调生命周期处理器
 9    getLifecycleProcessor().onRefresh();
10
11    //发布容器刷新完成事件:ContextRefreshedEvent
12    publishEvent(new ContextRefreshedEvent(this));
13
14    LiveBeansView.registerApplicationContext(this);
15 }

  以上基本分析了AnnotationConfigApplicationContext容器的初始化过程, Spring容器在启动过程中,会先保存所有注册进来的Bean的定义信息;Spring容器根据条件创建Bean实例,区分单例,还是原型,后置处理器等(后置处理器会在容器创建过程中通过getBean创建,并执行相应的逻辑);Spring容器在创建bean实例后,会使用多种后置处理器来增加bean的功能,比如处理自动注入,AOP,异步,这种后置处理器机制也丰富了bean的功能。

原文地址:https://www.cnblogs.com/ashleyboy/p/9662119.html

时间: 2024-10-01 04:25:57

Spring源码解析 – AnnotationConfigApplicationContext容器创建过程的相关文章

Spring源码解析-IOC容器的实现

1.IOC容器是什么? IOC(Inversion of Control)控制反转:本来是由应用程序管理的对象之间的依赖关系,现在交给了容器管理,这就叫控制反转,即交给了IOC容器,Spring的IOC容器主要使用DI方式实现的.不需要主动查找,对象的查找.定位和创建全部由容器管理. 在程序中不创建对象.以前我们要调用一个对象的方法,首先要new一个对象.但使用IOC容器,在代码中不直接与对象连接,而是在配置文件中描述要使用哪一个对象.容器负责将这些联系在一起. IOC容器的基本功能规范是由Be

软件开发工程师(JAVA)中级考试大纲--spring源码解析

spring源码解析(1)----IOC 一.IOC容器 在Spring中,IOC容器的重要地位我们就不多说了,对于Spring的使用者而言,IOC容器实际上是什么呢?我们可以说BeanFactory就是我们看到的IoC容器,当然了Spring为我们准备了许多种IoC容器来使用,这样可以方便我们从不同的层面,不同的资源位置,不同的形式的定义信息来建立我们需要的IoC容器. 在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 

SPRING源码解析-SPRING 核心-IOC

IoC 和 AOP是Spring的核心, 是Spring系统中其他组件模块和应用开发的基础.透过这两个模块的设计和实现可以了解Spring倡导的对企业应用开发所应秉承的思路: 易用性. POJO开发企业应用, 直接依赖于Java语言,而不是容器和框架. 提升程序的可测试性,提高软件质量. 提供一致性编程模型,面向接口的编程 降低应用的负载和框架的侵入性.IoC和AOP实现. 不作为现有解决方案的替代,而是集成现有. IoC和AOP这两个核心组件,特别是IoC容器,使用户在使用Spring完成PO

Spring 源码解析之HandlerAdapter源码解析(二)

Spring 源码解析之HandlerAdapter源码解析(二) 前言 看这篇之前需要有Spring 源码解析之HandlerMapping源码解析(一)这篇的基础,这篇主要是把请求流程中的调用controller流程单独拿出来了 解决上篇文章遗留的问题 getHandler(processedRequest) 这个方法是如何查找到对应处理的HandlerExecutionChain和HandlerMapping的,比如说静态资源的处理和请求的处理肯定是不同的HandlerMapping ge

Spring 源码解析之ViewResolver源码解析(四)

Spring 源码解析之ViewResolver源码解析(四) 1 ViewResolver类功能解析 1.1 ViewResolver Interface to be implemented by objects that can resolve views by name. View state doesn't change during the running of the application, so implementations are free to cache views. I

Spring 源码解析之DispatcherServlet源码解析(五)

Spring 源码解析之DispatcherServlet源码解析(五) 前言 本文需要有前四篇文章的基础,才能够清晰易懂,有兴趣可以先看看详细的流程,这篇文章可以说是第一篇文章,也可以说是前四篇文章的的汇总,Spring的整个请求流程都是围绕着DispatcherServlet进行的 类结构图 根据类的结构来说DispatcherServlet本身也是继承了HttpServlet的,所有的请求都是根据这一个Servlet来进行转发的,同时解释了为什么需要在web.xml进行如下配置,因为Spr

Spring IoC源码解析——Bean的创建和初始化

Spring介绍 Spring(http://spring.io/)是一个轻量级的Java 开发框架,同时也是轻量级的IoC和AOP的容器框架,主要是针对JavaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框架,MyBatis框架等组合使用. IoC介绍 IoC是什么 Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控

spring源码解析之IOC容器(一)

学习优秀框架的源码,是提升个人技术水平必不可少的一个环节.如果只是停留在知道怎么用,但是不懂其中的来龙去脉,在技术的道路上注定走不长远.最近,学习了一段时间的spring源码,现在整理出来,以便日后温故知新. IOC容器是spring最核心的模块之一,是整个spring体系的基石,spring其他模块中,都需要用到IOC容器的功能.spring框架为我们提供了多种IOC容器,DefaultableBeanFact ory.FileSystemXmlApplicationContext.Class

Spring源码解析和配置文件加载

Spring类的继承结构图: Spring运用了大量的模板方法模式和策略模式,所以各位看源码的时候,务必留意,每一个继承的层次都有不同的作用,然后将相同的地方抽取出来,依赖抽象将不同的处理按照不同的策略去处理. 步骤A. 读取 Resource 文件形成 Document 模型  类图: XmlBeanFactory -> XmlBeanDefinitionReader Spring 使用 XmlBeanDefinitionReader 来读取并解析 xml 文件,XmlBeanDefiniti