[转]Spring Resource loader with GetResource() example

转载 2013年12月04日 14:45:47

转载地址:http://www.mkyong.com/spring/spring-resource-loader-with-getresource-example/

Spring’s resource loader provides a very generic getResource() method to get the resources like (text file, media file, image file…) from file system , classpath or URL. You can get the getResource() method from the application context.

Here’s an example to show how to use getResource() to load a text file from

1. File system

Resource resource = appContext.getResource("file:c:\\testing.txt");

2. URL path

Resource resource = appContext.getResource("url:http://www.yourdomain.com/testing.txt");

3. Class path

Resource resource = appContext.getResource("classpath:com/mkyong/common/testing.txt");

You just need to specify the resource location, and the Spring will handle the rest and return you a Resource object.

Full example withgetResource() method.

package com.mkyong.common;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext appContext = 
    	   new ClassPathXmlApplicationContext(new String[] {"If-you-have-any.xml"});
 
    	Resource resource = 
           appContext.getResource("classpath:com/mkyong/common/testing.txt");
 
    try{
     	  InputStream is = resource.getInputStream();
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
 
          String line;
          while ((line = br.readLine()) != null) {
             System.out.println(line);
       	  } 
          br.close();
 
    	}catch(IOException e){
    		e.printStackTrace();
    	}
 
    }
}

Bean resource loader (ResourceLoaderAware)

Since bean does not have the application context access, how can a bean access a resources? The workaround is implement the ResourceLoaderAware interface and create setter method for ResourceLoader object. Spring will DI the resource loader into your bean.

package com.mkyong.customer.services;
 
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
 
public class CustomerService implements ResourceLoaderAware
{
	private ResourceLoader resourceLoader;
 
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}
 
	public Resource getResource(String location){
		return resourceLoader.getResource(location);
	}
}

Bean configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
   <bean id="customerService" 
           class="com.mkyong.customer.services.CustomerService" />
 
</beans>

Run it

package com.mkyong.common;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
 
import com.mkyong.customer.services.CustomerService;
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext appContext = 
    	   new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
 
    	CustomerService cust = 
           (CustomerService)appContext.getBean("customerService");
 
    	Resource resource = 
            cust.getResource("classpath:com/mkyong/common/testing.txt");
 
    try{
          InputStream is = resource.getInputStream();
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
 
          String line;
          while ((line = br.readLine()) != null) {
     	       System.out.println(line);
          } 
          br.close();
 
    	}catch(IOException e){
    		e.printStackTrace();
    	}
 
    }
}

Now you can get the resources from a bean.

Conclusion

Without this getResource() method, you will need to deal with different resources with different solution, like File object for file system resource, URL object for URL resource. Spring really did a good job with this super generic getResource()method, it really save our time to deal with resources.

相关文章推荐

Spring JTA multiple resource transactions in Tomcat with Atomikos example

In this tutorial we shall show you how to implement JTA multiple resource transactions in a Tomcat s...

Spring Injection with @Resource, @Autowired and @Inject

详细说明了spring 中依赖注入时的几个annotation: @Resource, @Inject, @Autowired

Spring 整合Hibernate报错:Error creating bean with name 'dataSource' defined in class path resource

Spring 整合Hibernate 时报错如下:org.springframework.beans.factory.BeanCreationException: Error creating bea...

Error creating bean with name 'sessionFactory' defined in class path resource [spring/applicationCon

十月 19, 2013 2:39:56 下午 org.apache.catalina.core.AprLifecycleListener init INFO: The Apache Tomcat Na...

spring异常解决方法Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto...

SPRING INJECTION WITH @RESOURCE, @AUTOWIRED AND @INJECT

Overview I’ve been asked several times to explain the difference between injecting Spring beans w...

Spring Bean Validation Example with JSR-303 Annotations

JSR-303 bean validation is a specification whose objective is to standardize the validation of Java ...

Spring with Hibernate persistence and transactions example

In this tutorial you will learn how to implement Hibernate persistence using the Spring framework in...

Spring3MVC:Internationlization&Localization Tutorial with Example

我们将会创建两个properties文件,他们包含显示的所有信息,这些文件被放在叫做resources的目录中, Right click on Project name > New > Source...

Spring MVC with ParameterizableViewController Tutorial and Example

dispatcher-servlet.xml ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)