Spring 2 资源
来源:互联网 发布:java判断文件是否存在 编辑:程序博客网 时间:2024/06/06 13:06
Spring 提供一个Resource接口来统一这些底层资源一致的访问,而且提供了一些便利的接口,从而能提供我们的生产力。
Resource接口代表底层外部资源,提供了对底层外部资源的一致性访问接口
public interface InputStreamSource {
InputStream getInputStream() throws IOException;
}
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();
}
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;
}
}
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"/>
<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>
<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
- Spring 2 资源
- spring资源2
- Spring framework(2):资源访问&资源加载
- spring资源
- Spring-资源
- Spring资源
- Spring相关资源
- Spring网上学习资源
- Spring 资源读取问题
- spring加载资源文件
- spring加载资源文件
- Chapter 4. Spring 资源
- Spring中的资源访问
- spring 读取资源文件
- Spring的资源访问
- spring 读取资源文件
- Spring资源访问
- Spring文件资源操作
- ubuntu下无法更新android sdk
- css中的滚动条以及清浮动
- HDOJ 4183 - Pahom on Water 拆点最大流
- Java 的JSON、XML转换方法——目录索引
- 网页中嵌入Flash之OBJECT和EMBED标签
- Spring 2 资源
- office word细节操作
- Spring 3 AOP 0 AOP基础
- Spring 3 AOP 1 简单AOP
- 浙江大学PAT上机题解析之1011. A+B和C (15)
- 10 Powerful Quotes From The Steve Jobs Movie And What They Teach Us About Leadership
- iOS arc机制 strong weak
- java 读取 properties 文件
- 关于typedef的用法总结