# 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"/>
```
