๋ฐ์ํ
๋ฆฌ์์ค๋ฅผ ์ฝ์ด์ค๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์ธํฐํ์ด์ค
ApplicationContext extends ResourceLoarder
๋ฆฌ์์ค ์ฝ์ด์ค๊ธฐ
- ํ์ผ ์์คํ ์์ ์ฝ์ด์ค๊ธฐ
- ํด๋์คํจ์ค์์ ์ฝ์ด์ค๊ธฐ
- URL๋ก ์ฝ์ด์ค๊ธฐ
- ์๋/์ ๋ ๊ฒฝ๋ก๋ก ์ฝ์ด์ค๊ธฐ
resource.exists();
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ResourceLoader resourceLoader;
@Override
public void run(ApplicationArguments args) throws Exception {
Resource resource = resourceLoader.getResource("classpath:test.txt");//test.txt๋ผ๋ ํ์ผ์ ๋ฐ์์จ๋ค.
System.out.println(resource.exists());//resource์ test.txt ํ์ผ์ด ์กด์ฌํ๋๊ฐ?
}
}
์คํ ๊ฒฐ๊ณผ
false
resource file ๋ด์ฉ ์ฝ์ด๋ค์ด๊ธฐ
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ResourceLoader resourceLoader;
@Override
public void run(ApplicationArguments args) throws Exception {
Resource resource = resourceLoader.getResource("classpath:test.txt");
//test.txt ํ์ผ์ด ์กด์ฌ ํ๋์ง?
System.out.println(resource.exists());
//resource์ ํ ํจํค์ง ์ด๋ฆ ์ถ๋ ฅ
System.out.println(resource.getDescription());
//test.txtํ์ผ ๋ด์ฉ์ ์ฝ์ด๋ค์ด๊ธฐ
System.out.println(Files.readString(Path.of(resource.getURI())));
//Files ๋ฉ์๋๋ java11๋ถํฐ ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ๋ ๋ฉ์๋๋ค.
}
}
์คํ ๊ฒฐ๊ณผ
true
class path resource [test.txt]
Hello Spring
URI ๋?? https://iseunghan.tistory.com/86
Resource getResource(java.lang.String loation)
์์ธํ๊ฑด ๋ค์๊ธ Resource ์ถ์ํ๋ฅผ ์ฐธ์กฐํ๋ผ.
๋ฐ์ํ