Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

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:/...

Monday, March 30, 2009

Spring ApplicationContextAware

If you need to get Spring Application Context don't initialize it. This operation could be slow. The better way is to allow Spring to inject its context:


public class MyClass implements ApplicationContextAware{
private ApplicationContext context;

...
public void setApplicationContext(ApplicationContext applicationcontext) throws BeansException {
this.context= applicationcontext;
}

}