classpath:与classpath*:分析

来源:互联网 发布:网站程序和网站源码 编辑:程序博客网 时间:2024/05/18 08:23

1.在spring中classpath:和classpath*:为前缀的文件加载过程:
classpath:

String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;/** Pseudo URL prefix for loading from the class path: "classpath:" */    public static final String CLASSPATH_URL_PREFIX = "classpath:";//(方法路径)org.springframework.core.io.DefaultResourceLoader#getResourcepublic Resource getResource(String location) {        Assert.notNull(location, "Location must not be null");        if (location.startsWith(CLASSPATH_URL_PREFIX)) {            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());        }        else {            try {                // Try to parse the location as a URL...                URL url = new URL(location);                return new UrlResource(url);            }            catch (MalformedURLException ex) {                // No URL -> resolve as resource path.                return getResourceByPath(location);            }        }    }//(方法路径):org.springframework.core.io.ClassPathResource    public ClassPathResource(String path, ClassLoader classLoader) {        Assert.notNull(path, "Path must not be null");        String pathToUse = StringUtils.cleanPath(path);        if (pathToUse.startsWith("/")) {            pathToUse = pathToUse.substring(1);        }        this.path = pathToUse;        this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());    }

上面的是classpath:的加载方式,从getResource()可以看出,如果是以classpath:为前缀的,调用ClassPathResource(),返回资源路径.

classpath*:

String CLASSPATH_ALL_URL_PREFIX = "classpath*:";//(方法路径:org.springframework.core.io.support.PathMatchingResourcePatternResolver)public Resource[] getResources(String locationPattern) throws IOException {        Assert.notNull(locationPattern, "Location pattern must not be null");        if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {            // a class path resource (multiple resources for same name possible)            //是否包含?或者*匹配符            if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {                // a class path resource pattern                //(Find all resources that match the given location pattern via the     * Ant-style PathMatcher. Supports resources in jar files and zip files     * and in the file system.)这是findPathMatchingResources的解释                return findPathMatchingResources(locationPattern);            }            else {                // all class path resources with the given name                return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));            }        }        //......    }//findAllClassPathResources()方法protected Resource[] findAllClassPathResources(String location) throws IOException {        String path = location;        if (path.startsWith("/")) {            path = path.substring(1);        }        Enumeration<URL> resourceUrls = getClassLoader().getResources(path);        Set<Resource> result = new LinkedHashSet<Resource>(16);        while (resourceUrls.hasMoreElements()) {            URL url = resourceUrls.nextElement();            result.add(convertClassLoaderURL(url));        }        return result.toArray(new Resource[result.size()]);    }//Finds all the resources with the given name.//可以看出如果有父加载器.向上加载,可以加载jar包中的public Enumeration<URL> getResources(String name) throws IOException {        Enumeration[] tmp = new Enumeration[2];        if (parent != null) {            tmp[0] = parent.getResources(name);        } else {            tmp[0] = getBootstrapResources(name);        }        tmp[1] = findResources(name);        return new CompoundEnumeration<>(tmp);    }

2.总结:(classpath是WEB-INF/classes,lib)
a)lib和classes同属classpath,两者的访问优先级为: lib>classes。
b)区别:
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中的class路径。如果有多个同名文件,都会加载

原创粉丝点击