BAEL-1628
Access a File from the Classpath in a Spring Application
This commit is contained in:
parent
d1b8f9b9b3
commit
3623391466
@ -0,0 +1,10 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.resource")
|
||||
public class AppConfig {
|
||||
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
@Component
|
||||
public class ClassPathResourceReader {
|
||||
|
||||
@Autowired
|
||||
ResourceLoader resourceLoader;
|
||||
|
||||
@Autowired
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@Value("classpath:data/resource-data.txt")
|
||||
Resource resourceFile;
|
||||
|
||||
/**
|
||||
* Constructs Resource object by making use of its built-in implementations.
|
||||
*
|
||||
* @return Resource
|
||||
*/
|
||||
public Resource constructResourceManually() {
|
||||
Resource resource = new ClassPathResource("data/resource-data.txt");
|
||||
return resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs resource object by making use of ResourceLoader.
|
||||
*
|
||||
* @return Resource
|
||||
*/
|
||||
public Resource retrieveResourceUsingResourceLoader() {
|
||||
Resource resource = resourceLoader.getResource("classpath:data/resource-data.txt");
|
||||
return resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs Resource instance by making use of ApplicationContext.
|
||||
*
|
||||
* @return Resource
|
||||
*/
|
||||
public Resource retrieveResourceUsingApplicationContext() {
|
||||
Resource resource = applicationContext.getResource("classpath:data/resource-data.txt");
|
||||
return resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* ResourceUtils example for getting file data.
|
||||
*
|
||||
* @return
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public File retrieveFileUsingResourceUtils() throws FileNotFoundException {
|
||||
return ResourceUtils.getFile("classpath:data/resource-data.txt");
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to list contents ofa file.
|
||||
*
|
||||
* @param fileResource
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String listResourceContentsUsingFile(File fileResource) throws IOException {
|
||||
FileReader fileReader = new FileReader(fileResource);
|
||||
BufferedReader bufReader = new BufferedReader(fileReader);
|
||||
String dataLine = bufReader.readLine();
|
||||
StringBuilder fileData = new StringBuilder();
|
||||
while (null != dataLine) {
|
||||
fileData.append(dataLine);
|
||||
dataLine = bufReader.readLine();
|
||||
}
|
||||
|
||||
fileReader.close();
|
||||
return fileData.toString();
|
||||
}
|
||||
|
||||
public Resource getSampleFile() {
|
||||
return resourceFile;
|
||||
}
|
||||
|
||||
public void setSampleFile(Resource sampleFile) {
|
||||
this.resourceFile = sampleFile;
|
||||
}
|
||||
}
|
1
spring-core/src/main/resources/data/resource-data.txt
Normal file
1
spring-core/src/main/resources/data/resource-data.txt
Normal file
@ -0,0 +1 @@
|
||||
This is a sample text to demonstrate usage of Spring Resource.
|
@ -0,0 +1,57 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class)
|
||||
public class SpringResourceTest {
|
||||
|
||||
@Autowired
|
||||
private ClassPathResourceReader classPathResourceReader;
|
||||
|
||||
static final String testData = "This is a sample text to demonstrate usage of Spring Resource.";
|
||||
|
||||
@Test
|
||||
public void whenManualInstance_thenReadSuccessful() throws IOException {
|
||||
Resource resource = classPathResourceReader.constructResourceManually();
|
||||
String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile());
|
||||
assertEquals(testData, fileData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenResourceLoader_thenReadSuccessful() throws IOException {
|
||||
Resource resource = classPathResourceReader.retrieveResourceUsingResourceLoader();
|
||||
String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile());
|
||||
assertEquals(testData, fileData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenApplicationContext_thenReadSuccessful() throws IOException {
|
||||
Resource resource = classPathResourceReader.retrieveResourceUsingApplicationContext();
|
||||
String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile());
|
||||
assertEquals(testData, fileData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAutowired_thenReadSuccessful() throws IOException {
|
||||
Resource resource = classPathResourceReader.getSampleFile();
|
||||
String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile());
|
||||
assertEquals(testData, fileData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenResourceUtils_thenReadSuccessful() throws IOException {
|
||||
String fileData = classPathResourceReader.listResourceContentsUsingFile(classPathResourceReader.retrieveFileUsingResourceUtils());
|
||||
assertEquals(testData, fileData);
|
||||
}
|
||||
}
|
1
spring-core/src/test/resources/data/resource-data.txt
Normal file
1
spring-core/src/test/resources/data/resource-data.txt
Normal file
@ -0,0 +1 @@
|
||||
This is a sample text to demonstrate usage of Spring Resource.
|
Loading…
x
Reference in New Issue
Block a user