06 Spring框架 依赖注入(三)多配置文件
来源:互联网 发布:淘宝模特摄影师 编辑:程序博客网 时间:2024/06/06 00:35
在Spring前几节的学习中我们都使用了一个配置文件,就像struts2中可以包含其他的配置文件,我们能不能使用多个配置文件呢(在工程比庞大,配置比较多的时候)?
Spring多配置文件分为两种:
- 平等关系的配置文件
- 包含子配置文件
(一)平等关系的配置文件
我们可以创建两个配置文件在包下
我们可以同时使用这两个配置文件在我们的应用当中,使用的方式由很多种,这里我举出比较常用的几种:
①
//Spring-*.xml只要配置文件的前缀相同我们就可以这样使用String resource = "com/testConfig/Spring-*.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
②
我们看看ClassPathXmlApplicationContext的构造函数,第四个构造函数提供了多个参数,这也就是说我们可以填写几个配置文件的String路径
@Test public void Test02() { String resource1 = "com/testConfig/Spring-base.xml"; String resource2 = "com/testConfig/Spring-equal.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource1,resource2); Book book = (Book)ac.getBean("book"); System.out.println(book); }
③
我们观察上面的ClassPathXmlApplicationContext构造函数,我们同样可以填写一个路径字符串数组:
@Test public void Test03() { String resource1 = "com/testConfig/Spring-base.xml"; String resource2 = "com/testConfig/Spring-equal.xml"; String[]resource = {resource1,resource2}; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); Book book = (Book)ac.getBean("book"); System.out.println(book); }
(二)包含子配置文件
上面说的几种方法在地位上都是平等的,接下来我们来配置一个主配置文件的子配置文件:
这种包含关系的配置文件和Struts2中的方式就比较像了只需要在主配置文件中加一个import标签:
同样的如果在主配置文件base中要import多个配置文件的话,我们也可以这样:
<!--引入多个配置文件--><import resource="Spring-*.xml"><!--注意:总的配置文件不能和包含的子配置文件格式相同,否则会将自身包含进去,出错-->
阅读全文
1 0
- 06 Spring框架 依赖注入(三)多配置文件
- Spring框架(三)依赖注入之注解方式
- spring-依赖注入 配置文件
- (三)Spring 依赖注入
- Spring 框架依赖注入
- spring框架依赖注入
- Spring配置文件解析-依赖注入
- spring框架学习(二)依赖注入
- spring框架学习(二)依赖注入
- spring框架学习(二)依赖注入
- spring框架学习(二)依赖注入
- spring框架学习(二)依赖注入
- spring框架学习(二)依赖注入
- spring框架(二)依赖注入
- spring框架学习(二)依赖注入
- spring框架学习(二)依赖注入
- spring框架学习(二)依赖注入
- spring 框架学习(二) 依赖注入
- ServletRequest学习
- JAVA文件上传和下载
- (6)实现源码
- 设计模式(十一)——抽象工厂
- [微信小程序]根据图片宽高动态改变swiper(轮播图)组件宽高
- 06 Spring框架 依赖注入(三)多配置文件
- ehcache配置参数说明
- studio界面、快捷键
- iOS 获取当前网络的状态
- 简单的angular方法简介
- Qt学习- 第一章 什么是Qt
- 区间求和II
- Python的下划线
- 【OI之路】11更高级数论-3快速傅里叶变换