Spring 2 资源

Spring 提供一个Resource接口来统一这些底层资源一致的访问,而且提供了一些便利的接口,从而能提供我们的生产力。

Resource接口代表底层外部资源,提供了对底层外部资源的一致性访问接口
public interface InputStreamSource {
    InputStream getInputStream() throws IOException;
}

public interface   Resource  extends InputStreamSource {
       boolean exists();
       boolean isReadable();
       boolean isOpen();
       URL getURL() throws IOException;
       URI getURI() throws IOException;
       File getFile() throws IOException;
       long contentLength() throws IOException;
       long lastModified() throws IOException;
       Resource createRelative(String relativePath) throws IOException;
       String getFilename();
       String getDescription();
}

内置Resource实现:ByteArrayResource、InputStreamResource 、FileSystemResource 、UrlResource 、ClassPathResource、ServletContextResource、VfsResource等。

ResourceLoader接口,ResourceLoader接口用于返回Resource对象;其实现可以看作是一个生产Resource的工厂类。
目前所有ApplicationContext都实现了ResourceLoader,因此可以使用其来加载资源。

ClassPathXmlApplicationContext:不指定前缀将返回默认的ClassPathResource资源,否则将根据前缀来加载资源;
FileSystemXmlApplicationContext:不指定前缀将返回FileSystemResource,否则将根据前缀来加载资源;
WebApplicationContext:不指定前缀将返回ServletContextResource,否则将根据前缀来加载资源;

Resource template = ctx.getResource( "some/resource/path/myTemplate.txt"  );
Resource template = ctx.getResource( "classpath:some/resource/path/myTemplate.txt"  );
Resource template = ctx.getResource( "file:/some/resource/path/myTemplate.txt"  );
Resource template = ctx.getResource( "http://myhost.com/resource/path/myTemplate.txt"  );

注入Resource,
package cn.javass.spring.chapter4.bean;
import org.springframework.core.io.Resource;
public class ResourceBean3 {
    private   Resource resource;
    public Resource getResource() {
        return resource;
    }
    public void setResource(Resource resource) {
        this.resource = resource;
    }
}
<bean id="resourceBean1" class="cn.javass.spring.chapter4.bean.ResourceBean3">
       <property name="resource" value="cn/javass/spring/chapter4/test1.properties"/>
</bean>
<bean id="resourceBean2" class="cn.javass.spring.chapter4.bean.ResourceBean3">
      <property name="resource"  value="classpath:cn/javass/spring/chapter4/test1.properties"/>
</bean>

Application contexts and Resource paths
ApplicationContext ctx =   new  ClassPathXmlApplicationContext( "conf/appContext.xml");
ApplicationContext ctx =  new  FileSystemXmlApplicationContext( "conf/appContext.xml");
ApplicationContext ctx =   new  ClassPathXmlApplicationContext( new  String[] { "services.xml",   "daos.xml"}, MessengerService.   class  );

路径通配符
?:匹配一个字符,如“config?.xml”将匹配“config1.xml”;
*:匹配零个或多个字符串,如“cn/*/config.xml”将匹配“cn/javass/config.xml”,但不匹配匹配“cn/config.xml”;而“cn/config-*.xml”将匹配“cn/config-dao.xml”;
 **:匹配路径中的零个或多个目录,如“cn/**/config.xml”将匹配“cn /config.xml”,也匹配“cn/javass/spring/config.xml”;而“cn/javass/config-**.xml”将匹配“cn/javass/config-dao.xml”,即把“**”当做两个“*”处理。
Spring在加载类路径资源时除了提供前缀“classpath:”的来支持加载一个Resource,还提供一个前缀   classpath*:来支持加载所有匹配的类路径Resource。

/WEB-INF/*-context.xml
com/mycompany/**/applicationContext.xml
file:C:/some/path/*-context.xml
classpath:com/mycompany/**/applicationContext.xml


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值