# 2.5ResourceLoaderAware接口

ResourceLoaderAware接口是一个特殊的回调，表明该组件需要提供一个ResourceLoader的引用。 下面是ResourceLoaderAware的定义：

```
public interface ResourceLoaderAware {

    void setResourceLoader(ResourceLoader resourceLoader);
}
```

当一个类实现了ResourceLoaderAware并被部署到application context，那么整个类就被识别为ResourceLoaderAware。 application context会去调用setResourceLoader(ResourceLoader)方法，并将其自身作为参数传入（所有的Spring application contexts 都实现了ResourceLoader 接口）。

因为ApplicationContext也是ResourceLoader，bean也可以实现ApplicationContextAware接口，然后直接使用提供的application context来加载资源。但是通常来说还是使用ResourceLoader来管理资源（代码会被转换为ResourceLoader而不是整个的ApplicationContext）。

在应用程序组件中，你也可以使用自动装载ResourceLoader，来替代使用ResourceLoaderAware接口。可以使用传统的constructor或者byType的自动装载模式。但是使用基于注解的自动装载可操作性更好。 在这种情况下，ResourceLoader 被自动注入到ResourceLoader类型的字段，构造函数参数，或者方法参数， 只要他们使用了@Autowired注解。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flydean.com/spring-framework-documentation5/core-technologies/2.resources/2.5resourceloaderaware.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
