MemeStorm

Exploring the Spring Framework and Application Development

Loading resources from files or the classpath

Posted in All by Jon on the February 19th, 2006

You want to load resources from the file system or classpath from
within beans initialized by a Spring context.

Background

Our HSQLDB utility class needs to load the SQL to create the database every time our application starts, as outlined in a Embedded Database Starting Up With Web Context. How does this bean get access to the file? Well, in the context configuration we supplied a file location, for example:

<bean id="databaseUtils" class="com.memestorm.utils.db.HSQLDB">
  <property name="location“>
    <value>/db/init.sql</value>
  </property>
</bean>

So, how can we actually get to the file?

How to do it

The easiest way is to have your bean implement the ResourceLoaderAware interface. This will then ensure that the setResourceLoader(ResourceLoader resourceLoader) method will get called on your bean, with a magic resource loader. (Look here for more information on the aware interfaces.)

The ResourceLoader
class is a fantastic utility class provided by Spring, that allows you
to load resources from all sorts of locations. Given the location above, the resource loader will look for the file db/init.sql relative to the root of the web application (when used from a web context). Our bean simply uses the location as follows:

Resource sql = this.resourceLoader.getResource(this.location);
JdbcTemplate jt = new JdbcTemplate(this.dataSource);
jt.execute(IOUtils.toString(sql.getInputStream()));

It’s so simply it belies how easy this makes life! To load from an arbitrary file location, use the file: prefix instead. For example, file:///temp/foobar.sql. You can also try and locate something in the classpath using the classpath prefix. For example, classpath:config.xml.

Have fun.


Print This Post Print This Post

5 Responses to 'Loading resources from files or the classpath'

Subscribe to comments with RSS or TrackBack to 'Loading resources from files or the classpath'.

  1. J Wilks said,

    on April 5th, 2006 at 7:39 am

    I was surprised how hard it was to get XmlWebApplicationContext to use a resource root *other than* the root of the webapp. For security reasons you usually want your configuration files under /WEB-INF/, not the actual webapp root. But if you place them there, your app context XML file has to reference “/WEB-INF/db/init.sql” — which means that configuration file is no longer useful outside of a web container context.

  2. A Korver said,

    on May 5th, 2006 at 9:16 am

    From the Spring docs…..Thus you can eliminate the ResourceAware interface and the String file location, instead, just use a Resource property and set it.

    “If the bean itself is going to determine and supply the resource path through some sort of dynamic process it probably makes sense for the bean to use the ResourceLoader interface to load resources. Consider as an example the loading of a template of some sort, where the specific one needed that depends on the role of the user. If on the other hand the resources are static, it makes sense to eliminate the use of the ResourceLoader interface completely, and just have the bean expose the Resource properties it needs, and expect that they will be injected into it.

    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 text string for that resource, as follows:

    http://static.springframework.org/spring/docs/1.2.x/reference/resources.html

  3. Gaurav Sood said,

    on August 2nd, 2006 at 8:43 am

    I’m stuck with the same problem, having to load a resource which is not in /WEB-INF/… Apparently my webapps work fine but the integration test is unable to get the resource from the classpath. Can anyone help

  4. Bill Poitras said,

    on August 17th, 2006 at 9:33 am

    Instead of using a property of type String, try using File or InputStream. Spring resource editors will automatically do the conversion for you (although you need to close InputStream). For classpath resources begin the string with “classpath:”

  5. Jamie Cramb said,

    on November 3rd, 2008 at 11:10 am

    Bump!

    Thanks, been trying to find a way to do this for a while now; works like a charm :).

    Cheers

Leave a Reply