The ResourceLoaderAware
interface is a special callback interface that identifies components that accept a reference to the ResourceLoader
that is provided to them. The following listing shows the definition of the ResourceLoaderAware
interface:
public interface ResourceLoaderAware {
void setResourceLoader(ResourceLoader resourceLoader);
}
If a class implements ResourceLoaderAware
and is deployed in an application context (as a managed Spring bean), it is recognized as ResourceLoaderAware
by the application context. The application context then calls setResourceLoader(ResourceLoader)
, providing itself as an argument (remember that all application contexts in Spring implement the ResourceLoader
interface).
Because ApplicationContext
is a ResourceLoader
, the bean can also implement the ApplicationContextAware
interface and use the provided application context directly to load resources. However, in general, it's better to use the specialized ResourceLoader
interface if that's all you need. The code will only be associated with the resource loading interface (which can be considered a service interface), and not with the entire Spring ApplicationContext
interface.
Application components can also rely on ResourceLoader
automatic discovery and binding as an alternative to implementing the ResourceLoaderAware
interface. Traditional auto-detection and binding modes constructor
and byType
are capable of providing a ResourceLoader
for either a constructor argument or a setter parameter, respectively. For greater flexibility (including the ability to auto-discover and link fields and methods with multiple parameters), consider using annotation-based auto-discovery and linking features. In this case, ResourceLoader
is automatically discovered and associated with a field, constructor argument, or method parameter that expects a ResourceLoader
type if the field, constructor, or method contains the @Autowired
annotation.
Resource
objects for a resource path that contains wildcards or uses a special resource prefix
classpath*:
, instead of
ResourceLoader
, an instance of
ResourcePatternResolver
should be associated with application components.
GO TO FULL VERSION