Spring Resource


Spring Resource


Spring에서 리소스 접근에 대해서 알아보자. 여기서 말하는 Resource란 FileSystemResource, UriResource, ClassPathResource, InputStreamResource등 다양한 리소스를 의미한다. Spring에서는 기본적으로 ResourceLoader라는 인터페이스를 이용하여 이들 Resource를 쉽게 생성하고 관리하는 것이 가능하도록 해준다. ResourceLoader를 직접 구현하는것도 가능하지만 기본 구현체는 org.springframework.core.io.DefaultResourceLoader 클래스를 제공하니 이를 사용하여도 무방하다. 또한 ApplicationContext가 이미 ResourceLoader 인터페이스를 구현하고 있기 때문에 보통 대부분의 케이스에서 DefaultResourceLoader를 사용하지 않고 ApplicationContext를 사용하는 것을  권장한다.


Resource 구현클래스

  • org.springframework.core.io.FileSysteResource : 파일시스템의 특정 파일로 부터 정보를 읽어온다.
  • org.springframework.core.io.InputStreamResource : InputStream으로부터 정보를 읽어온다.
  • org.springframework.core.io.ClassPathResource : 클래스패스에 있는 자원으로 부터 정보를 읽어온다.
  • org.springframework.core.io.UrlResource : 특정 URL로부터 정보를 읽어온다.




ResourceLoader 구현클래스

  • org.springframework.core.io.DefaultResourceLoader

  • org.springframework.core.io.DefaultResourceLoader 를 상속하는 AbstractApplicationContext

  • AbstractApplicationContex를 상속하는 모든 ApplicationContext 클래스 

ApplicationContext에서 DefalutResourceLoader에 모든 기능이 사용가능 하기 때문에 사용자가 직접 DefaultResourceLoader를 사용할 필요는 없다.

 




Example

 

[MainApplication Class]

별다른 설정이나 xml파일 조작 없이 ApplicationContext을 통해서 바로 Resource를 얻어 올 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.IOException;
 
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.Resource;
 
public class MainApplication {
 
    public static void main(String[] args) throws IOException {
        // ApplicationContext 부트스트랩
        GenericXmlApplicationContext appCtx = new GenericXmlApplicationContext();
        appCtx.load("classpath:META-INF/spring/app-context.xml");
        appCtx.registerShutdownHook();
        appCtx.refresh();
        
        Resource res1 = appCtx.getResource("file:///c:/temp/text.txt");
        pringResource(res1);
        
        Resource res2 = appCtx.getResource("classpath:test.txt");
        pringResource(res2);
        
        Resource res3 = appCtx.getResource("http://javaking.tistory.com");
        pringResource(res3);
                
    }
    
    public static void pringResource(Resource res) throws IOException {
        System.out.println(res.getClass());
        System.out.println(res.getURL().getContent());
        System.out.println("");
    }
    
}
cs

 


수행결과

class org.springframework.core.io.UrlResource

sun.net.www.content.text.PlainTextInputStream@11cab1c


class org.springframework.core.io.ClassPathResource

sun.net.www.content.text.PlainTextInputStream@125f027


class org.springframework.core.io.UrlResource

sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@118efc0



주의사항

수행결과를 보면 res1은 file리소스 인데 UrlResource가 리턴 되었다.

Spring에서는 기본 리소스 로딩 전략상 URL(http:)과 파일(file:)을 동일 타입의 리소스로 처리하기 때문

FileSystemResource 객체가 필요한 경우 FileSystemResourceLoader을 별도 사용




다운로드


SpringResource.zip





'프로그래밍 > Spring FWK' 카테고리의 다른 글

Spring ApplicationEvent  (0) 2016.01.10
Spring MessageSource 국제화  (0) 2016.01.08
Spring CustomPropertyEditor  (2) 2016.01.05
Spring PropertyEditor  (0) 2016.01.05
Spring facoty-bean, factory-method  (0) 2015.12.30
Spring FactoryBean Interface  (0) 2015.12.29
Spring ApplicationContextAware Interface  (0) 2015.12.28
Spring BeanNameAware Interface  (1) 2015.12.28

이 글을 공유하기

댓글

Email by JB FACTORY