Resources API in Spring – Part 1

At times we may need to read external resources (e.g., text files, XML files, properties file, or image files) into our application. These resources can be located different locations (e.g., a file system, classpath, or URL). Usually, you have to deal with different APIs for loading resources from different locations. To handle such file accessing tasks, Spring provides an interface called as Resource contained in the package org.springframework.core.io. In this post I shall cover the details of Resource interface, loading of Resource using getResource() method, using Resource loader and through setter methods.

Injecting Resources

Now let’s say you want to have a reference of the resource which you may use in your application; this can be achieved in two ways:

Resource Injection: By using setter method.

Using ResourceLoader: Here we use ResourceLoader interface to help us get an instance of the resource we require. Such technique is used for loading dynamic resources i.e which will be loaded based on some business logic.

Example for Resource Injection

Setup the project as in the example above: Contents of ProjectService.java are:

In the above test class, we get the bean in the usual way and when you get the externalResource property from this bean, you can successfully read the information from this resource object. Contents of Beans.xml are:

The above class has a property type ResourceLoader. ResourceLoader is used to dynamically load the 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 Beans.xml file.

Summary

In this post I discussed about the external resource injection in Spring applications. This is part-1 of the series, in the next post I shall cover another feature ResourceLoaderAware interface, Resources as dependencies and Application contexts and Resource paths. Please refer here for the official documentation on resource API. If you are interested in receiving the future articles, please subscribe here. follow us on @twitter and @facebook

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Trackbacks

[…] the previous post I discussed about the external resource loading in Spring using Resource Loader interface, and setter methods. In this post I shall discuss about another form of resource loading through […]

About Us

Javabeat.net is a blog dedicated to Java/J2EE developers. This site focus on beginners and advanced developers. We constantly write fresh content on J2EE topics like Spring, Hibernate and wide variety of J2EE frameworks.