spring多个配置包处理方式

一、把配置文件放进包进行统一管理

当配置内容很多时,可以拆分成多个配置文件

当配置文件很多时,可以创建package包管理配置文件

     在src下面新建package,把配置文件放到package下。

     

使用package管理的配置文件需要使用package名加文件名进行引用,package使用“/”进行分隔

[html]  view plain  copy
  1. String configLocation = "config/applicationContext.xml";  

二、配置文件的引用

构造spring运行环境时,可以使用多种构造方法加载配置文件

[html]  view plain  copy
  1. ApplicationContext context = null;  

加载一个配置文件:

[html]  view plain  copy
  1. context = new ClassPathXmlApplicationContext(configLocation);  

把多个配置文件加载到同一个spring上下文环境中:

[html]  view plain  copy
  1. context = new ClassPathXmlApplicationContext(configLocation1,configLocation2);  

也可以使用数组加载多个配置文件:

[html]  view plain  copy
  1. context = new ClassPathXmlApplicationContext(new String[] {configLocation,configLocation2});  

当使用在如Hadoop集群中,加载配置文件时:

加载一个配置文件时:

[html]  view plain  copy
  1. context = new FileSystemXmlApplicationContext(configLocation);  

配置文件的路径可以是相对路径,也可以是绝对路径

绝对路径:

[html]  view plain  copy
  1. String file1 = "D:\\lsworkspace\\springcore\\src\\config\\applicationContext.xml";  
  2. String file2 = "D:\\lsworkspace\\springcore\\src\\config\\baseConfig.xml";  

相对路径:

[html]  view plain  copy
  1. String file1 = "src\\config\\applicationContext.xml";  
  2. String file2 = "src\\config\\baseConfig.xml";  

加载多个文件方法:

[html]  view plain  copy
  1. context = new FileSystemXmlApplicationContext(file1,file2);  

或者

[html]  view plain  copy
  1. context = new FileSystemXmlApplicationContext(new String[] {file1,file2});  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值