마케팅스프린트
[ GitHub ]

[ jungyijun@naver.com ]


  • Category
    • 마케팅 자동화
    • old posting
      • ChatGPT
      • Java
      • MySQL
      • JSP
      • Spring Framework
      • Node.js
Total
Today
Yesterday
hELLO · Designed By 정상우.
old posting/Spring Framework

[Spring] ResourceLoader 와 Resource

2021. 6. 21. 10:43
  • ResourceLoader
    • 리소스를 읽어오는 기능을 제공하는 인터페이스
    • ApplicationContext extends ResourceLoader
  • 리소스 읽어오기
    • 파일 시스템에서 읽어오기
    • 클래스패스에서 읽어오기
    • URL로 읽어오기
    • 상대, 절대 경로로 읽어오기
  • Resource 추상화
    • java.net.URL을 추상화 한 것
    • 스프링 내부에서 많이 사용하는 인터페이스
  • 추상화를 한 이유
    • 클래스패스 기준으로 리소스를 읽어오는 기능의 부재
    • ServletContext를 기준으로 상대 경로로 읽어오는 기능의 부재
    • 새로운 핸들러를 등록하여 특별한 URL 접미사를 만들어 사용할 수는 있지만 구현이 복잡하고 편의성 메소드가 부족하다.
  • 주요 메소드
    • getInputStream()
    • exitst()
    • isOpen()
    • getDescription()
      • 전체 경로를 포함한 파일 이름 또는 실제 URL
  • 구현체
    • UrlResouce
      • java.net.URL 참고
        • 기본으로 지원하는 프로토콜 http, https, ftp, file, jar
    • ClassPathResource
      • 지원하는 접두어
        • classpath:
    • FIleSystemResource
    • ServletContextResource
      • 웹 어플리케이션 루트에서 상대 경로로 리소스를 찾는다.
  • 리소스 읽어오기
    • Resource의 타입은 location 문자열과 ApplicationContext의 타입에 따라 결정된다.
      • ClassPathXmlAppliationContext → ClassPathResource
      • FileSystemXmlApplicationContext → FileSystemResource
      • WebApplicationContext → ServletContextResource
    • ApplicationContext의 타입에 상관없이 리소스 타입을 강제로 하려면 java.net.URL 접두어(+ classpath:)중 하나를 사용할 수 있다.
      • classpath:me/test/config.xml → ClassPathResource
      • file://some//resource/path/config.xml → FileSystemResource
저작자표시 (새창열림)
'old posting/Spring Framework' 카테고리의 다른 글
  • [Spring] 데이터바인딩과 PropertyEditor
  • [Spring] Validation
  • [Spring] AppliacationEventPublisher
  • [Spring] MessageSource
마케팅스프린트
마케팅스프린트
쿠팡, 스마트스토어 자동화 솔루션

티스토리툴바