下面列举一些spring读取配置文件路径的代码!
包括注解形式的
配置文件放在src目录下:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springMVCForm-servlet.xml");
WEB-INF下:
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("WebContent/WEB-INF/springMVCForm-servlet.xml");
多个文件可用*表示:
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("WebContent/WEB-INF/springMVCForm-*.xml");
注解方式:
配置文件放在class目录下:
@ContextConfiguration(locations={"classpath:springMVCForm-servlet.xml"})
WEB-INF下面的
@ContextConfiguration(locations={"file:WebContent/WEB-INF/springMVCForm-servlet.xml"})
本文介绍了Spring框架中不同位置配置文件的加载方法,包括使用ClassPathXmlApplicationContext和FileSystemXmlApplicationContext类来加载src目录、WEB-INF目录下的配置文件,以及通过通配符加载多个配置文件的方式。同时,还展示了如何利用注解@ContextConfiguration来指定配置文件的位置。
9625

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



