1.spring的核心配置文件放在不同的位置,如何读取?
(1)项目路径下:beans.xml在项目路径下的config目录下
ApplicationContext ac=new FileSystemXmlApplicationContext("config/beans.xml");
(2)类路径下:把beans.xml放在src目录下(说资源文件时,类路径是在项目src下,说项目的时候,类路径是编译后.class路径)
ApplicationContext ac=new FileSystemXmlApplicationContext("classpath:beans_classpath.xml");
(3)硬盘目录: d:/beans.xml
ApplicationContext ac=new FileSystemXmlApplicationContext("file:d:/beans_file.xml");
(4)如何一次读取多个文件?
① 使用组数的形式,读取配置文件
String[ ] files=new String[ ]{"classpath:beans_classpath.xml","config/beans.xml","file:d:/beans_file.xml"};
ApplicationContext act=new FileSystemXmlApplicationContext(files);
② 使用通配符读取配置文件
ApplicationContext ac=new FileSystemXmlApplicationContext("classpath:beans_*.xml");

本文介绍了Spring框架中核心配置文件的不同加载方式,包括从项目路径、类路径、硬盘目录加载配置文件的方法,以及如何批量加载多个配置文件。
2086

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



