spring中ClassPathXmlApplicationContext和FileSystemXmlApplicationContext的路径问题

本文介绍了两种常用的Spring框架中XML配置文件的加载方式:ClassPathXmlApplicationContext和FileSystemXmlApplicationContext。详细解释了每种方式的使用场景及路径指定方法,并提供了获取classpath路径的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ClassPathXmlApplicationContext:

(1)一般使用classpath路径,前缀可加可不加,例如:new ClassPathXmlApplicationContext("classpath:conf/core/train-servlet.xml")

(2)也可以使用绝对路径,加上file:表示绝对路径,一般不建议使用绝对路径

FileSystemXmlApplicationContext:

(1)用文件系统的路径,默认指定项目的根路径,例如:new FileSystemXmlApplicationContext("src/conf/core/train-servlet.xml")

(2)也可以使用classpath路径,这样就能读到classpath下面的相对路径,例如:new FileSystemXmlApplicationContext("classpath:conf/core/train-servlet.xml")

(3)不需要加file:前缀表示绝对路径,不提倡使用


附:

classpath路径的获取:

(1)Thread.currentThread().getContextClassLoader().getResource("").getPath() 

(2)TestClass.class.getClassLoader().getResource("").getPath() (TestClass为src某一个包中的类)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值