ClassXmlAplicationContext、FileSystemXmlApplicationContext

本人在测试一个方法时需要加载XML配置文件,spring提供了相应的方法,就小小研究了下,在此记录下具体的过程,方便初学者和自己日后回顾。 


Spring容器最基本的接口就是BeanFactory.BeanFactory负责配置、创建、管理Bean,它有一个子接口ApplicationContext,也称为Spring上下文。Spring容器负责管理Bean与Bean之间的信赖关系。

BeanFactory有很多实现类,通常使用org.springframework.beans.factory.xml.XmlBeanFactory类。但对于大部分J2EE应用而言,推荐使 用ApplicationContext.ApplicationContext是BeanFactory的子接口,其常用实现类是org.springframework.context.support.FileSystemXmlApplicationContextorg.springframework.context.support.ClassXmlAplicationContext
Springr的配置信息通常采用XML配置文件来设置,因此,创建BeanFactory实例时,应该提供XML配置文件作为参数。、

下面详细介绍ApplicationContext的实际运用:

一:ClassPathXmlApplicationContext
1.没有前缀:默认为项目的classpath下相对路径
ApplicationContext appCt = newClassPathXmlApplicationContext("app.spring.xml");

2.前缀classpath:表示的是项目的classpath下相对路径
ApplicationContext appCt = newClassPathXmlApplicationContext("classpath:app.spring.xml");

3.使用前缀file 表示的是文件的绝对路径
ApplicationContext appCt = newClassPathXmlApplicationContext("file:D:/app.spring.xml");

4.可以同时加载多个文件
String[] xmlCfg = new String[] { "classpath:base.spring.xml","app.spring.xml"};
ApplicationContext appCt = new ClassPathXmlApplicationContext(xmlCfg);

5.使用通配符加载所有符合要求的文件
ApplicationContext appCt = newClassPathXmlApplicationContext("*.spring.xml");

二:FileSystemXmlApplicationContext
1.默认为项目工作路径即项目的根目录
ApplicationContext appCt2 = newFileSystemXmlApplicationContext("src/main/resources/app.spring.xml");

2.前缀classpath:表示的是项目的classpath下相对路径
ApplicationContext appCt2 = newFileSystemXmlApplicationContext("classpath:app.spring.xml");

3.使用前缀file 表示的是文件的绝对路径
ApplicationContext appCt2 = new FileSystemXmlApplicationContext("file:D:/app.spring.xml");
ApplicationContext appCt2 = new FileSystemXmlApplicationContext("D:/app.spring.xml");

4.可以同时加载多个文件
String[] xmlCfg = new String[] { "src/main/resources/base.spring.xml","classpath:app.spring.xml"};
ApplicationContext appCt2 = new FileSystemXmlApplicationContext(xmlCfg);

5.使用通配符加载所有符合要求的文件
ApplicationContext appCt2 = newFileSystemXmlApplicationContext("classpath:*.spring.xml");

详细代码如下:

Java代码

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import org.springframework.context.support.FileSystemXmlApplicationContext;
  4.  
  5. import aoplog.LogAfterAdvice;
  6. import aoplog.LogBeforeAdvice;
  7.  
  8. /**
  9. * @author Michael
  10. *
  11. */
  12. public class TestApplicationContext {
  13.  
  14. /**
  15. * @param args
  16. */
  17. public static void main(String[] args) {
  18.  
  19. /**
  20. * ClassPathXmlApplicationContext
  21. */
  22. // 没有前缀:默认为项目的classpath下相对路径
  23. ApplicationContext appCt = new ClassPathXmlApplicationContext(
  24. "app.spring.xml");
  25.  
  26. // 前缀classpath:表示的是项目的classpath下相对路径
  27. // ApplicationContext appCt = new ClassPathXmlApplicationContext(
  28. // "classpath:app.spring.xml");
  29.  
  30. // 使用前缀file 表示的是文件的绝对路径
  31. // ApplicationContext appCt = new ClassPathXmlApplicationContext(
  32. // "file:D:/app.spring.xml");
  33.  
  34. LogBeforeAdvice logBefore = (LogBeforeAdvice) appCt
  35. .getBean("logBefore");
  36. System.out.println("ClassPathXmlApplicationContext test:"
  37. + logBefore.getClass());
  38.  
  39. // 利用通配符文件加载
  40. ApplicationContext appCtXx = new ClassPathXmlApplicationContext(
  41. "*.spring.xml");
  42.  
  43. // 多文件加载
  44. String[] xmlCfg = new String[] { "classpath:base.spring.xml",
  45. "myapp.spring.xml" };
  46. ApplicationContext appCtMore = new ClassPathXmlApplicationContext(
  47. xmlCfg);
  48.  
  49. /*
  50. * FileSystemXmlApplicationContext
  51. */
  52.  
  53. // 默认为项目工作路径 即项目的根目录
  54. ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
  55. "src/main/resources/app.spring.xml");
  56.  
  57. // 前缀classpath:表示的是项目的classpath下相对路径
  58. // ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
  59. // "classpath:app.spring.xml");
  60.  
  61. // 使用前缀file 表示的是文件的绝对路径
  62. // ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
  63. // "file:D:/app.spring.xml");
  64.  
  65. LogAfterAdvice logAfter = (LogAfterAdvice) appCt2.getBean("logAfter");
  66. System.out.println("FileSystemXmlApplicationContext test:"
  67. + logAfter.getClass());
  68. }
  69.  
  70. }

 

 本文转自 http://sjsky.iteye.com/blog/787101

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值