# 2.7. 资源作为依赖

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

然后注入这些属性变得微不足道的是，所有应用程序上下文都注册并使用一个特殊的 JavaBeans `PropertyEditor`，它可以将`String`路径转换为`Resource`对象。例如，以下`MyBean`类具有`Resource` 类型的`template`属性。

```java
package example;

public class MyBean {

    private Resource template;

    public setTemplate(Resource template) {
        this.template = template;
    }

    // ...
}
```

在 XML 配置文件中，可以使用该资源的简单字符串配置`template`属性，如以下示例所示：

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

请注意，资源路径没有前缀。因此，由于应用程序上下文本身将用作`ResourceLoader`，因此资源通过 `ClassPathResource`、 `FileSystemResource`或 `ServletContextResource`加载，具体取决于应用程序上下文的确切类型。

如果需要强制使用特定`Resource`类型，可以使用前缀。以下两个示例展示了如何强制使用 `ClassPathResource`和 `UrlResource`（后者用于访问文件系统中的文件）：

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

如果`MyBean`类被重构以用于注解驱动的配置，则`myTemplate.txt`可以将路径存储在名为`template.path`的键下 - 例如，在 Spring `Environment`可用的属性文件中（请参阅 [环境抽象](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-environment)）。然后可以 使用属性占位符通过注解`@Value`引用模板路径（请参阅[使用`@Value`](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-value-annotations)）。Spring 会将模板路径的值作为字符串检索，而 指定的`PropertyEditor`会将字符串转换`Resource`为要注入`MyBean`构造函数的对象。以下示例演示了如何实现此目的。

```java
@Component
public class MyBean {

    private final Resource template;

    public MyBean(@Value("${template.path}") Resource template) {
        this.template = template;
    }

    // ...
}
```

如果我们想支持在类路径中多个位置的同一路径下发现的多个模板——例如，在类路径中的多个 jar 中——我们可以使用特殊`classpath*:`前缀和通配符将`templates.path`键定义为 `classpath*:/config/templates/*.txt`. 如果重新定义如下`MyBean`类，Spring 会将模板路径模式转换为`Resource`可以注入`MyBean`构造函数的对象数组。

```java
@Component
public class MyBean {

    private final Resource[] templates;

    public MyBean(@Value("${templates.path}") Resource[] templates) {
        this.templates = templates;
    }

    // ...
}
```
