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某一个包中的类)
本文介绍了两种常用的Spring框架中XML配置文件的加载方式:ClassPathXmlApplicationContext和FileSystemXmlApplicationContext。详细解释了每种方式的使用场景及路径指定方法,并提供了获取classpath路径的方法。
808

被折叠的 条评论
为什么被折叠?



