# 2.6资源作为依赖

如果bean本身要通过某种动态过程来确定和提供资源路径，那么bean使用ResourceLoader接口来加载资源可能是有意义的。例如，考虑加载某种类型的模板，其中所需的特定资源取决于用户的角色。如果资源是静态的，那么完全消除对ResourceLoader接口的使用是有意义的，让bean公开它所需的Resource属性，并期望将它们注入其中。

然后注入这些属性变得很简单，因为所有应用程序上下文都注册并使用一个特殊的JavaBeans PropertyEditor，它可以将String路径转换为Resource对象。因此，如果mybean具有类型Resource的template属性，那么可以为该资源配置一个简单的字符串，如下例所示：

```
<bean id="myBean" class="...">
    <property name="template" value="some/resource/path/myTemplate.txt"/>
</bean>
```

注意resource path没有前缀。因为application context 本身就是ResourceLoader， resource 将会根据context的类型不同去加载ClassPathResource ，FileSystemResource 或者ServletContextResource。

如果你想强制使用某一类型的Resource，那么可以加上前缀。下面是强制使用ClassPathResource 和UrlResource 的例子：

```
<property name="template" value="classpath:some/resource/path/myTemplate.txt">

<property name="template" value="file:///some/resource/path/myTemplate.txt"/>
```


---

# 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.6resource-as-dependencies.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.
