Home  >  Spring Core

ResourceLoader Spring Example | getResource

By Arvind Rai, August 17, 2013
ResourceLoader interface comes under the package org.springframework.core.io. ResourceLoader is used to loads resources from classpath as well as file system. ResourceLoader has a method as
Resource getResource(String location)
The Resource instance can be used to used print the resource. Every ApplicationContext implements ResourceLoader and we can get Resource by calling getResource on ApplicationContext. getResource argument can be a classpath, file system or a URL like
Resource res =  context.getResource("file:/path/concretepage.txt);
 
Resource res  =  context.getResource("classpath:concretepage.txt);
 



Resource res  =  context.getResource("http://www.concretepage.com/resource/concretepage.txt);
 
Find the example below

SpringDemo.java
package com.concretepage;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class SpringDemo {
    public static void main(String... args) throws IOException {
    	ApplicationContext context = new ClassPathXmlApplicationContext("spring-app.xml");
    	Entitlement ent = (Entitlement)context.getBean("entitlement");
    	System.out.println(ent.getName());
    	//using Resource
    	Resource res  =  context.getResource("classpath:concretepage.txt");
    	System.out.println(res.getFilename());
    	InputStream is =  res.getInputStream();
    	try{
    		int i=0;
            while((i=is.read())!=-1)
            {
               char c=(char)i;
               System.out.print(c);
            }
        }catch(IOException e){
        	 e.printStackTrace();
         }finally{
            
           if(is!=null)
            is.close();
         }
    } 
}
 


concretepage.txt
Hello World!
 


spring-app.xml
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <bean id="entitlement" class="com.concretepage.Entitlement">
	    <constructor-arg name="name" value="Entitlement"/>
	    <constructor-arg name="time" value="20"/>	           
    </bean>
</beans>
 


Entitlement.java
package com.concretepage;
public class Entitlement {
	private String name;
	private int time;
	public Entitlement(String name,int time){
		this.name=name;
		this.time=time;
	} 
	public String getName() {
		return name;
	}	
	public int getTime() {
		return time;
	}
}
 


Output
Testing...
concretepage.txt
Hello World!
 


pom.xml
<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
      <optional>true</optional>
    </dependency>
    
    <dependency>
	  <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
	  <version>2.1_3</version>
	  <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.5.2-Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>3.6.0.Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>2.0.8</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>3.1.0.RELEASE</version>
	  <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp-all</artifactId>
      <version>1.3</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-aop</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    	
    <dependency>
	  <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-expression</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-asm</artifactId>
	  <version>3.1.0.RELEASE</version>
	 <scope>compile</scope>
    </dependency>	
    
    <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <version>5.1.17</version>
    </dependency>
    
    <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
    </dependency>
    
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-agent</artifactId> 
     <version>2.5.6</version> 
     <scope>provided</scope> 
    </dependency> 

   <dependency>
	 <groupId>org.aspectj</groupId>
	 <artifactId>aspectjweaver</artifactId>
	 <version>1.5.4</version>
   </dependency>
  </dependencies>
 
Download Source Code
resourceloader-spring-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us