《Spring源码解析(三)》从源码深处体验Spring核心技术--IOC容器初体验

开局经验之谈:可能从这一篇文章开始,小伙伴们都会有点晕车的感受了,可是这个系列并非只是介绍下 spring 表面的一些肤浅的东西,本系列的目的是为了让你们从源码层次深刻理解 Spring,这也是你们在将来的求职道路上的一个重要的涨薪手段,但愿小伙伴都不要放弃,结合源码多看几遍,努力必定会有收获。java

本文即开始带领你们研读Spring5源码,若是尚未构建Spirng5源码工程的话,能够先去《Spring源码解析(二)》构建 Spring5 源码工程,开启研读Spring源码之路 学习如何构建Spring5源码工程。web

认识 IOC 与 DI

「IOC(Inversion of Control)控制反转」:所谓控制反转,就是把原先咱们代码里面须要实现的对象建立、依赖的代码,反转给容器来帮忙实现。那么必然的咱们须要建立一个容器,同时须要一种描述来让容器知道须要建立的对象与对象的关系。面试

这个描述最具体表现就是咱们所看到的配置文件。spring

「DI(Dependency Injection)依赖注入」:就是指对象是被动接受依赖类而不是本身主动去找,换句话说就是指对象不是从容器中查找它依赖的类,而是在容器实例化对象的时候主动将它依赖的类注入给它。安全

如何设计IOC和DI,咱们先来思考下面四个问题?网络

一、对象和对象的关系怎么表示?架构

能够用 xml,properties 文件等语义化配置文件表示。app

二、描述对象关系的文件存放在哪里?框架

多是 classpath,filesystem,或者是 URL 网络资源,servletContext 等。ide

回到正题,有了配置文件,还须要对配置文件解析。

三、不一样的配置文件对对象的描述不同,如标准的,自定义声明式的,如何统一?

在内部须要有一个统一的关于对象的定义,全部外部的描述都必须转化成统一的描述定义。

四、如何对不一样的配置文件进行解析?

须要对不一样的配置文件语法,采用不一样的解析器。

Spring 核心容器类图

一、BeanFactory

Spring Bean 的建立是典型的工厂模式,这一系列的 Bean 工厂,也即 IOC 容器为开发者管理对象间的依赖关系提供了不少便利和基础服务,在 Spring 中有许多的 IOC 容器的实现供用户选择和使用,其相互关系以下:

其中 BeanFactory 做为最顶层的一个接口类,它定义了 IOC 容器的基本功能规范 BeanFactory 有三 个重要的子类:ListableBeanFactory、HierarchicalBeanFactory 和 AutowireCapableBeanFactory。

可是从类图中咱们能够发现最终的默认实现类是 DefaultListableBeanFactory,它实现了全部的接口。

那为什么要定义这么多层次的接口呢? 查阅这些接口的源码和说明发现,每一个接口都有它使用的场合,它主要是为了区分在 Spring 内部在操做过程当中对象的传递和转化过程时,对对象的数据访问所作的限制。

例如 ListableBeanFactory 接口表示这些 Bean 是可列表化的,而 HierarchicalBeanFactory 表示的是这些 Bean 是有继承关系的,也就是每一个 Bean 有可能有父 Bean。

AutowireCapableBeanFactory 接口定义 Bean 的自动装配规则。这三个接口共同定义了 Bean 的集合、Bean 之间的关系、以及 Bean 行为。

最基本的 IOC 容器接口 BeanFactory,来看一下它的源码:

public interface BeanFactory {

	//对FactoryBean的转义定义,由于若是使用bean的名字检索FactoryBean获得的对象是工厂生成的对象,
	//若是须要获得工厂自己,须要转义
	String FACTORY_BEAN_PREFIX = "&";

	//根据bean的名字,获取在IOC容器中获得bean实例
	Object getBean(String name) throws BeansException;

	//根据bean的名字和Class类型来获得bean实例,增长了类型安全验证机制。
	<T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException;

	Object getBean(String name, Object... args) throws BeansException;

	<T> T getBean(Class<T> requiredType) throws BeansException;
	
	<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

	//提供对bean的检索,看看是否在IOC容器有这个名字的bean
	boolean containsBean(String name);

	//根据bean名字获得bean实例,并同时判断这个bean是否是单例
	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

	boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

	boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;

	boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException;

	//获得bean实例的Class类型
	@Nullable
	Class<?> getType(String name) throws NoSuchBeanDefinitionException;

	//获得bean的别名,若是根据别名检索,那么其原名也会被检索出来
	String[] getAliases(String name);

}

在 BeanFactory 里只对 IOC 容器的基本行为做了定义,根本不关心你的 Bean 是如何定义怎样加载的。

正如咱们只关心工厂里获得什么的产品对象,至于工厂是怎么生产这些对象的,这个基本的接口不关心。

而要知道工厂是如何产生对象的,咱们须要看具体的 IOC 容器实现,Spring 提供了许多 IOC 容器的 实现 。

比 如 GenericApplicationContext , ClasspathXmlApplicationContext 等 。

ApplicationContext 是 Spring 提供的一个高级的 IOC 容器,它除了可以提供 IOC 容器的基本功能外,还为用户提供了如下的附加服务。

从 ApplicationContext 接口的实现,咱们看出其特色:

一、支持信息源,能够实现国际化。(实现 MessageSource 接口)

二、访问资源。(实现 ResourcePatternResolver 接口,后面章节会讲到)

三、支持应用事件。(实现 ApplicationEventPublisher 接口)

二、BeanDefinition

SpringIOC 容器管理了咱们定义的各类 Bean 对象及其相互的关系,Bean 对象在 Spring 实现中是以 BeanDefinition 来描述的,其继承体系以下:

三、BeanDefinitionReader

Bean 的解析过程很是复杂,功能被分的很细,由于这里须要被扩展的地方不少,必须保证有足够的灵活性,以应对可能的变化。

Bean 的解析主要就是对 Spring 配置文件的解析。这个解析过程主要经过 BeanDefintionReader 来完成,最后看看 Spring 中 BeanDefintionReader 的类结构图:

经过本章内容的分析,咱们对 Spring 框架体系有了一个基本的宏观了解,但愿小伙伴们好好理解,最好在脑海中造成画面,为之后的学习打下良好的铺垫。

Web IOC 容器初体验

咱们仍是从你们最熟悉的 DispatcherServlet 开始,咱们最早想到的仍是 DispatcherServlet 的 init()方法。咱们发如今 DispatherServlet 中并无找到 init()方法。

可是通过探索,往上追索在其父类 HttpServletBean 中找到了咱们想要的 init()方法,以下:

/**
	 * Map config parameters onto bean properties of this servlet, and
	 * invoke subclass initialization.
	 * @throws ServletException if bean properties are invalid (or required
	 * properties are missing), or if subclass initialization fails.
	 */
	@Override
	public final void init() throws ServletException {
		if (logger.isDebugEnabled()) {
			logger.debug("Initializing servlet '" + getServletName() + "'");
		}

		// Set bean properties from init parameters.
		PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
		if (!pvs.isEmpty()) {
			try {
                /**定位资源**/
				BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
                /**加载配置信息**/
				ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
				bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
				initBeanWrapper(bw);
				bw.setPropertyValues(pvs, true);
			}
			catch (BeansException ex) {
				if (logger.isErrorEnabled()) {
					logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
				}
				throw ex;
			}
		}

		// Let subclasses do whatever initialization they like.
		initServletBean();

		if (logger.isDebugEnabled()) {
			logger.debug("Servlet '" + getServletName() + "' configured successfully");
		}
	}

在 init()方法中,真正完成初始化容器动做的逻辑其实在 initServletBean()方法中,咱们继续跟进 initServletBean()中的代码在 FrameworkServlet 类中:

protected final void initServletBean() throws ServletException {
    getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
    if (this.logger.isInfoEnabled()) {
      this.logger.info("FrameworkServlet '" + getServletName() + "': initialization      started");
    }
    long startTime = System.currentTimeMillis();
    try {
      this.webApplicationContext = initWebApplicationContext();
      initFrameworkServlet();
    }catch (ServletException ex) {
      this.logger.error("Context initialization failed", ex);
      throw ex;
    }catch (RuntimeException ex) {
      this.logger.error("Context initialization failed", ex);
      throw ex;
    }
    if (this.logger.isInfoEnabled()) {
      long elapsedTime = System.currentTimeMillis() - startTime;
      this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " + elapsedTime + " ms");
    }
}

在上面的代码中终于看到了咱们似曾相识的代码 initWebAppplicationContext(),继续跟进:

/**
	 * Initialize and publish the WebApplicationContext for this servlet.
	 * <p>Delegates to {@link #createWebApplicationContext} for actual creation
	 * of the context. Can be overridden in subclasses.
	 * @return the WebApplicationContext instance
	 * @see #FrameworkServlet(WebApplicationContext)
	 * @see #setContextClass
	 * @see #setContextConfigLocation
	 */
	protected WebApplicationContext initWebApplicationContext() {
        /**先从ServletContext中得到父容器WebApplicationContext**/
		WebApplicationContext rootContext =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        
        /**声明子容器**/
		WebApplicationContext wac = null;

        /**创建父,子容器之间的关联关系**/

		if (this.webApplicationContext != null) {
			// A context instance was injected at construction time -&gt; use it
			wac = this.webApplicationContext;
			if (wac instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
				if (!cwac.isActive()) {
					// The context has not yet been refreshed -&gt; provide services such as
					// setting the parent context, setting the application context id, etc
					if (cwac.getParent() == null) {
						// The context instance was injected without an explicit parent -&gt; set
						// the root application context (if any; may be null) as the parent
						cwac.setParent(rootContext);
					}
					configureAndRefreshWebApplicationContext(cwac);
				}
			}
		}

        /**先去ServletContext中查找Web容器的引用是否存在,并建立默认的空IOC容器**/
		if (wac == null) {
			// No context instance was injected at construction time -&gt; see if one
			// has been registered in the servlet context. If one exists, it is assumed
			// that the parent context (if any) has already been set and that the
			// user has performed any initialization such as setting the context id
			wac = findWebApplicationContext();
		}

        /**给上一步建立好的IOC容器赋值**/
		if (wac == null) {
			// No context instance is defined for this servlet -&gt; create a local one
			wac = createWebApplicationContext(rootContext);
		}

        /**触发onRefresh方法**/
		if (!this.refreshEventReceived) {
			// Either the context is not a ConfigurableApplicationContext with refresh
			// support or the context injected at construction time had already been
			// refreshed -&gt; trigger initial onRefresh manually here.
			onRefresh(wac);
		}

		if (this.publishContext) {
			// Publish the context as a servlet context attribute.
			String attrName = getServletContextAttributeName();
			getServletContext().setAttribute(attrName, wac);
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
						"' as ServletContext attribute with name [" + attrName + "]");
			}
		}

		return wac;
	}

	/**
	 * Retrieve a {@code WebApplicationContext} from the {@code ServletContext}
	 * attribute with the {@link #setContextAttribute configured name}. The
	 * {@code WebApplicationContext} must have already been loaded and stored in the
	 * {@code ServletContext} before this servlet gets initialized (or invoked).
	 * </p><p>Subclasses may override this method to provide a different
	 * {@code WebApplicationContext} retrieval strategy.
	 * @return the WebApplicationContext for this servlet, or {@code null} if not found
	 * @see #getContextAttribute()
	 */
	@Nullable
	protected WebApplicationContext findWebApplicationContext() {
		String attrName = getContextAttribute();
		if (attrName == null) {
			return null;
		}
		WebApplicationContext wac =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);
		if (wac == null) {
			throw new IllegalStateException("No WebApplicationContext found: initializer not registered?");
		}
		return wac;
	}

	/**
	 * Instantiate the WebApplicationContext for this servlet, either a default
	 * {@link org.springframework.web.context.support.XmlWebApplicationContext}
	 * or a {@link #setContextClass custom context class}, if set.
	 * </p><p>This implementation expects custom contexts to implement the
	 * {@link org.springframework.web.context.ConfigurableWebApplicationContext}
	 * interface. Can be overridden in subclasses.
	 * </p><p>Do not forget to register this servlet instance as application listener on the
	 * created context (for triggering its {@link #onRefresh callback}, and to call
	 * {@link org.springframework.context.ConfigurableApplicationContext#refresh()}
	 * before returning the context instance.
	 * @param parent the parent ApplicationContext to use, or {@code null} if none
	 * @return the WebApplicationContext for this servlet
	 * @see org.springframework.web.context.support.XmlWebApplicationContext
	 */
	protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {
		Class<!--?--> contextClass = getContextClass();
		if (this.logger.isDebugEnabled()) {
			this.logger.debug("Servlet with name '" + getServletName() +
					"' will try to create custom WebApplicationContext context of class '" +
					contextClass.getName() + "'" + ", using parent context [" + parent + "]");
		}
		if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
			throw new ApplicationContextException(
					"Fatal initialization error in servlet with name '" + getServletName() +
					"': custom WebApplicationContext class [" + contextClass.getName() +
					"] is not of type ConfigurableWebApplicationContext");
		}
		ConfigurableWebApplicationContext wac =
				(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

		wac.setEnvironment(getEnvironment());
		wac.setParent(parent);
		String configLocation = getContextConfigLocation();
		if (configLocation != null) {
			wac.setConfigLocation(configLocation);
		}
		configureAndRefreshWebApplicationContext(wac);

		return wac;
	}

	protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
		if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
			// The application context id is still set to its original default value
			// -&gt; assign a more useful id based on available information
			if (this.contextId != null) {
				wac.setId(this.contextId);
			}
			else {
				// Generate default id...
				wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
						ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName());
			}
		}

		wac.setServletContext(getServletContext());
		wac.setServletConfig(getServletConfig());
		wac.setNamespace(getNamespace());
		wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

		// The wac environment's #initPropertySources will be called in any case when the context
		// is refreshed; do it eagerly here to ensure servlet property sources are in place for
		// use in any post-processing or initialization that occurs below prior to #refresh
		ConfigurableEnvironment env = wac.getEnvironment();
		if (env instanceof ConfigurableWebEnvironment) {
			((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
		}

		postProcessWebApplicationContext(wac);
		applyInitializers(wac);
		wac.refresh();
	}

从上面的代码中能够看出,在 configAndRefreshWebApplicationContext()方法中,调用 refresh()方法,这个是真正启动 IOC 容器的入口,后面会详细介绍。

IOC 容器初始化之后,最后调用了 DispatcherServlet 的 onRefresh()方法,在 onRefresh()方法中又是直接调用 initStrategies()方法初始化 SpringMVC 的九大组件:

/**
	 * This implementation calls {@link #initStrategies}.
	 */
	@Override
	protected void onRefresh(ApplicationContext context) {
		initStrategies(context);
	}

	/**
	 * Initialize the strategy objects that this servlet uses.
	 * </p><p>May be overridden in subclasses in order to initialize further strategy objects.
	 */
    /**初始化策略**/
	protected void initStrategies(ApplicationContext context) {
        /**多文件上传的组件**/
		initMultipartResolver(context);
        /**初始化本地语言环境**/
		initLocaleResolver(context);
        /**初始化模板处理器**/
		initThemeResolver(context);
        /**handlerMapping**/
		initHandlerMappings(context);
        /**初始化参数适配器**/
		initHandlerAdapters(context);
        /**初始化异常拦截器**/
		initHandlerExceptionResolvers(context);
        /**初始化视图预处理器**/
		initRequestToViewNameTranslator(context);
        /**初始化视图转化器**/
		initViewResolvers(context);
		initFlashMapManager(context);
	}

本文带领你们把IOC的源码流程大体走了一遍,后续会有详细介绍,IOC是Spring源码的重中之重,咱们将分多个篇幅来进行讲解。

Spring Cloud 微服务精彩系列

  1. 阿里面试官问我:到底知不知道什么是Eureka,此次,我没沉默
  2. 万字详解Ribbon架构,针对面试高频题多角度细说Ribbon
  3. 什么是Hystrix,阿里技术最终面,遗憾的倒在Hystrix面前!
  4. 2万字好文全方位深刻学习SpringCloud Fegin,面试不在彷徨
  5. Zuul,据说SpringCloud不许备要我了,但是为何面试还要每天问我?
  6. 全网最全讲解 Spring Cloud Gateway,认真看完这一篇就够了!

相关文章
相关标签/搜索