001/* 002 * Copyright 2002-2019 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.context; 018 019import org.springframework.beans.factory.Aware; 020import org.springframework.core.io.ResourceLoader; 021 022/** 023 * Interface to be implemented by any object that wishes to be notified of the 024 * {@link ResourceLoader} (typically the ApplicationContext) that it runs in. 025 * This is an alternative to a full {@link ApplicationContext} dependency via 026 * the {@link org.springframework.context.ApplicationContextAware} interface. 027 * 028 * <p>Note that {@link org.springframework.core.io.Resource} dependencies can also 029 * be exposed as bean properties of type {@code Resource}, populated via Strings 030 * with automatic type conversion by the bean factory. This removes the need for 031 * implementing any callback interface just for the purpose of accessing a 032 * specific file resource. 033 * 034 * <p>You typically need a {@link ResourceLoader} when your application object has to 035 * access a variety of file resources whose names are calculated. A good strategy is 036 * to make the object use a {@link org.springframework.core.io.DefaultResourceLoader} 037 * but still implement {@code ResourceLoaderAware} to allow for overriding when 038 * running in an {@code ApplicationContext}. See 039 * {@link org.springframework.context.support.ReloadableResourceBundleMessageSource} 040 * for an example. 041 * 042 * <p>A passed-in {@code ResourceLoader} can also be checked for the 043 * {@link org.springframework.core.io.support.ResourcePatternResolver} interface 044 * and cast accordingly, in order to resolve resource patterns into arrays of 045 * {@code Resource} objects. This will always work when running in an ApplicationContext 046 * (since the context interface extends the ResourcePatternResolver interface). Use a 047 * {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver} as 048 * default; see also the {@code ResourcePatternUtils.getResourcePatternResolver} method. 049 * 050 * <p>As an alternative to a {@code ResourcePatternResolver} dependency, consider 051 * exposing bean properties of type {@code Resource} array, populated via pattern 052 * Strings with automatic type conversion by the bean factory at binding time. 053 * 054 * @author Juergen Hoeller 055 * @author Chris Beams 056 * @since 10.03.2004 057 * @see ApplicationContextAware 058 * @see org.springframework.core.io.Resource 059 * @see org.springframework.core.io.ResourceLoader 060 * @see org.springframework.core.io.support.ResourcePatternResolver 061 */ 062public interface ResourceLoaderAware extends Aware { 063 064 /** 065 * Set the ResourceLoader that this object runs in. 066 * <p>This might be a ResourcePatternResolver, which can be checked 067 * through {@code instanceof ResourcePatternResolver}. See also the 068 * {@code ResourcePatternUtils.getResourcePatternResolver} method. 069 * <p>Invoked after population of normal bean properties but before an init callback 070 * like InitializingBean's {@code afterPropertiesSet} or a custom init-method. 071 * Invoked before ApplicationContextAware's {@code setApplicationContext}. 072 * @param resourceLoader the ResourceLoader object to be used by this object 073 * @see org.springframework.core.io.support.ResourcePatternResolver 074 * @see org.springframework.core.io.support.ResourcePatternUtils#getResourcePatternResolver 075 */ 076 void setResourceLoader(ResourceLoader resourceLoader); 077 078}