Create a maven project and add Spring dependencies
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Create a Spring configuration class
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.vinod.test")
public class MySpringConifg {
}
Place one text file in to the class path
vinod.txt in to src/main/resources folder
Test class
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
public class ResourceLoaderTest {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(MySpringConifg.class);
context.refresh();
Resource resource = context.getResource("classpath:vinod.txt");
String theString = IOUtils.toString(resource.getInputStream(), "UTF-8");
System.out.println(theString);
}
}
Run the program and check the console for the output..
In the above program we are getting the Resource from the Spring context it self, if we wanted to get it from our own bean, spring provides ResourceLoaderAware interface , this will helps to inject ResourceLoader and we can use that bean to load the resources
Note: Internally AplicationContext also using the same ResourceLoader to load the resources
Let us see how can we implement this..
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
@Component
public class MyResourceLoader implements ResourceLoaderAware {
private ResourceLoader resourceLoader;
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public Resource getResource(String path) {
System.out.println("loading resources "+path);
return this.getResourceLoader().getResource(path);
}
}
Modified Main class
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
public class ResourceLoaderTest {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(MySpringConifg.class);
context.refresh();
MyResourceLoader resource = context.getBean(MyResourceLoader.class);
Resource file = resource.getResource("classpath:vinod.txt");
String theString = IOUtils.toString(file.getInputStream(), "UTF-8");
System.out.println(theString);
}
}
Output
loading resources classpath:vinod.txt
hai i am vinod
Done!!!