(二)深入理解 Spring 的核心技术 IOC

(二)深入理解 Spring 的核心技术 IOC


再谈 IOC 与 DI

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

这个描述的最具体表现就是我们的配置文件。

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


相关疑问

1、对象和对象的关系怎么表示?

:可以用xml,properties 等文件语义化配置文件表示。

2、描述对象关系的文件存放在那里?

:可能是 classpath, filesystem, 或者是URL网络资源,servletContext,resources等

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

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

4、如何对不同的配置文件进行解析?

:需要对不同的配置文件语法,采用不同的解释器。


Spring 的核心容器类

1、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 接口的实现,我们看出其特点:

1、支持信息源,可以实现国际化。(实现 MessageSource 接口)

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

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


2、BeanDefinition

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

在这里插入图片描述


3、BeanDefinitionReader

​ Bean 的解析过程非常复杂,功能被分的很细,因为这里需要被扩展的地方很多,必须保证有足够的灵活性,以应对可能的变化。

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

在这里插入图片描述

​ 通过本章内容的分析,我们对 Spring 框架体系有了一个基本的宏观了解,希望小伙伴们好好理解,最好在脑海中形成画面,为以后的学习打下良好的铺垫。


Web IOC 容器初体验

​ 我们还是从大家最熟悉的 DispatcherServlet 开始,我们最先想到的还是 DispatcherServlet 的 init() 方法。我们发现在 DispatherServlet 中并没有找到 init() 方法。

但是经过探索,往上追索在其父类HttpServletBean 中找到了我们想要的 init() 方法,如下:

@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> + 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"); 
    } }

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

protected WebApplicationContext initWebApplicationContext() {
		WebApplicationContext rootContext =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		WebApplicationContext wac = null;

		if (this.webApplicationContext != null) {
			// A context instance was injected at construction time -> use it
			wac = this.webApplicationContext;
			if (wac instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
				if (!cwac.isActive()) {
					// The context has not yet been refreshed -> 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 -> set
						// the root application context (if any; may be null) as the parent
						cwac.setParent(rootContext);
					}
					configureAndRefreshWebApplicationContext(cwac);
				}
			}
		}
		if (wac == null) {
			// No context instance was injected at construction time -> 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();
		}
		if (wac == null) {
			// No context instance is defined for this servlet -> create a local one
			wac = createWebApplicationContext(rootContext);
		}

		if (!this.refreshEventReceived) {
			// Either the context is not a ConfigurableApplicationContext with refresh
			// support or the context injected at construction time had already been
			// refreshed -> 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;
	}
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
		if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
			// The application context id is still set to its original default value
			// -> 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 的九大组件:

// 真正初始化的位置
protected void initStrategies(ApplicationContext context) {
   // 初始化文件上传组件
   initMultipartResolver(context);
   // 初始化本地语言环境
   initLocaleResolver(context);
   // 初始化模板处理器
   initThemeResolver(context);
   // 初始化映射处理器
   initHandlerMappings(context);
   // 初始化参数适配器
   initHandlerAdapters(context);
   // 初始化异常解析器
   initHandlerExceptionResolvers(context);
   // 初始化视图预处理器
   initRequestToViewNameTranslator(context);
   // 初始化视图转换器
   initViewResolvers(context);
   // 初始化映射管理器
   initFlashMapManager(context);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值