志不强者智不达,言不信者行不果。这篇文章主要讲述Spring源码解析 – AnnotationConfigApplicationContext容器创建过程相关的知识,希望能为你提供帮助。
【Spring源码解析 – AnnotationConfigApplicationContext容器创建过程】Spring在BeanFactory基础上提供了一些列具体容器的实现,其中AnnotationConfigApplicationContext是一个用来管理注解bean的容器,从AnnotationConfigApplicationContext的实现结构图中可以看出:
- AnnotationConfigApplicationContext继承GenericApplicationContext这个通用应用上下文,GenericApplicationContext内部定义了一个DefaultListableBeanFactory实例,GenericApplicationContext实现了BeanDefinitionRegistry接口,所以可以通过AnnotationConfigApplicationContext实例注册bean defintion,然后调用refresh()方法来初始化上下文。
- AnnotationConfigApplicationContext继承AbstractApplicationContext,AbstractApplicationContext提供了ApplicationContext的抽象实现。
AnnotationConfigApplicationContext applicationContext= new AnnotationConfigApplicationContext(ExtensionConfig.class);
构造函数:
1 //1. 初始化bean读取器和扫描器; 2//调用父类GenericApplicationContext无参构造函数,初始化一个BeanFactory: DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory() 3this(); 4 //2.注册bean配置类 5register(annotatedClasses); 6 //3.刷新上下文 7refresh(); 8 }
1. this() 初始化bean读取器和扫描器
1 public AnnotationConfigApplicationContext() { 2//在IOC容器中初始化一个 注解bean读取器AnnotatedBeanDefinitionReader 3this.reader = new AnnotatedBeanDefinitionReader(this); 4//在IOC容器中初始化一个 按类路径扫描注解bean的 扫描器 5this.scanner = new ClassPathBeanDefinitionScanner(this);
GenericApplicationContext部分代码:
1 public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry { 2 private final DefaultListableBeanFactory beanFactory; 3 4 //初始化一个BeanFactory 5 public GenericApplicationContext() { 6this.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) { 3for (Class< ?> annotatedClass : annotatedClasses) { 4registerBean(annotatedClass); 5} 6 } 7 8 public void registerBean(Class< ?> annotatedClass) { 9doRegisterBean(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上的注解信息 16AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass); 17//@Conditional装配条件判断是否需要跳过注册 18if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) { 19return; 20} 21//@param instanceSupplier a callback for creating an instance of the bean 22//设置回调 23abd.setInstanceSupplier(instanceSupplier); 24//解析bean作用域(单例或者原型),如果有@Scope注解,则解析@Scope,没有则默认为singleton 25ScopeMetadata scopeMetadata = https://www.songbingjia.com/android/this.scopeMetadataResolver.resolveScopeMetadata(abd); 26//作用域写回BeanDefinition数据结构, abd中缺损的情况下为空,将默认值singleton重新赋值到abd 27abd.setScope(scopeMetadata.getScopeName()); 28//生成bean配置类beanName 29String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry)); 30//通用注解解析到abd结构中,主要是处理Lazy, primary DependsOn, Role ,Description这五个注解 31AnnotationConfigUtils.processCommonDefinitionAnnotations(abd); 32//@param qualifiers specific qualifier annotations to consider, if any, in addition to qualifiers at the bean class level 33// @Qualifier特殊限定符处理, 34if (qualifiers != null) { 35for (Class< ? extends Annotation> qualifier : qualifiers) { 36if (Primary.class == qualifier) { 37// 如果配置@Primary注解,则设置当前Bean为自动装配autowire时首选bean 38abd.setPrimary(true); 39} 40else if (Lazy.class == qualifier) { 41//设置当前bean为延迟加载 42abd.setLazyInit(true); 43} 44else { 45//其他注解,则添加到abd结构中 46abd.addQualifier(new AutowireCandidateQualifier(qualifier)); 47} 48} 49} 50//自定义bean注册,通常用在applicationContext创建后,手动向容器中一lambda表达式的方式注册bean, 51//比如:applicationContext.registerBean(UserService.class, () -> new UserService()); 52for (BeanDefinitionCustomizer customizer : definitionCustomizers) { 53//自定义bean添加到BeanDefinition 54customizer.customize(abd); 55} 56//根据beanName和bean定义信息封装一个beanhold,heanhold其实就是一个 beanname和BeanDefinition的映射 57BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName); 58//创建代理对象 59definitionHolder = 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映射) 62BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry); 63 }
register方法重点完成了bean配置类本身的解析和注册,处理过程可以分为以下几个步骤:
- 根据bean配置类,使用BeanDefinition解析Bean的定义信息,主要是一些注解信息
- Bean作用域的处理,默认缺少@Scope注解,解析成单例
- 借助AnnotationConfigUtils工具类解析通用注解
- 将bean定义信息已beanname,beandifine键值对的形式注册到ioc容器中
AbstractApplicationContext.java中refresh方法的实现代码如下:
1 public void refresh() throws BeansException, IllegalStateException { 2synchronized (this.startupShutdownMonitor) { 3//1.刷新前的预处理 4prepareRefresh(); 5 6//2.获取刷新后的内部Bean工厂 7ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); 8 9//3.BeanFactory的预准备工作 10prepareBeanFactory(beanFactory); 11 12try { 13// BeanFactory准备工作完成后,可以做一些后置处理工作, 14// 4.空方法,用于在容器的子类中扩展 15postProcessBeanFactory(beanFactory); 16 17// 5. 执行BeanFactoryPostProcessor的方法,BeanFactory的后置处理器,在BeanFactory标准初始化之后执行的 18invokeBeanFactoryPostProcessors(beanFactory); 19 20// 6. 注册BeanPostProcessor(Bean的后置处理器),用于拦截bean创建过程 21registerBeanPostProcessors(beanFactory); 22 23// 7. 初始化MessageSource组件(做国际化功能;消息绑定,消息解析) 24initMessageSource(); 25 26// 8. 初始化事件派发器 27initApplicationEventMulticaster(); 28 29// 9.空方法,可以用于子类实现在容器刷新时自定义逻辑 30onRefresh(); 31 32// 10. 注册时间监听器,将所有项目里面的ApplicationListener注册到容器中来 33registerListeners(); 34 35// 11. 初始化所有剩下的单实例bean,单例bean在初始化容器时创建,原型bean在获取时(getbean)时创建 36finishBeanFactoryInitialization(beanFactory); 37 38// 12. 完成BeanFactory的初始化创建工作,IOC容器就创建完成; 39finishRefresh(); 40} 41 42catch (BeansException ex) { 43if (logger.isWarnEnabled()) { 44logger.warn("Exception encountered during context initialization - " + 45"cancelling refresh attempt: " + ex); 46} 47 48// Destroy already created singletons to avoid dangling resources. 49destroyBeans(); 50 51// Reset ‘active‘ flag. 52cancelRefresh(ex); 53 54// Propagate exception to caller. 55throw ex; 56} 57 58finally { 59// Reset common introspection caches in Spring‘s core, since we 60// might not ever need metadata for singleton beans anymore... 61resetCommonCaches(); 62} 63} 64 }
具体分析refresh中的函数逻辑:
1. 刷新预处理:
AbstractApplicationContext. prepareRefresh ()方法:
1 protected void prepareRefresh() { 2//设置容器启动时间 3this.startupDate = System.currentTimeMillis(); 4//启动标识 5this.closed.set(false); 6this.active.set(true); 7 8if (logger.isInfoEnabled()) { 9logger.info("Refreshing " + this); 10} 11 12//空方法,用于子容器自定义个性化的属性设置方法 13initPropertySources(); 14//检验属性的合法等 15getEnvironment().validateRequiredProperties(); 16 17//保存容器中的一些早期的事件 18this.earlyApplicationEvents = new LinkedHashSet< > (); 19 }
2. 获取刷新后的内部Bean工厂,obtainFreshBeanFactory方法为内部bean工厂重新生成id,并返回bean工厂
AbstractApplicationContext. obtainFreshBeanFactory()方法
1 protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { 2//为beanfactory生成唯一序列化id,beanfactory已经在GenericApplicationContext构造函数中初始化了,refreshBeanFactory的逻辑在AbstractApplicationContext的实现类GenericApplicationContext中 3refreshBeanFactory(); 4//获取beanfactory 5ConfigurableListableBeanFactory beanFactory = getBeanFactory(); 6if (logger.isDebugEnabled()) { 7logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); 8} 9return beanFactory; 10 }
GenericApplicationContext.refreshBeanFactory()实现代码
1 protected final void refreshBeanFactory() throws IllegalStateException { 2if (!this.refreshed.compareAndSet(false, true)) { 3throw new IllegalStateException( 4"GenericApplicationContext does not support multiple refresh attempts: just call ‘refresh‘ once"); 5} 6//生成一个序列化id 7this.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. 3beanFactory.setBeanClassLoader(getClassLoader()); //设置类加载器 4beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); //bean表达式解析器 5beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); 6 7// Configure the bean factory with context callbacks. 8beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); //添加一个BeanPostProcessor实现ApplicationContextAwareProcessor 9 //设置忽略的自动装配接口,表示这些接口的实现类不允许通过接口自动注入 10beanFactory.ignoreDependencyInterface(EnvironmentAware.class); 11beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class); 12beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); 13beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); 14beanFactory.ignoreDependencyInterface(MessageSourceAware.class); 15beanFactory.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 //注册可以自动装配的组件,就是可以在任何组件中允许自动注入的组件 20beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); 21beanFactory.registerResolvableDependency(ResourceLoader.class, this); 22beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); 23beanFactory.registerResolvableDependency(ApplicationContext.class, this); 24 25// Register early post-processor for detecting inner beans as ApplicationListeners. 26beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this)); 27 28//添加编译时的AspectJ 29if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { 30beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); 31// Set a temporary ClassLoader for type matching. 32beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); 33} 34 35// 给beanfactory容器中注册组件ConfigurableEnvironment、systemProperties、systemEnvironment 36if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { 37beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); 38} 39if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { 40beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()); 41} 42if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { 43beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); 44} 45 }
5.invokeBeanFactoryPostProcessors 执行bean工厂后置处理器
AbstractApplicationContext. invokeBeanFactoryPostProcessors方法实现:
1 protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { 2PostProcessorRegistrationDelegate.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) 6if (beanFactory.getTempClassLoader() == null & & beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { 7beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); 8beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); 9} 10 }
invokeBeanFactoryPostProcessors方法内部执行实现了BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor这两个接口的Processor,先获取所有BeanDefinitionRegistryPostProcessor的实现,按优先级执行(是否实现PriorityOrdered优先级接口,是否实现Ordered顺序接口);再以相同的策略执行所有BeanFactoryPostProcessor的实现。
PostProcessorRegistrationDelegate. invokeBeanFactoryPostProcessors实现:
1 public static void invokeBeanFactoryPostProcessors( 2ConfigurableListableBeanFactory beanFactory, List< BeanFactoryPostProcessor> beanFactoryPostProcessors) { 3 4// Invoke BeanDefinitionRegistryPostProcessors first, if any. 5Set< String> processedBeans = new HashSet< > (); 6 7if (beanFactory instanceof BeanDefinitionRegistry) { 8BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; 9List< BeanFactoryPostProcessor> regularPostProcessors = new ArrayList< > (); 10List< BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList< > (); 11 12for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { 13if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { 14BeanDefinitionRegistryPostProcessor registryProcessor = 15(BeanDefinitionRegistryPostProcessor) postProcessor; 16registryProcessor.postProcessBeanDefinitionRegistry(registry); 17registryProcessors.add(registryProcessor); 18} 19else { 20regularPostProcessors.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. 28List< BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList< > (); 29 30// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. 31String[] postProcessorNames = 32beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); 33for (String ppName : postProcessorNames) { 34if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { 35currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); 36processedBeans.add(ppName); 37} 38} 39sortPostProcessors(currentRegistryProcessors, beanFactory); 40registryProcessors.addAll(currentRegistryProcessors); 41invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); 42currentRegistryProcessors.clear(); 43 44// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered. 45postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); 46for (String ppName : postProcessorNames) { 47if (!processedBeans.contains(ppName) & & beanFactory.isTypeMatch(ppName, Ordered.class)) { 48currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); 49processedBeans.add(ppName); 50} 51} 52sortPostProcessors(currentRegistryProcessors, beanFactory); 53registryProcessors.addAll(currentRegistryProcessors); 54invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); 55currentRegistryProcessors.clear(); 56 57// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear. 58boolean reiterate = true; 59while (reiterate) { 60reiterate = false; 61postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); 62for (String ppName : postProcessorNames) { 63if (!processedBeans.contains(ppName)) { 64currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); 65processedBeans.add(ppName); 66reiterate = true; 67} 68} 69sortPostProcessors(currentRegistryProcessors, beanFactory); 70registryProcessors.addAll(currentRegistryProcessors); 71invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); 72currentRegistryProcessors.clear(); 73} 74 75// Now, invoke the postProcessBeanFactory callback of all processors handled so far. 76invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); 77invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); 78} 79 80else { 81// Invoke factory processors registered with the context instance. 82invokeBeanFactoryPostProcessors(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! 87String[] postProcessorNames = 88beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); 89 90// Separate between BeanFactoryPostProcessors that implement PriorityOrdered, 91// Ordered, and the rest. 92List< BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList< > (); 93List< String> orderedPostProcessorNames = new ArrayList< > (); 94List< String> nonOrderedPostProcessorNames = new ArrayList< > (); 95for (String ppName : postProcessorNames) { 96if (processedBeans.contains(ppName)) { 97// skip - already processed in first phase above 98} 99else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { 100priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); 101} 102else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { 103orderedPostProcessorNames.add(ppName); 104} 105else { 106nonOrderedPostProcessorNames.add(ppName); 107} 108} 109 110// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. 111sortPostProcessors(priorityOrderedPostProcessors, beanFactory); 112invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); 113 114// Next, invoke the BeanFactoryPostProcessors that implement Ordered. 115List< BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList< > (); 116for (String postProcessorName : orderedPostProcessorNames) { 117orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); 118} 119sortPostProcessors(orderedPostProcessors, beanFactory); 120invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); 121 122// Finally, invoke all other BeanFactoryPostProcessors. 123List< BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList< > (); 124for (String postProcessorName : nonOrderedPostProcessorNames) { 125nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); 126} 127invokeBeanFactoryPostProcessors(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... 131beanFactory.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) { 2PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this); 3 }
1 public static void registerBeanPostProcessors( 2ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) { 3 4String[] 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. 9int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; 10beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)); 11 12// Separate between BeanPostProcessors that implement PriorityOrdered, 13// Ordered, and the rest. 14 //按优先级分类 15List< BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList< > (); 16List< BeanPostProcessor> internalPostProcessors = new ArrayList< > (); 17List< String> orderedPostProcessorNames = new ArrayList< > (); 18List< String> nonOrderedPostProcessorNames = new ArrayList< > (); 19for (String ppName : postProcessorNames) { 20if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { 21BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); 22priorityOrderedPostProcessors.add(pp); 23if (pp instanceof MergedBeanDefinitionPostProcessor) { 24internalPostProcessors.add(pp); 25} 26} 27else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { 28orderedPostProcessorNames.add(ppName); 29} 30else { 31nonOrderedPostProcessorNames.add(ppName); 32} 33} 34 35//先注册实现PriorityOrdered接口的处理器,添加到beanfactory容器中beanFactory.addBeanPostProcessor(postProcessor); 36sortPostProcessors(priorityOrderedPostProcessors, beanFactory); 37registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors); 38 39//注册实现Ordered接口的处理器 40List< BeanPostProcessor> orderedPostProcessors = new ArrayList< > (); 41for (String ppName : orderedPostProcessorNames) { 42BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); 43orderedPostProcessors.add(pp); 44if (pp instanceof MergedBeanDefinitionPostProcessor) { 45internalPostProcessors.add(pp); 46} 47} 48sortPostProcessors(orderedPostProcessors, beanFactory); 49registerBeanPostProcessors(beanFactory, orderedPostProcessors); 50 51// 注册没有实现Ordered或PriorityOrdered的处理器(nonOrderedPostProcessors) 52List< BeanPostProcessor> nonOrderedPostProcessors = new ArrayList< > (); 53for (String ppName : nonOrderedPostProcessorNames) { 54BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); 55nonOrderedPostProcessors.add(pp); 56if (pp instanceof MergedBeanDefinitionPostProcessor) { 57internalPostProcessors.add(pp); 58} 59} 60registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors); 61 62// Finally, re-register all internal BeanPostProcessors. 63//最后,重新注册所有internal BeanPostProcessors(实现MergedBeanDefinitionPostProcessor接口的后置处理器 64 65sortPostProcessors(internalPostProcessors, beanFactory); 66registerBeanPostProcessors(beanFactory, internalPostProcessors); 67 68//注册ApplicationListenerDetector,用于Bean创建完时检查是否是ApplicationListener 69beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext)); 70 }
7.初始化MessageSource组件(做国际化功能;消息绑定,消息解析)
AbstractApplicationContext .initMessageSource()方法实现代码:
1 protected void initMessageSource() { 2 //获取beanFactory 3ConfigurableListableBeanFactory beanFactory = getBeanFactory(); 4 //判断是否已经存在id为MESSAGE_SOURCE_BEAN_NAME的组件 5if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) { 6this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class); 7// Make MessageSource aware of parent MessageSource. 8if (this.parent != null & & this.messageSource instanceof HierarchicalMessageSource) { 9HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource; 10if (hms.getParentMessageSource() == null) { 11// Only set parent context as parent MessageSource if no parent MessageSource 12// registered already. 13hms.setParentMessageSource(getInternalParentMessageSource()); 14} 15} 16if (logger.isDebugEnabled()) { 17logger.debug("Using MessageSource [" + this.messageSource + "]"); 18} 19} 20else { 21// Use empty MessageSource to be able to accept getMessage calls. 22DelegatingMessageSource dms = new DelegatingMessageSource(); 23dms.setParentMessageSource(getInternalParentMessageSource()); 24this.messageSource = dms; 25beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource); 26if (logger.isDebugEnabled()) { 27logger.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 3ConfigurableListableBeanFactory beanFactory = getBeanFactory(); 4 //如果有配置beanName为applicationEventMulticaster的事件派发器,则将其赋给容器中的applicationEventMulticaster对象 5if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) { 6this.applicationEventMulticaster = 7beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class); 8if (logger.isDebugEnabled()) { 9logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]"); 10} 11} 12else { 13 //不存在,则创建一个SimpleApplicationEventMulticaster事件派发器,并注册到beanfactory中 14this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory); 15beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster); 16if (logger.isDebugEnabled()) { 17logger.debug("Unable to locate ApplicationEventMulticaster with name ‘" + 18APPLICATION_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. 3for (ApplicationListener< ?> listener : getApplicationListeners()) { 4getApplicationEventMulticaster().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! 9String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false); 10for (String listenerBeanName : listenerBeanNames) { 11getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName); 12} 13 14// Publish early application events now that we finally have a multicaster... 15Set< ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents; 16this.earlyApplicationEvents = null; 17if (earlyEventsToProcess != null) { 18for (ApplicationEvent earlyEvent : earlyEventsToProcess) { 19getApplicationEventMulticaster().multicastEvent(earlyEvent); 20} 21} 22 }
11. 初始化所有剩下的单实例bean,单例bean在初始化容器时创建,原型bean在获取时(getbean)时创建
AbstractApplicationContext.finishBeanFactoryInitialization(beanFactory); 方法实现代码:
1 protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { 2//组件转换器相关 3if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) & & 4beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { 5beanFactory.setConversionService( 6beanFactory.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. 12if (!beanFactory.hasEmbeddedValueResolver()) { 13beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal)); 14} 15 16//aspectj相关. 17String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); 18for (String weaverAwareName : weaverAwareNames) { 19getBean(weaverAwareName); 20} 21 22// Stop using the temporary ClassLoader for type matching. 23beanFactory.setTempClassLoader(null); 24 25// Allow for caching all bean definition metadata, not expecting further changes. 26beanFactory.freezeConfiguration(); 27 28// 初始化后剩下的单实例bean 29beanFactory.preInstantiateSingletons(); 30 }
DefaultListableBeanFactory. preInstantiateSingletons()方法实现逻辑:
1 public void preInstantiateSingletons() throws BeansException { 2if (logger.isDebugEnabled()) { 3logger.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名称 9List< String> beanNames = new ArrayList< > (this.beanDefinitionNames); 10 11// Trigger initialization of all non-lazy singleton beans... 12for (String beanName : beanNames) { 13//获取Bean的定义信息;RootBeanDefinition 14RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); 15//非抽象,单例,非延迟加载 16if (!bd.isAbstract() & & bd.isSingleton() & & !bd.isLazyInit()) { 17//是否是FactoryBean 18if (isFactoryBean(beanName)) { 19// 通过"& beanName"获取工厂Bean实例 20Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); 21if (bean instanceof FactoryBean) { 22final FactoryBean< ?> factory = (FactoryBean< ?> ) bean; 23boolean isEagerInit; 24if (System.getSecurityManager() != null & & factory instanceof SmartFactoryBean) { 25isEagerInit = AccessController.doPrivileged((PrivilegedAction< Boolean> ) 26((SmartFactoryBean< ?> ) factory)::isEagerInit, 27getAccessControlContext()); 28} 29else { 30isEagerInit = (factory instanceof SmartFactoryBean & & 31((SmartFactoryBean< ?> ) factory).isEagerInit()); 32} 33if (isEagerInit) { 34getBean(beanName); 35} 36} 37} 38else { 39//不是FactoryBean,则利用getBean(beanName)实例化bean 40getBean(beanName); 41} 42} 43} 44 45// Trigger post-initialization callback for all applicable beans... 46for (String beanName : beanNames) { 47Object singletonInstance = getSingleton(beanName); 48if (singletonInstance instanceof SmartInitializingSingleton) { 49final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance; 50if (System.getSecurityManager() != null) { 51AccessController.doPrivileged((PrivilegedAction< Object> ) () -> { 52smartSingleton.afterSingletonsInstantiated(); 53return null; 54}, getAccessControlContext()); 55} 56else { 57smartSingleton.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). 3clearResourceCaches(); 4 5//初始化和生命周期有关的后置处理器LifecycleProcessor,默认DefaultLifecycleProcessor 6initLifecycleProcessor(); 7 8// 回调生命周期处理器 9getLifecycleProcessor().onRefresh(); 10 11//发布容器刷新完成事件:ContextRefreshedEvent 12publishEvent(new ContextRefreshedEvent(this)); 13 14LiveBeansView.registerApplicationContext(this); 15 }
以上基本分析了AnnotationConfigApplicationContext容器的初始化过程, Spring容器在启动过程中,会先保存所有注册进来的Bean的定义信息;Spring容器根据条件创建Bean实例,区分单例,还是原型,后置处理器等(后置处理器会在容器创建过程中通过getBean创建,并执行相应的逻辑);Spring容器在创建bean实例后,会使用多种后置处理器来增加bean的功能,比如处理自动注入,AOP,异步,这种后置处理器机制也丰富了bean的功能。
推荐阅读
- Android SharedPreferences
- Android-自定义仿QQ列表Item滑动
- 迈向高阶(优秀Android程序员必知必会的网络基础)
- Android PDA扫描枪广播接搜条码并使用
- 高德定位腾讯定位在APP上无法开启定位权限的解决方案
- Android开发资源收集
- Android View 的事件分发原理解析
- mybatis学习 十六 auto_mapping实现连表查询
- Android Studio中进行单元测试