Wednesday, April 1, 2009

Resources and Spring

Spring provides an easy way to store resources. For instance you need to use an xslt transformation file in your class.

1. Add resource member in your class
 
public class A {
...
private org.springframework.core.io.Resource myResource;
public void setMyResource(org.springframework.core.io.Resource myResource) {
this.myResource = myResource;
}
...
// You Code
// Now you can get the file/stream from the resource:
// myResource.getFile();
// myResource.getInputStream();
...
}


2. Define the resource in spring configuration

<bean id="myBean" class="A">
<property name="myResource">
<value type="org.springframework.core.io.Resource">{path}</value>
</property>
</bean>



The {path} could be:
- /WEB-INF/...
- classpath:/...
- file:/c:/...

No comments:

Post a Comment