![]() | Draft |
|---|---|
This is a first draft of this chapter. It is technically accurate, but not in the style or state of the final revision. |
Spring provides a custom way to load file resources from different locations and also two different mechanisms for creating beans.
A BeanFactory or ApplicationContext can be used to create Spring beans, but typically an
ApplicationContext should be used.
Spring's resource abstraction allows generic handling of resources from file, classpath, URL, HTTP, FTP locations, and web context locations.
Table 5.1. Resource Loaders
| Resource Loader | Description |
|---|---|
UrlResource |
Loads URL resources as the default. Built on top of java.net.URL and appropriately can handle 'http:', 'ftp:', and 'file:'
|
ClassPathResource | Uses classpath as the default to load resources. |
FileSystemResource | Uses file system as the default to load resources. |
ServletContextResource | Uses web context as the default. Ex: '/WEB-INF/applicationContext.xml' |
InputStreamResource | Uses an open InputStream. Other resource types should be used whenever possible since re-reads aren't possible with this type. |
ByteArrayResource | Used to read a byte array. |
Table 5.2. Resource Prefixes
| Resource Prefix | Example | Description | ||||
|---|---|---|---|---|---|---|
| classpath: |
Loads resource from the classpath using the ClassPathResource.
| classpath:/org/springbyexample/bean/config.txt | ||||
| file: |
Loads resource from the file system using the FileSystemResource.
| file:/usr/local/app/config.txt | ||||
| ftp: |
Loads resource from FTP using the UrlResource.
| ftp://ftp.springbyexample.org/config.txt | ||||
| http: |
Loads resource from HTTP using the UrlResource.
| http://www.springbyexample.org/config.txt | ||||
No prefix defaults to the resource loading of the BeanFactory or ApplicationContext.
|
|