Spring IOC容器 源码解析系列,建议你们按顺序阅读,欢迎讨论web
(spring源码均为4.1.6.RELEASE版本)spring
在实际应用中使用Spring框架,大多数都不会使用BeanFactory的方式来构建Spring容器,由于Spring容器提供了一个更加简易而强大的方式——ApplicationContext。ApplicationContext也是一个接口,不只继承了BeanFactory的功能特性,并且支持了其余高级容器的特性。先来看它的继承结构:bootstrap
从继承的接口能够看出它支持了下面的几个特性:app
ApplicationContext自己提供的方法很是简单,只定义了id和名称的一些信息以及内部BeanFactory的get方法。框架
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver { // 惟一id String getId(); // 所属应用的名称 String getApplicationName(); // 显示名称 String getDisplayName(); // 启动时间 long getStartupDate(); // 父类ApplicationContext ApplicationContext getParent(); // 内部BeanFactory AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}ide
ApplicationContext的实现类有不少,经常使用的有:函数
ClassPathXmlApplicationContext和FileSystemXmlApplicationContext只是加载资源文件的方式不一样,而XmlWebApplicationContext是支持web项目,可是其底层的实现方式大部分都是一致的。下面就以经常使用的 ClassPathXmlApplicationContext来举例分析Spring容器的启动原理。post
实际的项目中启动一个Spring容器其实很简单this
new ClassPathXmlApplicationContext("applicationcontext.xml"); CountDownLatch latch = new CountDownLatch(1); latch.await();
首先建立ClassPathXmlApplicationContext对象,并传入配置文件路径,后面的两句只是用来阻塞主进程结束的。来看ClassPathXmlApplicationContext的构造方法。编码
public ClassPathXmlApplicationContext(String configLocation) throws BeansException { this(new String[] {configLocation}, true, null); } public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }
super方法一直调用父类构造函数,直到AbstractApplicationContext抽象基类
public AbstractApplicationContext() { this.resourcePatternResolver = getResourcePatternResolver(); } public AbstractApplicationContext(ApplicationContext parent) { this(); setParent(parent); }
this方法得到默认的资源解析器(ResourcePatternResolver),setParent方法设置父ApplicationContext,默认parent传参为null。
setConfigLocations方法将构造方法传入的资源文件设置到AbstractRefreshableConfigApplicationContext方法的configLocations集合中。主要的操做refresh方法的实现是在AbstractApplicationContext类中。在refresh方法中,Spring抽象出每一个细分操做为单独的方法,而后按顺序进行调用。具体来看源码。
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. // 刷新前准备,主要是设置开始时间以及标识active标志位为true prepareRefresh(); // Tell the subclass to refresh the internal bean factory. // 建立BeanFactory实例,并加载配置文件 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. // BeanFactory准备工做,主要是设置类加载器,Spring表达式解析器以及框架相关的Aware接口默认配置 prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. // BeanFactory后置处理(BeanFactory初始化完成后的扩展),如web项目中配置ServletContext postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. // 实例化并执行全部注册的BeanFactoryPostProcessor invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. // 实例化并注册全部BeanPostProcessor registerBeanPostProcessors(beanFactory); // Initialize message source for this context. // 初始化消息源 initMessageSource(); // Initialize event multicaster for this context. // 初始化上下文事件机制 initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. // 为特殊的上下文预留的方法,初始化特殊的bean onRefresh(); // Check for listener beans and register them. // 注册监听器 registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. // 冻结全部配置并实例化全部非懒加载的单例bean finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. // 初始化生命周期,发布容器事件 finishRefresh(); } catch (BeansException ex) { logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex); // Destroy already created singletons to avoid dangling resources. // 销毁已经建立的单例bean destroyBeans(); // Reset 'active' flag. // 重置active标识 cancelRefresh(ex); // Propagate exception to caller. throw ex; } } }
其实经过每一个子方法的名称和注释基本就能清楚其内部处理的主要内容,下面分析一些比较重要的节点方法。
在ClassPathXmlApplicationContext构造方法中,定义的方法名为refresh,就是指刷新,也就是Spring容器不只仅只是建立,也是能够刷新的,而refresh方法中的obtainFreshBeanFactory方法顾名思义是得到一个新鲜的BeanFactory,它的实如今AbstractApplicationContext中。
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { refreshBeanFactory(); ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; }
能够看到,真正做用的是refreshBeanFactory,也就是真正对BeanFactory进行重置刷新的地方,而后refresh方法以后的操做基于一个新的BeanFactory进行组装重建,从而达到刷新整个Spring容器的目的。refreshBeanFactory方法的实现是在AbstractApplicationContext的子类AbstractRefreshableApplicationContext中。
protected final void refreshBeanFactory() throws BeansException { // 若是已存在BeanFactory,则销毁全部bean并关闭BeanFactory if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { // 实例化一个新的BeanFactory DefaultListableBeanFactory beanFactory = createBeanFactory(); // 设置序列化id为惟一id beanFactory.setSerializationId(getId()); // BeanFactory的自定义配置 customizeBeanFactory(beanFactory); // 加载资源配置文件 loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } } protected DefaultListableBeanFactory createBeanFactory() { return new DefaultListableBeanFactory(getInternalParentBeanFactory()); }
能够看到默认建立的BeanFactory就是DefaultListableBeanFactory对象,以前的章节讨论BeanFactory时也重点强调了这个类,至此发现它就是当前Spring容器内部BeanFactory的默认实现类。另外在此处对资源配置文件进行了加载,具体的加载方法同以前的章节大体相同,请见spring源码-IOC容器(二)-Bean的定位解析注册。
BeanFactory建立完成后,须要对BeanFactory进行一些配置,提供对框架级操做的基础。
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { // Tell the internal bean factory to use the context's class loader etc. // 类加载器 beanFactory.setBeanClassLoader(getClassLoader()); // Spring表达式解析器 beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); // 属性编辑注册器策略类 beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); // Configure the bean factory with context callbacks. // 设置框架级Aware接口实现由容器自动注入对应属性 beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); beanFactory.ignoreDependencyInterface(EnvironmentAware.class); // BeanFactory interface not registered as resolvable type in a plain factory. // MessageSource registered (and found for autowiring) as a bean. beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this); // Detect a LoadTimeWeaver and prepare for weaving, if found. if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); // Set a temporary ClassLoader for type matching. beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } // 注册环境相关bean // Register default environment beans. if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); } if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()); } if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); } }
BeanFactoryPostProcessor的定义是在BeanFactory初始化完成后对BeanFactory进行调整的扩展点。
public interface BeanFactoryPostProcessor { // 支持BeanFactory初始化完成后对其进行调整 void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; } 而BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子类,支持对BeanDefinition的调整。 public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor { void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException; }
来看refresh方法中执行BeanFactoryPostProcessor的具体子方法
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); }
实际的操做是经过一个PostProcessor注册委托类来处理,步骤以下:
对于内置的beanFactoryPostProcessors,判断BeanFactory实现是否实现BeanDefinitionRegistry接口
查询全部BeanFactory中注册的BeanDefinition有类型为BeanFactoryPostProcessor的beanName,再根据是否实现PriorityOrdered或Ordered接口进行排序,调用接口方法postProcessBeanFactory,先执行PriorityOrdered接口的,其次为Ordered,最后执行其余的。
public static void invokeBeanFactoryPostProcessors( ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { // Invoke BeanDefinitionRegistryPostProcessors first, if any. Set<String> processedBeans = new HashSet<String>(); // 判断beanFactory是否为BeanDefinitionRegistry的子类 if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>(); List<BeanDefinitionRegistryPostProcessor> registryPostProcessors = new LinkedList<BeanDefinitionRegistryPostProcessor>(); // 遍历内置beanFactoryPostProcessors,查询BeanFactoryPostProcessor的子接口BeanDefinitionRegistryPostProcessor for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; registryPostProcessor.postProcessBeanDefinitionRegistry(registry); registryPostProcessors.add(registryPostProcessor); } else { regularPostProcessors.add(postProcessor); } } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! // Separate between BeanDefinitionRegistryPostProcessors that implement // PriorityOrdered, Ordered, and the rest. String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>(); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } OrderComparator.sort(priorityOrderedPostProcessors); registryPostProcessors.addAll(priorityOrderedPostProcessors); invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry); // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>(); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } OrderComparator.sort(orderedPostProcessors); registryPostProcessors.addAll(orderedPostProcessors); invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry); // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear. boolean reiterate = true; while (reiterate) { reiterate = false; postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName)) { BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class); registryPostProcessors.add(pp); processedBeans.add(ppName); pp.postProcessBeanDefinitionRegistry(registry); reiterate = true; } } } // Now, invoke the postProcessBeanFactory callback of all processors handled so far. invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { // Invoke factory processors registered with the context instance. invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); // Separate between BeanFactoryPostProcessors that implement PriorityOrdered, // Ordered, and the rest. List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); List<String> orderedPostProcessorNames = new ArrayList<String>(); List<String> nonOrderedPostProcessorNames = new ArrayList<String>(); for (String ppName : postProcessorNames) { if (processedBeans.contains(ppName)) { // skip - already processed in first phase above } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. OrderComparator.sort(priorityOrderedPostProcessors); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // Next, invoke the BeanFactoryPostProcessors that implement Ordered. List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); for (String postProcessorName : orderedPostProcessorNames) { orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } OrderComparator.sort(orderedPostProcessors); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // Finally, invoke all other BeanFactoryPostProcessors. List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); for (String postProcessorName : nonOrderedPostProcessorNames) { nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
}
refresh方法中的registerBeanPostProcessors,用来注册BeanPostProcessor到BeanFactory中,具体实现也是经过PostProcessorRegistrationDelegate委托类来进行。
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) { PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this); }
处理的过程相似于上面的BeanFactoryPostProcessor,都是从BeanFactory中查询类型为BeanPostProcessor的beanName,再根据其是否实现PriorityOrdered,Ordered接口排序,而后统一调用BeanFactory的addBeanPostProcessor方法注册。
最后硬编码方式内置增长了一个监听器发现的BeanPostProcessor的实现ApplicationListenerDetector,用来在bean初始化以后,判断bean是否实现ApplicationListener接口,若是是,就将其注册到applicationListeners中。
若是bean配置的是非懒加载的单例(默认为懒加载),则在容器启动过程当中就经过getBean方法对其实例化,这个操做在refresh方法中对应finishBeanFactoryInitialization子方法。
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { // Initialize conversion service for this context. // 初始化类型转换服务bean if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); } // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } // Stop using the temporary ClassLoader for type matching. // 中止使用临时类加载器 beanFactory.setTempClassLoader(null); // Allow for caching all bean definition metadata, not expecting further changes. // 冻结bean definition元数据配置 beanFactory.freezeConfiguration(); // Instantiate all remaining (non-lazy-init) singletons. // 实例化non-lazy-init单例 beanFactory.preInstantiateSingletons(); }
在预实例化时,对FactoryBean也作了特殊的处理,只有SmartFactoryBean的子类而且isEagerInit方法为true时,才会执行FactoryBean的getObject方法建立真正的对象。而且建立的对象是SmartInitializingSingleton的子类时,执行接口方法afterSingletonsInstantiated。
public void preInstantiateSingletons() throws BeansException { if (this.logger.isDebugEnabled()) { this.logger.debug("Pre-instantiating singletons in " + this); } // Iterate over a copy to allow for init methods which in turn register new bean definitions. // While this may not be part of the regular factory bootstrap, it does otherwise work fine. List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames); // Trigger initialization of all non-lazy singleton beans... for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName); boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { [@Override](https://my.oschina.net/u/1162528) public Boolean run() { return ((SmartFactoryBean<?>) factory).isEagerInit(); } }, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } else { getBean(beanName); } } } // Trigger post-initialization callback for all applicable beans... for (String beanName : beanNames) { Object singletonInstance = getSingleton(beanName); if (singletonInstance instanceof SmartInitializingSingleton) { final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance; if (System.getSecurityManager() != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { [@Override](https://my.oschina.net/u/1162528) public Object run() { smartSingleton.afterSingletonsInstantiated(); return null; } }, getAccessControlContext()); } else { smartSingleton.afterSingletonsInstantiated(); } } } }
finishRefresh方法中对生命周期处理类进行初始化并刷新,而后发布了容器刷新完成事件。到此容器刷新的过程就结束了。
protected void finishRefresh() { // Initialize lifecycle processor for this context. initLifecycleProcessor(); // Propagate refresh to lifecycle processor first. getLifecycleProcessor().onRefresh(); // Publish the final event. publishEvent(new ContextRefreshedEvent(this)); // Participate in LiveBeansView MBean, if active. LiveBeansView.registerApplicationContext(this); }
##异常##
固然若是过程当中抛出了BeansException异常,则须要对Spring容器进行清理。
// Destroy already created singletons to avoid dangling resources. // 销毁全部实例化的单例bean destroyBeans(); // Reset 'active' flag. // 重置active为false cancelRefresh(ex);