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;

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

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

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);
}
}
}
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;

@Configuration
public class InjectResourceDataExample {

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

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


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

private static class ClientBean {
@Autowired
private String myData;

public void doSomething() {
System.out.println(myData);
}
}
}
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;

@Configuration
public class InjectFileResourceDataExample {

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

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

public static void main(String[] args) throws IOException {
System.out.println(new File("").getAbsolutePath());
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
InjectFileResourceDataExample.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}


private static class ClientBean {
@Autowired
private String myResourceData;

public void doSomething() throws IOException {
System.out.println(myResourceData);
}
}
}
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 {
@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();
}

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

private static class ClientBean {
@Autowired
private String myData;

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




See Also