Spring 3.0 inject files as resources
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 ApplicationContext
s are, by definition, ResourceLoader
s. 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();
}
Randomize
Just a random developer that does NOT believe in software development and people
Updated on July 12, 2020Comments
-
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 anInputStream
(or some other type). How can I retrieve them? Is it possible to inject them as a normalResource
? -
Sumanth Ravipati over 12 yearsThe
Resource
interface does not declare agetInputStream()
method, you would need to callnew FileInputStream(resource.getFile())
. -
Sumanth Ravipati over 12 yearsI fail to see how any of the answers to that question result in reduced dependency on Spring...
-
Saket over 12 yearsThat's incorrect.
Resource
inherits it from the interfaceorg.springframework.core.io.InputStreamSource
-
Sumanth Ravipati over 12 yearsMy bad, sorry, didn't see the
implements
. -
Babu Subburathinam over 12 yearsI agree that using @Value is not zero spring dependency.
-
Babu Subburathinam over 12 yearsBut 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 almost 11 yearsThe dependency nit might not be true, but this is a valid and very simple solution.
-
Menelaos Kotsollaris almost 7 yearsAny idea on how can a directory be loaded? E.g, a directory "dir" which contains 25 files
-
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 about 3 yearsWhy the DataInputStream in the stack?
-
Donal Fellows almost 3 yearsThis really should be using Java 8+
try
-with-resources.