Close

Spring Framework - org.springframework.core.io.Resource Examples

Spring Framework 

This example injects classpath Resource using @Value annotation into our Bean.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class InjectResourceExample {

public static void main (String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
;
}

@Configuration
public static class Config {

@Bean
public ClientBean clientBean () {
return new ClientBean();
}

}

private static class ClientBean {
@Value("classpath:myResource.txt")
private Resource myResource;

public void doSomething () throws IOException {
File file = myResource.getFile();
String s = new String(Files.readAllBytes(file.toPath()));
System.out.println(s);
}
}
}

Output

this is a test resource.
Original Post




This example injects a classpath 'Resource' using @Value annotation into the method parameter of a @Configuration class.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class InjectResourceDataExample {

public static void main (String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
public static class Config {

@Bean
public ClientBean clientBean () {
return new ClientBean();
}

//probably we want to use some @Qualifier here
@Bean
public String myResourceData (
@Value("classpath:myResource.txt") Resource myResource) throws
IOException {
File file = myResource.getFile();
return new String(Files.readAllBytes(file.toPath()));
}
}

private static class ClientBean {
@Autowired
private String myData;

public void doSomething () throws IOException {
System.out.println(myData);
}
}
}

Output

this is a test resource.
Original Post




This example injects file system 'Resource' using @Value annotation.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class InjectFileResourceDataExample {

public static void main (String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
public static class Config {

@Bean
public ClientBean clientBean () {
return new ClientBean();
}

@Bean
public String myResourceData (
@Value("file:D:\\test\\test.txt") Resource myResource) throws
IOException {
File file = myResource.getFile();
return new String(Files.readAllBytes(file.toPath()));
}
}

private static class ClientBean {
@Autowired
private String myData;

public void doSomething () throws IOException {
System.out.println(myData);
}
}
}

Output

This is a test file content
Original Post




This example injects an url 'Resource' using @Value annotation.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InjectUrlResourceDataExample {

public static void main (String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
public static class Config {
@Bean
public ClientBean clientBean () {
return new ClientBean();
}

@Bean
public String myResourceData (
@Value("url:http://www.example.com/") Resource resource) throws
IOException {

StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(resource.getInputStream()))) {

reader.lines()
.forEach(stringBuilder::append);
}
return stringBuilder.toString();
}
}

private static class ClientBean {
@Autowired
private String myData;

public void doSomething () throws IOException {
System.out.println(myData);
}
}
}

Output

<!doctype html><html><head>    <title>Example Domain</title>    <meta charset="utf-8" />    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />    <meta name="viewport" content="width=device-width, initial-scale=1" />    <style type="text/css">    body {        background-color: #f0f0f2;        margin: 0;        padding: 0;        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;            }    div {        width: 600px;        margin: 5em auto;        padding: 50px;        background-color: #fff;        border-radius: 1em;    }    a:link, a:visited {        color: #38488f;        text-decoration: none;    }    @media (max-width: 700px) {        body {            background-color: #fff;        }        div {            width: auto;            margin: 0 auto;            border-radius: 0;            padding: 1em;        }    }    </style>    </head><body><div>    <h1>Example Domain</h1>    <p>This domain is established to be used for illustrative examples in documents. You may use this    domain in examples without prior coordination or asking for permission.</p>    <p><a href="http://www.iana.org/domains/example">More information...</a></p></div></body></html>
Original Post




See Also