> For the complete documentation index, see [llms.txt](https://docs.flydean.com/spring-framework-documentation5/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flydean.com/spring-framework-documentation5/core-technologies/2.resources/2.4resourceloader.md).

# 2.4ResourceLoader

ResourceLoader用来返回Resource实例，下面是其定义：

```
public interface ResourceLoader {

    Resource getResource(String location);

}
```

所有的 application contexts 都实现了ResourceLoader类。因此所有的application contexts 都可以用来获取Resource。

当你在特定的应用程序上下文上调用getResource（），并且指定的位置路径没有特定的前缀时，你将返回适合该特定应用程序上下文的资源类型。例如，假设对ClassPathXmlApplicationContext实例执行了以下代码片段：

```
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
```

在ClassPathXmlApplicationContext中，这个方法返回ClassPathResource，如果在FileSystemXmlApplicationContext中，方法返回FileSystemResource。 在WebApplicationContext， 方法返回ServletContextResource。 他会返回和ApplicationContext相对应的Resource实现。

因此，你可以以适合特定应用程序上下文的方式加载资源。

当然，你可以强制ClassPathResource使用，而不管ApplicationContext到底是什么。这样做你需要添加classpath:前缀。如下：

```
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
```

同样的，你可以强制使用UrlResource通过添加标准的java.net.URL前缀。

```
Resource template = ctx.getResource("file:///some/resource/path/myTemplate.txt");

Resource template = ctx.getResource("https://myhost.com/resource/path/myTemplate.txt");
```

下面是将String转换为Resource的策略：

| Prefix     | Example                        | Explanation              |
| ---------- | ------------------------------ | ------------------------ |
| classpath: | classpath:com/myapp/config.xml | 从classpath加载             |
| file:      | file:///data/config.xml        | 从文件系统以URL方式加载            |
| http:      | <https://myserver/logo.png>    | URL方式加载                  |
| (none)     | /data/config.xml               | 依赖于ApplicationContext实现。 |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.flydean.com/spring-framework-documentation5/core-technologies/2.resources/2.4resourceloader.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
