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不可见。从而达到隔离的目的
参考列表
更多网易技术、产品、运营经验分享请访问网易云社区。
相关文章:
【推荐】 #3.14 Piday#我的圆周率日
【推荐】 玩转可视化--来聊聊地图投影的学问
spring的父子上下文容器及配置的更多相关文章
- 【串线篇】spring boot嵌入式Servlet容器自动配置原理
EmbeddedServletContainerAutoConfiguration:嵌入式的Servlet容器自动配置? @AutoConfigureOrder(Ordered.HIGHEST_PREC ...
- Spring父子上下文(WebApplicationContext)(防止事务失效)
如果你使用了listener监听器来加载配置,一般在Struts+Spring+Hibernate的项目中都是使用listener监听器的.如下 <listener> <listen ...
- Spring的父子容器问题
在ssm框架搭建的时候 配置了一个Spring容器,又配置了一个前端控制器 <!-- 初始化spring容器 --> <context-param> <param-nam ...
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置
经过<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(1) - 数据源与事务管理>和<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(2) - ...
- Spring和SpringMVC父子的容器之道---[上篇]
Spring和SpringMVC作为Bean管理容器和MVC层的默认框架,已被众多WEB应用采用,而在实际开发中,由于有了强大的注解功能,很多基于XML的配置方式已经被替代,但在实际项目中,我们经常会 ...
- spring的父子容器
在创建ssm项目工程时,经常需要读取properties资源配置文件,传统的方法当然可以. 但是spring提供了更简便的方法,@value注解. 在page.properties文件中,配置分页信息 ...
- Spring父子上下文的使用案例
Spring父子上下文的使用案例 一.背景 二.需求 三.实现步骤 1.基础代码编写 2.测试结果 四.小彩蛋 五.完整代码 一.背景 最近在看在使用Spring Cloud的时候发现,当我们通过Fe ...
- 配置Spring的用于初始化容器对象的监听器
<!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web ...
- [转]Spring IOC父子容器简介
通过HierarchicalBeanFactory接口,Spring的IoC容器可以建立父子层级关联的容器体系,子容器可以访问父容器中的Bean,但父容器不能访问子容器的Bean.在容器内,Bean的 ...
随机推荐
- Ubuntu 安装VNC
ubuntu:.安装x11vnc sudo apt-get install x11vnc .设置密码 x11vnc -storepasswd .启动x11vnc(每次windows远程控制,都需要启动 ...
- leetcode119
public class Solution { public IList<int> GetRow(int rowIndex) { List<List<int>> l ...
- leetcode110
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- 利用TortoiseGit从Github上下载代码
1.首先确保安装好了Git和TortoiseGit并在Github上有存放资源 2.将git上博客源文件克隆到本地,在本地创建好要存放资源的文件夹,之后在此文件内右键单击,可以看到下拉菜单中增加了To ...
- centos7 二进制安装MySQL5.7.22
1.详细描安装的过程 1.1关闭防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service ...
- Spring cloud 分布式锁
https://github.com/easonstudy/springboot_demo study目录中
- tomcat 域名直接访问默认工程,而不添加项目路径
<Engine name="Catalina" defaultHost="xx.xx.xx.xx"> <!--For clustering, ...
- Linux学习---新建文件,查看文件,修改权限,删除
过程:在一个文件夹下面新建一个文件,然后查看文件,再修改权限,运行,最后删除 1.新建文件: touch Test.sh 补充:新建文件有好多种方式,一般用mkdir(创建目录,即文件夹).touc ...
- MyEclipse10.0 采用插件方式安装 SVN(转)
原文:http://blog.sina.com.cn/s/blog_a5f093b401015uzl.html 一.到官方上下载svn1.8.3,下载后的文件名叫site-1.8.3.zip 地址:h ...
- new Class{}形式
先看下面代码 Test.java public class Test { public static void main(String[] args) { A a=new A() { @Overrid ...