Springのリソースローダーは、(テキストファイル、メディアファイル、イメージファイルなどの)リソースをファイルシステム、クラスパス、またはURLから取得するための非常に汎用的なgetResource()
メソッドを提供します。アプリケーションコンテキストから
getResource()** メソッドを取得できます。
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/mkyong/common/testing.txt");
リソースの場所を指定するだけで、Springが残りの部分を処理し、Resourceオブジェクトを返します。
`getResource()`メソッドの完全な例。
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リソースローダー(ResourceLoaderAware)
Beanにはアプリケーションコンテキストへのアクセス権がないため、Beanはどのようにリソースにアクセスできますか?この問題を回避するには、
ResourceLoaderAware
インターフェースを実装し、
ResourceLoader
オブジェクトのsetterメソッドを作成します。 SpringはあなたのbeanにリソースローダーをDIします。
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設定ファイル
<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>
それを実行します
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(); } } }
これで、Beanからリソースを取得できます。
結論
このgetResource()メソッドがなければ、ファイルシステムリソースのFileオブジェクト、URLリソースのURLオブジェクトなど、さまざまなソリューションで異なるリソースを処理する必要があります。 Springはこのスーパージェネリック
getResource()
メソッドで本当にうまくやったので、リソースを処理する時間が本当に節約されました。