Monday, May 28, 2012

Resource Injection Using Spring

Many times we would come across reading a file into application. File can be located anywhere in the system or network.
To handle such file accessing tasks, Spring provides an interface called as “Resource” contained in the package org.springframework.core.io. To stream in the file, this interface has a method called as getInputStream(). we can get input stream from FileSystemResource, URLResource and ClassPathResource.


Spring’s resource loader provides a very generic getResource() method to get the resources like (text file, media file, image file…) from file system , classpath or URL. You can get the getResource() method from the application context.

Without this getResource() method, you will need to deal with different resources with different solution, like File object for file system resource, URL object for URL resource. Spring really did a good job with this super generic getResource() method, it really save our time to deal with resources.

Here’s an example to show how to use getResource() to load a text file from


1) The following code will read a file named sample.txt located in the src folder in the current working directory using class FileSystemResource.

2) For the files which you usually access using the url protocol like http://, ftp://, file:// etc, we have UrlResource. The following code will read a file named sample.txt present in C drive and that can be accessed from your browser using file:// protocol.


3) For the resources that are present on your classpath, you can use ClassPathResource. The following code will read in a file named sample.txt present in the classpath.


Injecting Resources
  Resource Injection(Static resources): Here we inject resources like the usual Spring Injection. This way is used for loading static resources i.e. when we know the resource which is required beforehand.

 Resource Loader(Dynamic Resources): Here we use Resource Loader interface to help us a resource we require. This way is used for loadind dynamic resources i.e which will be loaded based on some business logic .


Resource Injection: for static resources


In the above class, we have created a property of type Resource. And in the below context xml file we have injected this property with the path of the file we want spring to provide us with.

resourceContext.xml

SpringResourceTest.java


In the above test class, we get this bean in the usual way and when you get the textFile property from this bean, you can successfully read the information from this resource object. Now let us see the second way i.e how to access dynamic resources.


resourceContext.xml resource path has no prefix, so because the application context itself is going to be used as the ResourceLoader, the resource itself will be loaded via a ClassPathResource, FileSystemResource, or ServletContextResource (as appropriate) depending on the exact type of the context.
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"/>


Resource Loader Injection: for dynamic resources



The above class has a property of type ResourceLoader. This ResourceLoader will be used to dynamically load a resource.To tell the spring about injecting value into this, we have used the @Autowired annotation. With annotations, we need not create setter methods for our properties. Only thing we have to do is to tell Spring framework that we are using the annotation, so in addition to the injecting values based on the property tags from the xml, it has to read class level annotations also. We do this by adding in the content xml file as seen below.

resourceContext.xml


SpringResourceTest.java
In our test class, we get this bean in the usual way and call its loadDynamicResource. This method internally uses the ResourceLoader to return a Resource dynamically.