转自:https://www.dev2qa.com/spring-bean-load-external-resources-example/
Spring Bean Load External Resources Example
Jerry Zhao September 19, 2017 0
Spring framework provide methods to load external resource data (text file, image file, binary file) into your application. The external resource can be a local file, a url locator file and the file saved in the ClassPath. This article will show you examples about how to do that.
1. Load external resource by ApplicationContext.
Below example code demonstrate how to use Spring ApplicationContext to load external resource. First you should get the spring ApplicationContext object.
ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("BeanSettings.xml");
Load from local file system
Save file atC:\\WorkSpace\\dev2qa.com\\SpringExternalResourceData.txt.
Load it in java code.
Resource resource = springAppCtx.getResource("file:C:\\WorkSpace\\dev2qa.com\\SpringExternalResourceData.txt");
Load from classpath
Save the file under classpath such ascom/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txtor java project resources file folder.
Load it in java code.
Resource res = springAppCtx.getResource("classpath:com/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txt");
Resource res = springAppCtx.getResource("classpath:SpringExternalResourceData.txt");
Load from url
Upload the file to a web server. And access it with below url.
http://www.dev2qa.com/demo/spring/load_resource/example_data.txt.
Load it in java code.
Resource res = springAppCtx.getResource("url:http://www.dev2qa.com/demo/spring/load_resource/example_data.txt");
ApplicationContextLoadResource.java
public class ApplicationContextLoadResource {
public static void main(String[] args) {
try
{
// Initiate Spring application context, this line code will invoke bean initialize method.
ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("BeanSettings.xml");
// Get from local file.
Resource res = springAppCtx.getResource("file:C:\\WorkSpace\\dev2qa.com\\SpringExternalResourceData.txt");
// Get from a url file.
//Resource res = springAppCtx.getResource("url:http://www.dev2qa.com/demo/spring/load_resource/example_data.txt");
// Get from a file in classpath.
//Resource res = springAppCtx.getResource("classpath:com/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txt");
//Resource res = springAppCtx.getResource("classpath:SpringExternalResourceData.txt");
InputStream inputStream = res.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferReader = new BufferedReader(inputStreamReader);
String line = bufferReader.readLine();
while(line!=null)
{
System.out.println(line);
line = bufferReader.readLine();
}
}catch(IOException ex)
{
ex.printStackTrace();
}
}
}
2. Load external resource in spring bean.
org.springframework.core.io.ResourceLoader can be used in spring bean to load external resources.
To use it in your spring bean class, you should make your class implements org.springframework.context.ResourceLoaderAwareinterface.
Override it’ssetResourceLoader(ResourceLoader resourceLoader)method, spring framework will use this method to inject a ResourceLoader object into your bean class automatically.
Assign the spring framework injected ResourceLoader object to a bean local property.
Use bean local ResourceLoader object to load external resources.
public class SpringBeanLoadResource implements ResourceLoaderAware {
// bean property.
private ResourceLoader resourceLoader;
// This method will be used by Spring framework to inject resouceLoader instance into this bean class.
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
// Assign the framework injected resourceLoader to a local variable for later use.
this.resourceLoader = resourceLoader;
}
// Use resourceloader to get Resource object by locator.
public Resource getExternalResource(String resourceLocator)
{
return this.resourceLoader.getResource(resourceLocator);
}
public static void main(String[] args) {
try
{
// Initiate Spring application context, this line code will invoke bean initialize method.
ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("BeanSettings.xml");
SpringBeanLoadResource springBeanLoadResource = (SpringBeanLoadResource)springAppCtx.getBean("springBeanLoadResource");
// Get from local file.
//Resource res = springBeanLoadResource.getExternalResource("file:C:\\WorkSpace\\dev2qa.com\\SpringExternalResourceData.txt");
// Get from a url file.
//Resource res = springBeanLoadResource.getExternalResource("url:http://www.dev2qa.com/demo/spring/load_resource/example_data.txt");
// Get from a file in classpath.
//Resource res = springBeanLoadResource.getExternalResource("classpath:com/dev2qa/example/spring/bean/loadresource/SpringExternalResourceData.txt");
Resource res = springBeanLoadResource.getExternalResource("classpath:SpringExternalResourceData.txt");
InputStream inputStream = res.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferReader = new BufferedReader(inputStreamReader);
String line = bufferReader.readLine();
while(line!=null)
{
System.out.println(line);
line = bufferReader.readLine();
}
}catch(IOException ex)
{
ex.printStackTrace();
}
}
}
网友评论