* BAEL-2434

* BAEL-2434

* Minor alterations to reflect changes in the article

* Fixing Spel expression and removing extra code that is not referenced anymore in the article

* Moved the files from spring-core to spring-static-resources. Renamed ResourceUtil to ResourceReader.

* Changes due to change of article outline.

* Changes due to change of article outline.

* Removing Converter from article
This commit is contained in:
cscib 2019-06-18 22:35:04 +02:00 committed by ashleyfrieze
parent b2df1257c8
commit 548890541c
5 changed files with 113 additions and 0 deletions

View File

@ -140,6 +140,13 @@
<artifactId>handlebars</artifactId>
<version>${handlebars.version}</version>
</dependency>
<!-- commons.io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
@ -208,6 +215,9 @@
<!-- Maven plugins -->
<yuicompressor-maven-plugin.version>1.5.1</yuicompressor-maven-plugin.version>
<!-- commons.io -->
<commons.io.version>2.5</commons.io.version>
</properties>
</project>

View File

@ -0,0 +1,15 @@
package com.baeldung.loadresourceasstring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LoadResourceConfig {
@Bean
public String resourceString() {
return ResourceReader.readFileToString("resource.txt");
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.loadresourceasstring;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import static java.nio.charset.StandardCharsets.UTF_8;
public class ResourceReader {
public static String readFileToString(String path) {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource(path);
return asString(resource);
}
public static String asString(Resource resource) {
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

View File

@ -0,0 +1 @@
This is a resource text file. This file will be loaded as a resource and use its contents as a string.

View File

@ -0,0 +1,57 @@
package com.baeldung.loadresourceasstring;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.util.FileCopyUtils;
import java.io.InputStreamReader;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = LoadResourceConfig.class)
public class LoadResourceAsStringIntegrationTest {
private static final String EXPECTED_RESOURCE_VALUE = "This is a resource text file. This file will be loaded as a " + "resource and use its contents as a string.";
@Value("#{T(com.baeldung.loadresourceasstring.ResourceReader).readFileToString('classpath:resource.txt')}")
private String resourceStringUsingSpel;
@Autowired
@Qualifier("resourceString")
private String resourceString;
@Autowired
private ResourceLoader resourceLoader;
@Test
public void givenUsingResourceLoadAndFileCopyUtils_whenConvertingAResourceToAString_thenCorrect() {
Resource resource = resourceLoader.getResource("classpath:resource.txt");
assertEquals(EXPECTED_RESOURCE_VALUE, ResourceReader.asString(resource));
}
@Test
public void givenUsingResourceStringBean_whenConvertingAResourceToAString_thenCorrect() {
assertEquals(EXPECTED_RESOURCE_VALUE, resourceString);
}
@Test
public void givenUsingSpel_whenConvertingAResourceToAString_thenCorrect() {
assertEquals(EXPECTED_RESOURCE_VALUE, resourceStringUsingSpel);
}
}