Spring 3.0 inject files as resources

44,973

Solution 1

Here is an easiest way to do it via annotation:

import org.springframework.core.io.Resource;

@Value("classpath:<path to file>")
private Resource cert;

Solution 2

All ApplicationContexts are, by definition, ResourceLoaders. This means that they are capable of resolving any resource strings found within their configuration. With this in mind, you can declare your target bean with a setter that accepts an org.springframework.core.io.Resource. Then when you configure the target bean, just use a resource path in the value for the property. Spring will attempt to convert the String value in your configuration into a Resource.

public class Target {
  private Resource resource;
  public void setResource(final Resource resource) {
    this.resource = resource;
  }
}

//configuration
<beans>
  <bean id="target" class="Target">
    <property name="resource" value="classpath:path/to/file"/>
  </bean>
</beans>

Solution 3

You should be able to use :

Resource resource = appContext.getResource("classpath:<your resource name>");
InputStream is = resource.getInputStream();

where appContext is your Spring ApplicationContext (specifically, a WebApplicationContext, since you have a webapp)

Solution 4

Here's a full example to retrieve a classpath resource. I use it to grab SQL files that have really complex queries which I don't want to store in Java classes:

public String getSqlFileContents(String fileName) {
    StringBuffer sb = new StringBuffer();
    try {
        Resource resource = new ClassPathResource(fileName);
        DataInputStream in = new DataInputStream(resource.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null) {
            sb.append(" " + strLine);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
Share:
44,973
Randomize
Author by

Randomize

Just a random developer that does NOT believe in software development and people

Updated on July 12, 2020

Comments

  • Randomize
    Randomize almost 4 years

    In my Spring 3.0 app, I have some resources in /WEB-INF/dir. At runtime I need some of them as an InputStream (or some other type). How can I retrieve them? Is it possible to inject them as a normal Resource?

  • Sumanth Ravipati
    Sumanth Ravipati over 12 years
    The Resource interface does not declare a getInputStream() method, you would need to call new FileInputStream(resource.getFile()).
  • Sumanth Ravipati
    Sumanth Ravipati over 12 years
    I fail to see how any of the answers to that question result in reduced dependency on Spring...
  • Saket
    Saket over 12 years
    That's incorrect. Resource inherits it from the interface org.springframework.core.io.InputStreamSource
  • Sumanth Ravipati
    Sumanth Ravipati over 12 years
    My bad, sorry, didn't see the implements.
  • Babu Subburathinam
    Babu Subburathinam over 12 years
    I agree that using @Value is not zero spring dependency.
  • Babu Subburathinam
    Babu Subburathinam over 12 years
    But annotations is the best way to achieve spring injection without violating do-not-repeat-yourself principle. The XML approach, that you recommend, forces you to repeat yourself. Sooner or later somebody is going to change the property name in the java file and forget to update the spring bean definition file.
  • Karl the Pagan
    Karl the Pagan almost 11 years
    The dependency nit might not be true, but this is a valid and very simple solution.
  • Menelaos Kotsollaris
    Menelaos Kotsollaris almost 7 years
    Any idea on how can a directory be loaded? E.g, a directory "dir" which contains 25 files
  • yasgur99
    yasgur99 almost 5 years
    @Andrei_N, I believe I have this issue of not working in a jar file. If i run as spring boot application it works fine, but if I deploy a jar to aws beanstalk/ec2 i get exception. do you have reccomendation on how to handle in a jar
  • Paŭlo Ebermann
    Paŭlo Ebermann about 3 years
    Why the DataInputStream in the stack?
  • Donal Fellows
    Donal Fellows almost 3 years
    This really should be using Java 8+ try-with-resources.