转载自
跳刀的兔子 http://www.cnblogs.com/shipengzhi/articles/3029872.html
加载文件顺序
情形一:使用classpath加载且不含通配符
这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource
方法获取资源的
URL
,如果无法获得当前线程的
ClassLoader
,
Spring
将使用加载类
org.springframework.util.ClassUtils的ClassLoader。
1.当工程目录结构如图所示:

ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");
加载[conf/application-context.xml]
2.当工程目录结构如图所示:


ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");
加载[conf/application-context.xml]
3. 当工程目录结构如图所示:


ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");
只会会加载bin/conf目录下的application-context.xml文件,不会加载jar包中的conf/application-context.xml。
情形二:使用classpath加载,包含通配符
Spring会通过使用路径中的非通配符部分先确定资源的大致位置,然后根据这个位置在确定具体的资源位置

ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");
加载[admin-application-context.xml]
加载[application-context.xml]
2.当工程目录结构如图所示:


ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");
加载conf/application-context.xml
加载conf/admin/admin-application-context.xml
3.当工程目录结构如图所示:


ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");
bin/conf/application-context.xml文件和bin/conf/admin/admin-application-context.xml都会被加载,
但conf.jar文件中的配置文件并不会被加载。
情形三:使用classpath*前缀且不包含通配符
使用classpath*前缀可以获取所有与给定路径匹配的classpath资源,从而避免出现两个不同位置有相同名字的文件,Spring只加载其中一个的情况。


这时使用
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:conf/application-context.xml");
Spring将会加载bin目录下的application-context.xml文件和jar包里的application-context.xml文件。
情形四:使用classpath*前缀,包含通配符
当工程目录结构如图所示:


ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:conf/**/*application-context.xml");
conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml和 bin/conf/admin/admin-application-context.xml
以及jar包中的 conf/application-context.xml和 conf/admin/admin-application-context.xml都会被加载
Classpath*加载和Classpath加载区别
classpath*:的出现是为了从classpath和多个jar文件中加载相同的文件,classpath:只能加载找到的第一个文件。
classpath*载使用了classloader的getResources() 方法;
PathMatchingResourcePatternResolver类中,我们可以更清楚的了解其对的处理:如果是以classpath*开头,它会遍历classpath。
最终从文件加载的时候仍然是JAVA中常见的读取文件的方法:
如ClassPathResource得到inputstream的方法是利用class loader。
如ClassPathResource得到inputstream的方法是利用class loader。
public InputStream getInputStream() throws IOException {
InputStream is;
if (this.clazz != null) {
is = this.clazz.getResourceAsStream(this.path);
}
如FileSystemResource得到inputstream的方法是利用FileInputStream。
public InputStream getInputStream() throws IOException {
return new FileInputStream(this.file);
}