`
angie_hawk7
  • 浏览: 46722 次
  • 性别: Icon_minigender_1
  • 来自: 乌托邦
社区版块
存档分类
最新评论

Resources

阅读更多
1:The Resource interface
引用
Spring's Resource interface is meant to be a more capable interface for abstracting access to low-level
resources.

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

public interface InputStreamSource {
InputStream getInputStream() throws IOException;
}


2:Built-in Resource implementations
UrlResource
ClassPathResource
引用

A ClassPathResource is created by Java code explicitly using the ClassPathResource
constructor, but will often be created implicitly when you call an API method which takes a String
argument which is meant to represent a path. For the latter case, a JavaBeans PropertyEditor will
recognize the special prefix classpath:on the string path, and create a ClassPathResource in
that case.

FileSystemResource
ServletContextResource
InputStreamResource
ByteArrayResource
3:The ResourceLoader
引用
The ResourceLoader interface is meant to be implemented by objects that can return (i.e. load)
Resource instances.

public interface ResourceLoader {
Resource getResource(String location);
}

引用
When you call getResource() on a specific application context, and the location path specified
doesn't have a specific prefix, you will get back a Resource type that is appropriate to that particular
application context

Resource template = ctx.getResource("some/resource/path/myTemplate.txt");

What would be returned would be a ClassPathResource
if the same method was executed against a
FileSystemXmlApplicationContext instance, you'd get back a FileSystemResource

On the other hand, you may also force ClassPathResource to be used, regardless of the application
context type, by specifying the special classpath: prefix
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");


Similarly, one can force a UrlResource to be used by specifying any of the standard java.net.URL prefixes:
Resource template = ctx.getResource("file:/some/resource/path/myTemplate.txt");

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


4:Resources as dependencies
引用
What makes it trivial to then inject these properties, is that all application contexts register and use a
special JavaBeans PropertyEditor which can convert String paths to Resource objects. So if
myBean has a template property of type Resource, it can be configured with a simple string for that
resource, as follows:

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


If there is a need to force a specific Resource type to be used, then a prefix may be used. The following
two examples show how to force a ClassPathResource and a UrlResource (the latter being used
to access a filesystem file).
<property name="template" value="classpath:some/resource/path/myTemplate.txt">

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


5:Application contexts and Resource paths
When such a location path doesn't have a prefix, the specific Resource type built from that path andused to load the bean definitions, depends on and is appropriate to the specific application context. For example, if you create a ClassPathXmlApplicationContext as follows:
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");

The bean definitions will be loaded from the classpath, as a ClassPathResource will be used

if you create a FileSystemXmlApplicationContext as follows:
ApplicationContext ctx =
new FileSystemXmlApplicationContext("conf/appContext.xml");

The bean definition will be loaded from a filesystem location, in this case relative to the current working
directory

The basic idea is that one supplies merely a string array containing just the filenames of the
XML files themselves (without the leading path information), and one also supplies a Class; the
ClassPathXmlApplicationContext will derive the path information from the supplied class.
com/
foo/
services.xml
daos.xml
MessengerService.class

ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {"services.xml", "daos.xml"}, MessengerService.class);


some Examples
String filePath="D:/TJ_project/spring/conf/test.properties";
Resource res1=new FileSystemResource(filePath);
Resource res2=new ClassPathResource("test.properties");
InputStream ins1 = res1.getInputStream(); 
InputStream ins2 = res2.getInputStream();
System.out.println(res1.getFilename());
System.out.println(res2.getFilename());


Resource resource=new ClassPathResource("hello.xml");   
BeanFactory factory=new XmlBeanFactory(resource);   
T tt=(T)factory.getBean("tt");   
tt.method(); 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics