美文网首页
Spring父子容器

Spring父子容器

作者: AC编程 | 来源:发表于2022-11-19 09:47 被阅读0次

    一、Spring和Spring MVC父子容器概念介绍

    在Spring和Spring MVC进行整合的时候,一般情况下我们会使用不同的配置文件来配置Spring和Spring MVC,因此我们的应用中会存在至少2个ApplicationContext实例,由于是在web应用中,因此最终实例化的是 ApplicationContext的子接口WebApplicationContext。如下图所示:

    WebApplicationContext Spring MVC官网

    上图中显示了2个WebApplicationContext实例,为了进行区分,分别称之为:Servlet WebApplicationContext、Root WebApplicationContext。 其中:

    • Servlet WebApplicationContext:这是对J2EE三层架构中的web层进行配置,如控制器(controller)、视图解析器(view resolvers)等相关的bean。通过Spring MVC中提供的DispatchServlet来加载配置,通常情况下,配置文件的名称为spring-servlet.xml。

    • Root WebApplicationContext:这是对J2EE三层架构中的service层、dao层进行配置,如业务bean,数据源(DataSource)等。通常情况下,配置文件的名称为applicationContext.xml。在web应用中,其一般通过ContextLoaderListener来加载。

    以下是一个web.xml配置案例:

    <?xml version="1.0" encoding="UTF-8"?>  
      
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
        
        <!—创建Root WebApplicationContext-->
        <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/spring/applicationContext.xml</param-value>  
        </context-param>  
      
        <listener>  
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
        </listener>  
        
        <!—创建Servlet WebApplicationContext-->
        <servlet>  
            <servlet-name>dispatcher</servlet-name>  
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
            <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>/WEB-INF/spring/spring-servlet.xml</param-value>  
            </init-param>  
            <load-on-startup>1</load-on-startup>  
        </servlet>  
        <servlet-mapping>  
            <servlet-name>dispatcher</servlet-name>  
            <url-pattern>/*</url-pattern>  
        </servlet-mapping>  
      
    </web-app>
    
    Spring MVC官网

    在上面的配置中:

    • ContextLoaderListener会被优先初始化时,其会根据<context-param>元素中contextConfigLocation参数指定的配置文件路径,在这里就是"/WEB-INF/spring/applicationContext.xml”,来创建WebApplicationContext实例。 并调用ServletContext的setAttribute方法,将其设置到ServletContext中,属性的key为”org.springframework.web.context.WebApplicationContext.ROOT”,最后的”ROOT"字样表明这是一个 Root WebApplicationContext。

    • DispatcherServlet在初始化时,会根据<init-param>元素中contextConfigLocation参数指定的配置文件路径,即"/WEB-INF/spring/spring-servlet.xml”,来创建Servlet WebApplicationContext。同时,其会调用ServletContext的getAttribute方法来判断是否存在Root WebApplicationContext。如果存在,则将其设置为自己的parent。这就是父子上下文(父子容器)的概念。

    父子容器的作用在于,当我们尝试从child context(即:Servlet WebApplicationContext)中获取一个bean时,如果找不到,则会委派给parent context (即Root WebApplicationContext)来查找。

    如果我们没有通过ContextLoaderListener来创建Root WebApplicationContext,那么Servlet WebApplicationContext的parent就是null,也就是没有parent context。

    二、父子容器特点

    • 父容器和子容器是相互隔离的,他们内部可以存在名称相同的bean。

    • 子容器可以访问父容器中的bean,而父容器不能访问子容器中的bean。

    • 调用子容器的getBean方法获取bean的时候,会沿着当前容器开始向上面的容器进行查找,直到找到对应的bean为止。

    • 子容器中可以通过任何注入方式注入父容器中的bean,而父容器中是无法注入子容器中的bean,原因是第2点。

    三、为什么要有父子容器

    父子容器的作用主要是划分框架边界。

    在J2EE三层架构中,在service层我们一般使用Spring框架, 而在web层则有多种选择,如Spring MVC、Struts等。因此,通常对于web层我们会使用单独的配置文件。例如在上面的案例中,一开始我们使用spring-servlet.xml来配置web层,使用applicationContext.xml来配置service、dao层。如果现在我们想把web层从Spring MVC替换成Struts,那么只需要将spring-servlet.xml替换成Struts的配置文件struts.xml即可,而applicationContext.xml不需要改变。

    事实上,如果你的项目确定了只使用Spring和Spring MVC的话,你甚至可以将service 、dao、web层的bean都放到spring-servlet.xml中进行配置,并不是一定要将service、dao层的配置单独放到applicationContext.xml中,然后使用ContextLoaderListener来加载。在这种情况下,就没有了Root WebApplicationContext,只有Servlet WebApplicationContext。

    四、父子容器相关接口

    在IOC容器时,Spring中通常会提到两个顶级接口BeanFactory和ApplicationContext,这两个都是IOC容器接口,相比BeanFactory而言,ApplicationContext提供了更强大的功能。

    4.1 HierarchicalBeanFactory

    该接口作为BeanFactory的子接口,它的定义如下:

    public interface HierarchicalBeanFactory extends BeanFactory {
        BeanFactory getParentBeanFactory();
    
        boolean containsLocalBean(String name);
    }
    

    从它名称可以看出,它是一个有层级的BeanFactory,它提供的两个方法其中一个就是用来获取父容器的。

    4.2 ConfigurableBeanFactory

    上面说了HierarchicalBeanFactory提供了获取父容器的方法,那么父容器是怎么设置的呢?而设置父容器的方法则被定义在ConfigurableBeanFactory接口中。从名字可以看出它是一个可配置的BeanFactory,设置父容器的方法定义如下:

    void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException;
    
    4.3 ApplicationContext

    上面讲了BeanFactory中获取和设置父容器相关接口和方法,而ApplicationContext中同样提供了一个方法用来获取父容器。

    ApplicationContext getParent();
    
    4.4 ConfigurableApplicationContext

    与BeanFactory中设置父容器一样,ConfigurableApplicationContext提供了一个用来设置父容器的方法。

    void setParent(@Nullable ApplicationContext parent);
    

    五、父子容器使用注意点

    我们使用容器的过程中,经常会使用到的一些方法,这些方法通常会在下面的两个接口中

    org.springframework.beans.factory.BeanFactory
    org.springframework.beans.factory.ListableBeanFactory
    

    这两个接口中有很多方法,这里就不列出来了,大家可以去看一下源码,这里要说的是使用父子容器的时候,有些需要注意的地方。

    BeanFactory接口,是Spring容器的顶层接口,这个接口中的方法是支持容器嵌套结构查找的,比如我们常用的getBean方法,就是这个接口中定义的,调用getBean方法的时候,会从沿着当前容器向上查找,直到找到满足条件的bean为止。

    而ListableBeanFactory这个接口中的方法是不支持容器嵌套结构查找的,比如下面这个方法

    String[] getBeanNamesForType(@Nullable Class<?> type)
    

    获取指定类型的所有bean名称,调用这个方法的时候只会返回当前容器中符合条件的bean,而不会去递归查找其父容器中的bean。

    5.1 案例

    来看一下案例代码,感受一下:

    @Test
    public void test3() {
        //创建父容器parentFactory
        DefaultListableBeanFactory parentFactory = new DefaultListableBeanFactory();
        //向父容器parentFactory注册一个bean[userName->"路人甲Java"]
        parentFactory.registerBeanDefinition("userName",
                BeanDefinitionBuilder.
                        genericBeanDefinition(String.class).
                        addConstructorArgValue("路人甲Java").
                        getBeanDefinition());
     
        //创建一个子容器childFactory
        DefaultListableBeanFactory childFactory = new DefaultListableBeanFactory();
        //调用setParentBeanFactory指定父容器
        childFactory.setParentBeanFactory(parentFactory);
        //向子容器parentFactory注册一个bean[address->"上海"]
        childFactory.registerBeanDefinition("address",
                BeanDefinitionBuilder.
                        genericBeanDefinition(String.class).
                        addConstructorArgValue("上海").
                        getBeanDefinition());
     
        System.out.println("获取bean【userName】:" + childFactory.getBean("userName"));//@1
     
        System.out.println(Arrays.asList(childFactory.getBeanNamesForType(String.class))); //@2
    }
    

    上面定义了2个容器

    父容器:parentFactory,内部定义了一个String类型的bean:userName->路人甲Java

    子容器:childFactory,内部也定义了一个String类型的bean:address->上海

    • @1:调用子容器的getBean方法,获取名称为userName的bean,userName这个bean是在父容器中定义的,而getBean方法是BeanFactory接口中定义的,支持容器层次查找,所以getBean是可以找到userName这个bean的

    • @2:调用子容器的getBeanNamesForType方法,获取所有String类型的bean名称,而getBeanNamesForType方法是ListableBeanFactory接口中定义的,这个接口中方法不支持层次查找,只会在当前容器中查找,所以这个方法只会返回子容器的address

    我们来运行一下看看效果:

    获取bean【userName】:路人甲Java
    [address]
    
    5.2 解决ListableBeanFactory接口不支持层次查找的问题

    Spring中有个工具类就是解决这个问题的,如下:

    org.springframework.beans.factory.BeanFactoryUtils
    

    这个类中提供了很多静态方法,有很多支持层次查找的方法,源码你们可以去细看一下,名称中包含有Ancestors的都是支持层次查找的。

    在test2方法中加入下面的代码:

    //层次查找所有符合类型的bean名称
    String[] beanNamesForTypeIncludingAncestors = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(childFactory, String.class);
    System.out.println(Arrays.asList(beanNamesForTypeIncludingAncestors));
     
    Map<String, String> beansOfTypeIncludingAncestors = BeanFactoryUtils.beansOfTypeIncludingAncestors(childFactory, String.class);
    System.out.println(Arrays.asList(beansOfTypeIncludingAncestors));
    

    运行输出

    [address, userName]
    [{address=上海, userName=路人甲Java}]
    

    查找过程是按照层次查找所有满足条件的bean。

    六、总结

    6.1 Spring MVC中只使用一个容器是否可以

    Spring MVC中只使用一个容器是可以正常运行的。SpringBoot默认就只有一个容器。

    6.2 Spring MVC中为什么需要用到父子容器

    通常我们使用Spring MVC的时候,采用3层结构,controller层,service层,dao层;父容器中会包含dao层和service层,而子容器中包含的只有controller层;这2个容器组成了父子容器的关系,controller层通常会注入service层的bean。

    采用父子容器可以避免有些人在service层去注入controller层的bean,导致整个依赖层次是比较混乱的。

    父容器和子容器的需求也是不一样的,比如父容器中需要有事务的支持,会注入一些支持事务的扩展组件,而子容器中controller完全用不到这些,对这些并不关心,子容器中需要注入一下Spring MVC相关的bean,而这些bean父容器中同样是不会用到的,也是不关心一些东西,将这些相互不关心的东西隔开,可以有效的避免一些不必要的错误,而父子容器加载的速度也会快一些。

    相关文章

      网友评论

          本文标题:Spring父子容器

          本文链接:https://www.haomeiwen.com/subject/edmmxdtx.html