Spring中 classpath* 和 classpath 前缀的区别

1     // org.springframework.core.io.support.ResourcePatternResolver
2     /**
3      * Pseudo URL prefix for all matching resources from the class path: "classpath*:"
4      * This differs from ResourceLoader's classpath URL prefix in that it
5      * retrieves all matching resources for a given name (e.g. "/beans.xml"),
6      * for example in the root of all deployed JAR files.
7      * @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX
8      */
9     String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
    // org.springframework.core.io.ResourceLoader
    /** Pseudo URL prefix for loading from the class path: "classpath:" */
    String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;

说明:写了另一篇文章,想详细了解的可以过去看看。

Spring Framework 官方文档学习(三)之Resource


简单的说,如果有多个文件时,classpath* 前缀再加上通配符,会搜到所有符合的文件;而classpath前缀再加上通配符则可能会在找到一个符合的之后不再查找。

所以使用classpath* 总是没错的。

顺便提一句,这个前缀是在ApplicationContext的实现类的构造方法中处理的,用于甄别资源(Resource)类型。

稍后会写一下Resource类型与ApplicationContext类型的关系,以及有无前缀的区别。

详见官方文档

原文地址:https://www.cnblogs.com/larryzeal/p/5915324.html