ClassPathXmlApplicationContext和FileSystemXmlApplicationContext的区别

IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
Spring提供IOC容器的两种实现方式(两个接口):

  • BeanFactory:IOC容器的基本实现,是Spring内部的使用接口
    注:加载配置文件的时候不会创建对象,在获取对象(使用)才去创建对象
  • ApplicationContext:BeanFactory接口的子接口,提供更多更强大的功能,一般由开发人员进行使用
    注:加载配置文件的时候就会创建对象

下面介绍ApplicationContext常用的接口:
在这里插入图片描述

一、ClassPathXmlApplicationContext

(1)空前缀默认扫描项目的classpath下相对路径(即src包下的相对路径

ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

(2)classpath前缀:表示扫描的是项目的classpath下相对路径

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean1.xml"); 

(3)file前缀: 表示扫描的是文件的绝对路径

ApplicationContext context = new ClassPathXmlApplicationContext("file:D:/.../bean1.xml"); 

(4)可以同时加载多个文件

String[] locations = {"classpath:bean1.xml","bean2.xml"}; 
ApplicationContext context = new ClassPathXmlApplicationContext(locations); 

(5)使用通配符加载所有符合要求的文件

ApplicationContext context = new ClassPathXmlApplicationContext("bean*.xml"); 

二、FileSystemXmlApplicationContext

(1)空前缀:默认为项目的根目录(即项目包名下的相对路径)

ApplicationContext context = new FileSystemXmlApplicationContext("src/bean1.xml"); 

(2)classpath前缀:表示扫描的是项目的classpath下相对路径

ApplicationContext context = new FileSystemXmlApplicationContext("classpath:bean1.xml"); 

(3)file前缀: 表示扫描的是文件的绝对路径,不加file前缀也是一样的

ApplicationContext context = new FileSystemXmlApplicationContext("file:D:/.../bean1.xml"); 
ApplicationContext context = new FileSystemXmlApplicationContext("D:/.../bean1.xml"); 

(4)可以同时加载多个文件

String[] locations = {"classpath:bean1.xml","bean2.xml"}; 
ApplicationContext context= new FileSystemXmlApplicationContext(locations);

(5)使用通配符加载所有符合要求的文件

ApplicationContext context = new FileSystemXmlApplicationContext("classpath:bean*.xml"); 

综上,ClassPathXmlApplicationContext和FileSystemXmlApplicationContext用法类似,仅仅是在参数默认情况下有所不同:前一个默认扫描src包下,后一个默认扫描project包(项目包)下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值