The ResourceLoaderAware interface is
a special marker interface, identifying objects that expect to be provided
with a ResourceLoader reference.
public interface ResourceLoaderAware { void setResourceLoader(ResourceLoader resourceLoader); }
When a class implements
ResourceLoaderAware and is deployed into an
application context (as a Spring-managed bean), it is recognized as
ResourceLoaderAware by the application
context. The application context will then invoke the
setResourceLoader(ResourceLoader), supplying
itself as the argument (remember, all application contexts in Spring
implement the ResourceLoader
interface).
Of course, since an
ApplicationContext is a
ResourceLoader, the bean could also
implement the ApplicationContextAware
interface and use the supplied application context directly to load
resources, but in general, it's better to use the specialized
ResourceLoader interface if that's all
that's needed. The code would just be coupled to the resource loading
interface, which can be considered a utility interface, and not the whole
Spring ApplicationContext interface.
As of Spring 2.5, you can rely upon autowiring of the
ResourceLoader as an alternative to
implementing the ResourceLoaderAware interface.
The "traditional" constructor and byType
autowiring modes (as described in Section 3.4.5, “Autowiring collaborators”)
are now capable of providing a dependency of type
ResourceLoader for either a
constructor argument or setter method parameter respectively. For more flexibility
(including the ability to autowire fields and multiple parameter methods), consider
using the new annotation-based autowiring features. In that case, the
ResourceLoader will be autowired into a field,
constructor argument, or method parameter that is expecting the
ResourceLoader type as long as the field,
constructor, or method in question carries the
@Autowired annotation. For more information,
see Section 3.9.2, “@Autowired and @Inject”.