IT/Spring Boot
[Spring Boot] 스프링부트 Spring Resource
nohumb
2024. 9. 27. 21:47
SMALL
- java.net.URL의 한계(classpath 내부 접근이나 상대경로 등)를 넘어서기 위해 추가적으로 구현된 부분
- 실무에서 딱히 필수적이진 않음
- 구현체 목록
- UrlResource : “ftp:”, “file:”, “http:” 등의 prefix로 Resource에 접근, 기본적으로는 http(s): 로 접근
- ClassPathResource : 코드를 빌드한 결과가 특정 경로로 저장되는데 이 때 하위 파일들을 접근하기 위해 사용
- FileSystemResource : 로컬의 파일을 경로로 특정해 사용
- SevletContextResource : Spring 밖의 Sevlet 어플리케이션 루트의 리소스에 접근
- InputStreamResource : 키보드, 마우스 등 Input 값 리소스에 접근
- ByteArrayResource : Byte로 들어오는 Stream을 읽는 용도
- Spring ResourceLoader
- 스프링 프로젝트 내의 특정 리소스에 접근할 때 사용, 대부분의 사전 정의된 파일들은 자동으로 로딩이 되나 추가적으로 필요한 파일이 있을 때 이 기능을 사용
- 기본적으로 ApplicationContext에 구현되어 있다
@Service public class ResourceService { @Autowired ApplicationContext ctx; public void setResource() { Resource myTemplate = ctx.getResource("classpath:some/resource/path/myTemplate.txt"); } }
- ResourcePatternResolver
- ApplicationContext에서 ResourceLoader를 불러올 때 사용하는 Interface, 위치 지정자 패턴에 따라 자동으로 Resource Loader 구현체를 선택
- Application Contexts & Resource Paths
- applicationContext(스프링의 핵심 설정)을 이루는 설정 값을 가져오는 방법 들
- 예전엔 xml로 Configuration을 하였으나 요즘은 대부분 Java Config로, Annotation 기반의 설정으로 바뀐다고 함
-
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml"); ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml"); //use ctx as a spring Bear bear = (Bear) ctx.getBean("bear");
LIST