001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.io; 018 019import org.springframework.lang.Nullable; 020import org.springframework.util.ResourceUtils; 021 022/** 023 * Strategy interface for loading resources (e.. class path or file system 024 * resources). An {@link org.springframework.context.ApplicationContext} 025 * is required to provide this functionality, plus extended 026 * {@link org.springframework.core.io.support.ResourcePatternResolver} support. 027 * 028 * <p>{@link DefaultResourceLoader} is a standalone implementation that is 029 * usable outside an ApplicationContext, also used by {@link ResourceEditor}. 030 * 031 * <p>Bean properties of type Resource and Resource array can be populated 032 * from Strings when running in an ApplicationContext, using the particular 033 * context's resource loading strategy. 034 * 035 * @author Juergen Hoeller 036 * @since 10.03.2004 037 * @see Resource 038 * @see org.springframework.core.io.support.ResourcePatternResolver 039 * @see org.springframework.context.ApplicationContext 040 * @see org.springframework.context.ResourceLoaderAware 041 */ 042public interface ResourceLoader { 043 044 /** Pseudo URL prefix for loading from the class path: "classpath:". */ 045 String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX; 046 047 048 /** 049 * Return a Resource handle for the specified resource location. 050 * <p>The handle should always be a reusable resource descriptor, 051 * allowing for multiple {@link Resource#getInputStream()} calls. 052 * <p><ul> 053 * <li>Must support fully qualified URLs, e.g. "file:C:/test.dat". 054 * <li>Must support classpath pseudo-URLs, e.g. "classpath:test.dat". 055 * <li>Should support relative file paths, e.g. "WEB-INF/test.dat". 056 * (This will be implementation-specific, typically provided by an 057 * ApplicationContext implementation.) 058 * </ul> 059 * <p>Note that a Resource handle does not imply an existing resource; 060 * you need to invoke {@link Resource#exists} to check for existence. 061 * @param location the resource location 062 * @return a corresponding Resource handle (never {@code null}) 063 * @see #CLASSPATH_URL_PREFIX 064 * @see Resource#exists() 065 * @see Resource#getInputStream() 066 */ 067 Resource getResource(String location); 068 069 /** 070 * Expose the ClassLoader used by this ResourceLoader. 071 * <p>Clients which need to access the ClassLoader directly can do so 072 * in a uniform manner with the ResourceLoader, rather than relying 073 * on the thread context ClassLoader. 074 * @return the ClassLoader 075 * (only {@code null} if even the system ClassLoader isn't accessible) 076 * @see org.springframework.util.ClassUtils#getDefaultClassLoader() 077 * @see org.springframework.util.ClassUtils#forName(String, ClassLoader) 078 */ 079 @Nullable 080 ClassLoader getClassLoader(); 081 082}