org.springframework.core.io.Resource
특징
- java.net.URL을 추상화 한 것
- 스프링 내부에서 많이 사용하는 인터페이스
추상화 한 이유
- 클래스패스 기준으로 리소스 읽어오는 기능 부재
- ServletContext를 기준으로 상대경로로 읽어오는 기능 부재
- 새로운 핸들러를 등록하여 특별한 URL 접미사를 만들어 사용할 수는 있지만 구현이 복잡하고 편의성 메소드가 부족하다.
인터페이스 둘러보기
- 상속받은 인터페이스
- 주요 메소드
- getInputStream()
- exitst()
- isOpen()
- getDescription()
구현체
- UrlResource
- ClassPathResource
- ServeletContextResource
리소스 읽어오기
- Resource의 타입은 location문자열과 ApplicationContext의 타입에 따라 결정 된다.
- ClassPathXmlApplicationContext -> ClassPathResource
- FileSystemXmlApplicationContext -> FileSystemResource
- WebApplicationContext -> ServletContextResource
- ApplicationContext의 타입에 상관없이 리소스 타입을 강제하려면 java.net.URL
접두어(+ classpath:)중 하나를 사용할 수 있다.
- classpath:me/whiteship/config.xml -> ClassPathResource
- file:///some/resource/path/config.xml -> FileSystemResource
백기선님은 접두어를 사용하는 것을 추천