spring的父子上下文容器及配置

本文由作者张远道授权网易云社区发布。

spring父子容器

spring总的上下文容器有父子之分。父容器和子容器。父容器对子容器可见,子容器对父容器不可见。

对于传统的spring mvc来说,spring mvc容器为子容器,也就是说ServletDispatcher对应的容器为子容器,而web.xml中通过ConextLoaderListener的contextConfigLocation属性配置的为父容器。

使用场景

父子容器的主要用途之一便是是上下文隔离。考虑以下一种场景。

project-service.jar为服务层模块。包含一些数据库service方法。起对应的spring配置文件为project-service.xml。 project-api为api服务器代码。它依赖于project-service.jar。 但是,project-api需要对project-service里的某些方法进行decorate,进行装饰,比如给CustomerService进行装饰。装饰后的类为CachedCustomerService。于是,现在project-api里面包含两个CustomerService,一个是来自project-service的CustomerService,另一个是CachedCustomerService。这个时候,如果project-api工程所有的配置文件都通过一个上下文进行加载,势必出现问题。因为,project里的PayService里通过@Resource标准注入了CustomerService,类似如下

@Serivcepublic class PayService{@Resourceprivate CustomerService cusService;

}

这时,由于上下文在注入customerService属性的时候,遇到了两个CustomService。它无法判读注入哪个Service。 当然了,有人会说,改一下PayService的Resource属性,指定下具体注入哪个。但是,project-service.jar是第三方库的话,改动代码变得不可行,除非拿到源码。

配置父子容器

这个时候,就可以通过父子容器的方式解决这个问题。 将project-service放在父容器中,project-api所有的bean用子容器加载。

假设project-api的上下文配置文件为project-api.xml,实现方法如下。

1、定义project-total.xml

    <bean id = "serviceContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
        <value>
            classpath:project-service.xml        </value>
        </constructor-arg>
    </bean>


    <bean id = "apiContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
            <value>
                classpath:project-api.xml            </value>
        </constructor-arg>

        <constructor-arg>
            <ref bean="serviceContext"/>
        </constructor-arg>
    </bean>

2、在web.xml的上下文配置中如下。

    <context-param> 
        <param-name>contextConfigLocation</param-name>
        <param-value> classpath*:project-total.xml</param-value>  
    </context-param>


    <listener>   
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>   
      </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

说明,其中,serviceContext为父容器,apiContext为子容器。在apiContext扫描路径里的任务bean都对serviceContext不可见。从而达到隔离的目的

参考列表


更多网易技术、产品、运营经验分享请访问网易云社区

相关文章:
【推荐】 致传统企业朋友:不够痛就别微服务,有坑 (2)
【推荐】 后端接口迁移(从 webapi 到 openapi)前端经验总结

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,925评论 6 342
  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 6,748评论 2 22
  • spring父子容器 spring总的上下文容器有父子之分,父容器和子容器。** 父容器对子容器可见,子容器对父容...
    张一一阅读 6,728评论 0 1
  • “解放路四号家园”评论说,在南京军事学院即现在的南京军区司令部大院内也有雪松栽种。确实是这样的,栽种的位置主...
    jinlinglq阅读 572评论 0 0