๐ค Class.getResource()
์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ์ Class<T> ํด๋์ค์ getResource() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
์ฃผ์์ ๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Finds a resource with a given name.
If this class is in a named Module then this method will attempt to find the resource in the module.
This is done by delegating to the module's class loader findResource(String,String) method, invoking it with the module name and the absolute name of the resource.
Resources in named modules are subject to the rules for encapsulation specified in the Module getResourceAsStream method and so this method returns null when the resource is a non-".class" resource in a package that is not open to the caller's module.
Otherwise, if this class is not in a named module then the rules for searching resources associated with a given class are implemented by the defining class loader of the class.
This method delegates to this Class object's class loader.
If this Class object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResource.
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
- If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
- Otherwise, the absolute name is of the following form: modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
์ ์ค๋ช ์ ๋ฐ๋ฅด๋ฉด Class.getResource() ๋ฉ์๋๋ ClassLoader์ ์์ํ์ฌ ์ํ๋ resource๋ฅผ ์ฐพ์์จ๋ค๊ณ ํฉ๋๋ค.
๋ํ ์ํ๋ ๋ฆฌ์์ค๋ฅผ ์ฐพ๊ธฐ ์ํด์๋ ํด๋น ๋ฆฌ์์ค ์ด๋ฆ์ ๋งจ ์์ '/'๋ฅผ ๋ถ์ฌ์ฃผ์ด์ผ ํ๋ค๋ ๊ฒ๋ ์ ์ ์์์ต๋๋ค.
ํ๋ฒ ์ค์ ๋ก ํ ์คํธ๋ฅผ ์งํํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
resource ํด๋์๋ ๋ค์ ํ์ผ์ด ์กด์ฌํฉ๋๋ค.
@Test
void resource_๋๋ ํฐ๋ฆฌ์_์๋_ํ์ผ์_๊ฒฝ๋ก๋ฅผ_์ฐพ๋๋ค() {
String fileName = "nextstep.txt";
URL resource = getClass().getResource("/" + fileName);
String actual = resource.getFile();
assertThat(actual).endsWith(fileName);
}
@DisplayName("์์ '/' ๊ฐ ์์ผ๋ฉด ๋ชป์ฐพ๋๋ค")
@Test
void fail() {
String fileName = "nextstep.txt";
URL resource = getClass().getResource(fileName);
String actual = resource.getFile();
assertThat(actual).isNull();
}
์ด์ ํ์ผ์ ์ฐพ์์ผ๋ ๊ทธ ๋ด์ฉ์ ์ฝ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
๊ทธ ์ ์ Class์์ ClassLoader์ ์์ํ์ฌ ํ์ผ์ ์ฐพ์์จ๋ค๊ณ ํ๋๋ฐ, ์ฒ์๋ถํฐ ClassLoader๋ฅผ ํตํด ํธ์ถํ๋ฉด ํ์ผ์ ์ด๋ฆ ์์ '/'๋ฅผ ๋ถ์ฌ์ผ ํ๋ ๊ท์ฐฎ์๋ ํด๊ฒฐํ ์ ์์ ๊ฒ์ ๋๋ค.
๐ค ClassLoader.getResource()
์๋์ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
@Test
void resource_๋๋ ํฐ๋ฆฌ์_์๋_ํ์ผ์_๊ฒฝ๋ก๋ฅผ_์ฐพ๋๋ค() {
String fileName = "nextstep.txt";
URL resource = getClass().getClassLoader().getResource(fileName);
String actual = resource.getFile();
assertThat(actual).endsWith(fileName);
}
getClass().getClasssLoader() ๋ง๊ณ ๋ค์๊ณผ ๊ฐ์ด๋ ๊ฐ๋ฅํฉ๋๋ค.
@Test
void resource_๋๋ ํฐ๋ฆฌ์_์๋_ํ์ผ์_๊ฒฝ๋ก๋ฅผ_์ฐพ๋๋ค() {
String fileName = "nextstep.txt";
URL resource = ClassLoader.getSystemClassLoader().getResource(fileName);
String actual = resource.getFile();
assertThat(actual).endsWith(fileName);
}
๐ค ํ์ผ ๋ด์ฉ์ ์ฝ๋ ๋ฐฉ๋ฒ
ํ์ผ์ ๋ด์ฉ์ ํ๋ก๊ทธ๋จ์์ ์ฝ์ ๋์๋ ์ด์ ๊ธ์์ ๋ฐฐ์ด InputStream์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ
ํ์ผ์ ๋ด์ฉ์ ํ๋ก๊ทธ๋จ์์ ์ฝ๊ฒ ํ๊ธฐ ์ํด์๋ InputStream์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
์ฌ๋ฌ ๋ฐฉ๋ฒ์ด ์๊ฒ ์ง๋ง, ๋ค ์ดํด๋ณด๋ ๊ฒ์ ๋ฌด๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ ๋ช๊ฐ์ง ๋ฐฉ๋ฒ๋ง ์ดํด๋ณด๊ฒ ์ต๋๋ค.
@Test
void ํ์ผ์_๋ด์ฉ์_์ฝ๋๋ค() throws URISyntaxException {
String fileName = "nextstep.txt";
URL resource = getClass().getClassLoader().getResource(fileName);
Path path = Path.of(resource.toURI());
try (BufferedReader bufferedReader = Files.newBufferedReader(path)) {
List<String> actual = bufferedReader.lines().toList();
assertThat(actual).containsOnly("nextstep");
} catch (Exception e) {
}
}
@Test
void ํ์ผ์_๋ด์ฉ์_์ฝ๋๋ค2() throws FileNotFoundException {
String fileName = "nextstep.txt";
URL resource = getClass().getClassLoader().getResource(fileName);
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(resource.getFile()))) {
List<String> actual = bufferedReader.lines().toList();
assertThat(actual).containsOnly("nextstep");
} catch (Exception e) {
}
}
@Test
void ํ์ผ์_๋ด์ฉ์_์ฝ๋๋ค3() throws IOException {
String fileName = "nextstep.txt";
URL resource = getClass().getClassLoader().getResource(fileName);
InputStream inputStream = Files.newInputStream(Paths.get(resource.getFile()));
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
List<String> actual = bufferedReader.lines().toList();
assertThat(actual).containsOnly("nextstep");
} catch (Exception e) {
}
}
์์ ์ฝ๋ ์ค 3๋ฒ์งธ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์ฝ์ด์จ ํ์ผ์ InputStream์ผ๋ก ๋ณํํด์ฃผ๋ ๊ณผ์ ์ด ํ์ํ๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
์ฒ์๋ถํฐ ํ์ผ์ InputStream์ผ๋ก ๋ฐ์์ค๋ ๋ฐฉ๋ฒ์ ์์๊น์?
๐ค getResourceAsStream()
๊ธฐ์กด์ ์ฌ์ฉํ๋ getResource() ๋ฉ์๋ ๋์ getResourceAsStream() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ผ์ ๋ด์ฉ์ ๊ณง๋ฐ๋ก InputStream์ผ๋ก ๋ฐ์์ฌ ์ ์์ต๋๋ค.
@Test
void ํ์ผ์_๋ด์ฉ์_์ฝ๋๋ค() {
String fileName = "nextstep.txt";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
List<String> actual = bufferedReader.lines().toList();
assertThat(actual).containsOnly("nextstep");
} catch (Exception e) {
}
}
์ด๋ ๊ฒ ํด์ ํฐ์บฃ์ ํตํ ์น ์๋ฒ ๊ตฌํ์ ์ํ ๊ธฐ๋ณธ์ ์ธ ์ง์๋ค์ ์์๋ณด์์ต๋๋ค.
'๐ ์ฐ์ํํ ํฌ์ฝ์ค 5๊ธฐ > Tomcat ๊ตฌํํ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Tomcat ๊ตฌํํ๊ธฐ] (5) - HTTP Cache (0) | 2023.09.10 |
---|---|
[Tomcat ๊ตฌํํ๊ธฐ] (4) - HTTP Compression (ํฐ์บฃ์์์ ๊ตฌํ) (0) | 2023.09.09 |
[Tomcat ๊ตฌํํ๊ธฐ] (2) - InputStream, OutputStream์ด ๋ญ์ง? (2) | 2023.09.02 |
[Tomcat ๊ตฌํํ๊ธฐ] (1) - ์น ์๋ฒ๊ฐ ํ๋ ์ผ ์์๋ณด๊ธฐ (0) | 2023.09.02 |