Searching...
Friday, 28 February 2014

Spring ResourceLoader/ResourceloaderAware Example

09:30
Spring org.springframework.core.io.Resource interface is used to load resources from the class path. Here is one simple example to load a text file from the class path.
 

Create a maven project and add Spring dependencies

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
 

Create a Spring configuration class

package com.vinod.test;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.vinod.test")
public class MySpringConifg {
   
}
 

Place one text file in to the class path

vinod.txt in to src/main/resources folder

Test class

package com.vinod.test;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;

public class ResourceLoaderTest {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MySpringConifg.class);
        context.refresh();
        Resource resource = context.getResource("classpath:vinod.txt");
        String theString = IOUtils.toString(resource.getInputStream(), "UTF-8");
        System.out.println(theString);
   
    }

}
 

Run the program and check the console for the output..

In the above program we are getting the Resource from the Spring context it self, if we wanted to get it from our own bean, spring provides ResourceLoaderAware interface , this will helps to inject ResourceLoader and we can use that bean to load the resources 

Note: Internally AplicationContext also using the same ResourceLoader to load the resources

Let us see how can we implement this..

package com.vinod.test;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
@Component
public class MyResourceLoader implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    public ResourceLoader getResourceLoader() {
        return resourceLoader;
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public Resource getResource(String path) {
        System.out.println("loading resources "+path);
        return this.getResourceLoader().getResource(path);
    }

}
 

Modified Main class

package com.vinod.test;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;

public class ResourceLoaderTest {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MySpringConifg.class);
        context.refresh();
        MyResourceLoader resource = context.getBean(MyResourceLoader.class);
        Resource file = resource.getResource("classpath:vinod.txt");
        String theString = IOUtils.toString(file.getInputStream(), "UTF-8");
        System.out.println(theString);

    }

}
 

Output

loading resources classpath:vinod.txt

hai i am vinod

Done!!!

0 comments: