getResource()の例によるSpringリソースローダー

getResource()を使用したSpring Resourceローダーの例

Springのリソースローダーは、ファイルシステム、クラスパス、またはURLから(テキストファイル、メディアファイル、画像ファイルなど)などのリソースを取得するための非常に一般的なgetResource()メソッドを提供します。 getResource()メソッドはアプリケーションコンテキストから取得できます。

getResource()を使用してテキストファイルをロードする方法を示す例を次に示します。

1. ファイルシステム

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

2. URLパス

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

3. クラスパス

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

リソースの場所を指定するだけで、残りはSpringによって処理され、Resourceオブジェクトが返されます。

getResource()メソッドを使用した完全な例。

package com.example.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/example/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リソースローダー(ResourceLoaderAware)

Beanにはアプリケーションコンテキストアクセスがないため、Beanはどのようにリソースにアクセスできますか? 回避策は、ResourceLoaderAwareインターフェイスを実装し、ResourceLoaderオブジェクトのsetterメソッドを作成することです。 SpringはBeanにリソースローダーを実行します。

package com.example.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構成ファイル



   

それを実行します

package com.example.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.example.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/example/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からリソースを取得できます。

結論

このgetResource()メソッドを使用しない場合、ファイルシステムリソースのFileオブジェクト、URLリソースのURLオブジェクトなど、さまざまなソリューションでさまざまなリソースを処理する必要があります。 Springは、この非常に一般的なgetResource()メソッドで本当に良い仕事をしました。それは、リソースを処理するための時間を本当に節約します。

ソースコードをダウンロード

ダウンロード–Spring-getResource-Example.zip