Resource(외부 자원 가져오기)

박근수·2024년 2월 24일
0

Spring

목록 보기
3/11

Resoruce

우리 시스템은 다양한 자원이 필요. 외부 sftp, http, 파일 등에서 자원들을 끌어올 수 있음

Resource Interface와 구현체

public interface Resource extends InputStreamSoruce{

	boolean exists();
    
    boolean isReadable():
    
    boolean isOpen();
    
    boolean isFile();
    
    URL getURL() throws IOExeption;
    
    URL getURI() throws IOExeption;
    
    ReadableByreChannel readableChannel() throws IOException;
    
    long contenLength() throws IOException;
    
    long lastModified() throws IOException;
    
    Resource createRelative(Sting relativePath) throws IOException;
    
    String getFilename();
    
    String getDescrption();
  
}

자바의 표쥰 클래스들을 다양한 리소스(URL, 파일 등)에 접근 할 때 충분한 기능을 제공하지 않음, 스프링은 필요한 기능을 만들어서 제공

URLResource

file : http: 이런식의 Prefix로 프로토콜을 명시해주고 해당 리소스의 위치를 알려주는 URL방식을 통해서 리소스의 위치를 알려주는 방식

Resource resource = new UrlResource("file : {절대경로}");

ClassPathResource

Class Loader를 통해서 ClassPath에서 리소스를 찾는 방식

ClassPathResource resource = new ClassPathResource("dao.xml);

FileSystemResource

절대경로라던지 파일시스템에서 리소스를 찾는 방식

Resource resource = new FileSystemResource({절대경로});

SevletContextResource, InputStreamResource, ByteArrayResource

servlet 어플리케이션 루트 하위 파일, inputStream, ByteArrayInput 스트림을 가져오기 위한 구현체

Spring ResourceLoader

스프링 프로젝트 내 Resource에 접근할 때 사용하는 기능

public interface ResourceLoader{
	Resource getResource(String location);
    
    ClassLoader getClassLoader();
}
  • 기본적으로 applicationContext에서 구현이 되어 있음
  • 프로젝트 내 파일(주로 classpath 하위 파일)에 접근할 일이 있을 경우 활용
  • 대부분의 사전정의된 파일들은 자동으로 로딩되도록 되어 있으나, 추가로 필요한 파일이 있을 때 이 부분 활용 가능
@Service
public class ResourceService{
	@Autowired
    ApplicationContext ctx;
    
    public void setResource() {
    	Resoource myTemplate =
        	ctx.getResource("classpath:some/resource/path/myTemplate.txt");
    }
}

ResourcePatternResolver

스프링 ApplicaitonContext에서 ResourceLoader를 불러올 때 사용하는 interface 위치 지정자 패턴 ("classpath:***", http:") 에 따라 자동으로 Resource로더 구현체를 선택

public interface ApplicationContext extends EnvironmentCapable, 
ListableBeanFactory, HierarchicaBeanFactory, MessageSource, 
ApplicationEventPublisher, ResourcePatternResolver{
	//Spring ApplicationContext interface
}
profile
개발블로그

0개의 댓글