Goal
- ApplicationContext의 Resource Loader에 대해 알아본다
ResourceLoader란?
리소스를 읽어오는 기능을 제공하는 인터페이스
public interface ApplicationContext extends ResourcePatternResolver
public interface ResourcePatternResolver extends ResourceLoader
- getResource(location);
ResourceLoader의 기능
- 파일 시스템에서 읽어오기
- 클래스패스에서 읽어오기
- URL로 읽어오기
- 상대/절대 경로로 읽어오기
AppRunner
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ResourceLoader resourceLoader;
@Override
public void run(ApplicationArguments args) throws Exception {
Resource resource = resourceLoader.getResource("classpath:/text.txt");
System.out.println(resource.exists());
System.out.println(resource.getDescription());
System.out.println(Files.readString(Path.of(resource.getURI())));
}
}
text.txt
hello spring
결과
true
class path resource [text.txt]
hello spring
참고
스프링입문 강의 by 백기선
https://www.inflearn.com/course/lecture?courseSlug=spring&unitId=15538 https://www.inflearn.com/course/spring_revised_edition/dashboard
https://www.inflearn.com/course/spring_revised_edition/dashboard
'Spring > about spring' 카테고리의 다른 글
[Resource / Validation] Validation 추상화 (0) | 2023.07.26 |
---|---|
[Resource / Validation] Resource 추상화 (0) | 2023.07.25 |
[IoC 컨테이너와 빈] ApplicationEvent Publisher (0) | 2023.07.24 |
[IoC 컨테이너와 빈] MessageSource (0) | 2023.07.24 |
[IoC 컨테이너와 빈] Environment 프로파일, 프로퍼티 (0) | 2023.07.23 |