org.springframework.core.io.Resource
Resource ์ถ์ํ๋? springframework.core.io.Resource๋ก java.net.URL๋ผ๋ ํด๋์ค๋ฅผ ๊ฐ์ธ๊ณ ,
์ค์ low level์ ์๋ Resource์ ์ ๊ทผํ๋ ๊ธฐ๋ฅ ์ ์ถ์ํ๋ผ๊ณ ํ๋ค.
ํน์ง
- java.net.URL ์ ์ถ์ํ ํ ๊ฒ
- ์คํ๋ง ๋ด๋ถ์์ ๋ง์ด ์ฌ์ฉํ๋ ์ธํฐํ์ด์ค!
์ถ์ํ ํ ์ด์
- ํด๋์คํจ์ค ๊ธฐ์ค์ผ๋ก ๋ฆฌ์์ค ์ฝ์ด์ค๋ ๊ธฐ๋ฅ ๋ถ์ฌ
- ServletContext๋ฅผ ๊ธฐ์ค์ผ๋ก ์๋ ๊ฒฝ๋ก๋ก ์ฝ์ด์ค๋ ๊ธฐ๋ฅ ๋ถ์ฌ
- ์๋ก์ด ํธ๋ค๋ฌ๋ฅผ ๋ฑ๋กํ์ฌ ํน๋ณํ URL ์ ๋ฏธ์ฌ๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ ์๋ ์์ง๋ง ๊ตฌํ์ด ๋ณต์กํ๊ณ ํธ์์ฑ ๋ฉ์๋๊ฐ ๋ถ์กฑํ๋ค.
์ฐ๋ฆฌ๊ฐ ApplicationContext๋ฅผ ๋ง๋ค๋,
var ctx = new ClassPathXmlApplicationContext("xxx.xml");
//xxx.xml ๋ฌธ์์ด์ด resource๋ก ๋ณํ์ด ๋๋ค.
์ธํฐํ์ด์ค ๋๋ฌ๋ณด๊ธฐ
- ์์ ๋ฐ์ ์ธํฐํ์ด์ค
- ์ฃผ์ ๋ฉ์๋
- getInputStream()
- exists() : ๋ฆฌ์์ค๊ฐ ์กด์ฌํ๋์ง true / false
- isOpen() : ๋ฆฌ์์ค๊ฐ ์ด๋ ค์๋์ง
- getDescription(): ์ ์ฒด ๊ฒฝ๋ก ํฌํจํ ํ์ผ ์ด๋ฆ ๋๋ ์ค์ URL
๊ตฌํ์ฒด
- UrlResource : java.net.URL ์ฐธ๊ณ , ๊ธฐ๋ณธ์ผ๋ก ์ง์ํ๋ ํ๋กํ ์ฝ http, https, ftp, file, jar.
- ClassPathResource : ์ง์ํ๋ ์ ๋์ด "classpath:~~"
- FileSystemResource
- ServletContextResource : ์น ์ ํ๋ฆฌ์ผ์ด์ ๋ฃจํธ์์ ์๋ ๊ฒฝ๋ก๋ก ๋ฆฌ์์ค ์ฐพ๋๋ค.
- ....
๋ฆฌ์์ค ์ฝ์ด์ค๊ธฐ
- Resource์ ํ์
์ location ๋ฌธ์์ด๊ณผ ApplicationContext์ ํ์
์ ๋ฐ๋ผ ๊ฒฐ์ ๋๋ค.
- ClassPathXmlApplicationContext -> ClassPathResource
- FileSystemXmlApplicationContext -> FileSystemResource
- WebApplicationContext -> ServletContextResource
- ApplicationContext์ ํ์
์ ์๊ด์์ด ๋ฆฌ์์ค ํ์
์ ๊ฐ์ ํ๋ ค๋ฉด java.net.URL ์ ๋์ด(+ classpath:)์ค ํ๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- classpath: me/whiteship/config.xml -> ClassPathResource : classpath๊ฒฝ๋ก ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ํด๋นํ๋ ๋ฆฌ์์ค๋ฅผ ์ฐพ๋๋ค.
- file:// some/resource/path/config.xml -> FileSystemResource : filesystem ๊ธฐ์ค์ผ๋ก ๋ฆฌ์์ค๋ฅผ ์ฐพ๋๋ค.
์ ๋์ด๋ฅผ ์ฌ์ฉํด์ ๋ช ์๋ฅผ ํด์ฃผ๋ฉด ์์ฃผ ์ข๋ค! ์ข์ ์ฝ๋๊ฐ ๋๋ค.
ex) classpath: ~~ -> ๋ฅผ ๋ณด๊ณ "์~์๋ ClassPathResource๋ก ์ค๋๊ตฌ๋~"
Resource ํ์ ์ฐ์ด๋ณด๊ธฐ
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ApplicationContext resourceLoader;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(resourceLoader.getClass()); //ApplicationContext์ ๋ฆฌ์์ค ํ์
์ ์ฐ์ด๋ณธ๋ค.
Resource resource = resourceLoader.getResource("classpath:test.txt");
System.out.println(resource.getClass());//classpath๋ผ๋ prefix๋ฅผ ์ผ๊ธฐ๋๋ฌธ์, classpathResource๊ฐ ๋์จ๋ค.
System.out.println(resource.exists());
System.out.println(resource.getDescription());
System.out.println(Files.readString(Path.of(resource.getURI())));
}
}
class org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
class org.springframework.core.io.ClassPathResource
true
class path resource [test.txt]
Hello Spring
AnnotationConfigServletWebServerApplicationContext ๋ฅผ ๋ฐ๋ผ๊ฐ๋ค ๋ณด๋ฉด,..
public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext implements AnnotationConfigRegistry
public class ServletWebServerApplicationContext extends GenericWebApplicationContext implements ConfigurableWebServerApplicationContext
public class GenericWebApplicationContext extends GenericApplicationContext implements ConfigurableWebApplicationContext, ThemeSource
public interface ConfigurableWebApplicationContext extends WebApplicationContext, ConfigurableApplicationContext
๊ฒฐ๊ตญ ๋์๋ WebApplicationContext๊ฐ ์๋ค.
public interface WebApplicationContext extends ApplicationContext
์๋๋ ApplicationContext resourceLoader ์ด๋ฉด WebApplicationContext๊ฐ ์ฐํ์ผํ๋๋ฐ,
classpath: ๋ผ๋ prefix ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์! ClassPathResource ๊ฐ ์ฐํ๊ฒ์ด๋ค.
๋ง์ฝ classpath: ๋ฅผ ์ง์ด๋ค๋ฉด?
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ApplicationContext resourceLoader;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(resourceLoader.getClass()); //ApplicationContext์ ๋ฆฌ์์ค ํ์
์ ์ฐ์ด๋ณธ๋ค.
Resource resource = resourceLoader.getResource("test.txt");//classpath: ์๋ต
System.out.println(resource.getClass()); //๊ธฐ๋ณธ์ ์ผ๋ก WebApplicationContext๊ฐ ์ฐํ๊ฒ ๋๋ค.
System.out.println(resource.exists()); // ์ฐพ์์ ์์ผ๋ flase
System.out.println(resource.getDescription()); //Description์ ๋์ธ์ ์์ด๋..
System.out.println(Files.readString(Path.of(resource.getURI()))); //ใ
}
}
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
ServletContextResource๋ context path๋ถํฐ ์ฐพ๊ฒ ๋๋๋ฐ.. spring์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ๋ด์ฅ๋์ด์๋ tomcat์๋ context path๊ฐ ์ง์ ๋์ด ์์ง ์๋ค. ๊ทธ๋์ Files.readString์์ ์ค๋ฅ๊ฐ ๋๊ฒ ๋๋ค. (ํ์ผ์ ์ฝ์์๊ฐ ์๊ธฐ ๋๋ฌธ)
์ ๋์ด๋ฅผ ์ ์จ์ฃผ์
org.springframework.core.io.Resource
Resource ์ถ์ํ๋? springframework.core.io.Resource๋ก java.net.URL๋ผ๋ ํด๋์ค๋ฅผ ๊ฐ์ธ๊ณ ,
์ค์ low level์ ์๋ Resource์ ์ ๊ทผํ๋ ๊ธฐ๋ฅ ์ ์ถ์ํ๋ผ๊ณ ํ๋ค.
ํน์ง
- java.net.URL ์ ์ถ์ํ ํ ๊ฒ
- ์คํ๋ง ๋ด๋ถ์์ ๋ง์ด ์ฌ์ฉํ๋ ์ธํฐํ์ด์ค!
์ถ์ํ ํ ์ด์
- ํด๋์คํจ์ค ๊ธฐ์ค์ผ๋ก ๋ฆฌ์์ค ์ฝ์ด์ค๋ ๊ธฐ๋ฅ ๋ถ์ฌ
- ServletContext๋ฅผ ๊ธฐ์ค์ผ๋ก ์๋ ๊ฒฝ๋ก๋ก ์ฝ์ด์ค๋ ๊ธฐ๋ฅ ๋ถ์ฌ
- ์๋ก์ด ํธ๋ค๋ฌ๋ฅผ ๋ฑ๋กํ์ฌ ํน๋ณํ URL ์ ๋ฏธ์ฌ๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ ์๋ ์์ง๋ง ๊ตฌํ์ด ๋ณต์กํ๊ณ ํธ์์ฑ ๋ฉ์๋๊ฐ ๋ถ์กฑํ๋ค.
์ฐ๋ฆฌ๊ฐ ApplicationContext๋ฅผ ๋ง๋ค๋,
var ctx = new ClassPathXmlApplicationContext("xxx.xml");
//xxx.xml ๋ฌธ์์ด์ด resource๋ก ๋ณํ์ด ๋๋ค.
์ธํฐํ์ด์ค ๋๋ฌ๋ณด๊ธฐ
- ์์ ๋ฐ์ ์ธํฐํ์ด์ค
- ์ฃผ์ ๋ฉ์๋
- getInputStream()
- exists() : ๋ฆฌ์์ค๊ฐ ์กด์ฌํ๋์ง true / false
- isOpen() : ๋ฆฌ์์ค๊ฐ ์ด๋ ค์๋์ง
- getDescription(): ์ ์ฒด ๊ฒฝ๋ก ํฌํจํ ํ์ผ ์ด๋ฆ ๋๋ ์ค์ URL
๊ตฌํ์ฒด
- UrlResource : java.net.URL ์ฐธ๊ณ , ๊ธฐ๋ณธ์ผ๋ก ์ง์ํ๋ ํ๋กํ ์ฝ http, https, ftp, file, jar.
- ClassPathResource : ์ง์ํ๋ ์ ๋์ด "classpath:~~"
- FileSystemResource
- ServletContextResource : ์น ์ ํ๋ฆฌ์ผ์ด์ ๋ฃจํธ์์ ์๋ ๊ฒฝ๋ก๋ก ๋ฆฌ์์ค ์ฐพ๋๋ค.
- ....
๋ฆฌ์์ค ์ฝ์ด์ค๊ธฐ
- Resource์ ํ์
์ location ๋ฌธ์์ด๊ณผ ApplicationContext์ ํ์
์ ๋ฐ๋ผ ๊ฒฐ์ ๋๋ค.
- ClassPathXmlApplicationContext -> ClassPathResource
- FileSystemXmlApplicationContext -> FileSystemResource
- WebApplicationContext -> ServletContextResource
- ApplicationContext์ ํ์
์ ์๊ด์์ด ๋ฆฌ์์ค ํ์
์ ๊ฐ์ ํ๋ ค๋ฉด java.net.URL ์ ๋์ด(+ classpath:)์ค ํ๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- classpath: me/whiteship/config.xml -> ClassPathResource : classpath๊ฒฝ๋ก ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ํด๋นํ๋ ๋ฆฌ์์ค๋ฅผ ์ฐพ๋๋ค.
- file:// some/resource/path/config.xml -> FileSystemResource : filesystem ๊ธฐ์ค์ผ๋ก ๋ฆฌ์์ค๋ฅผ ์ฐพ๋๋ค.
์ ๋์ด๋ฅผ ์ฌ์ฉํด์ ๋ช ์๋ฅผ ํด์ฃผ๋ฉด ์์ฃผ ์ข๋ค! ์ข์ ์ฝ๋๊ฐ ๋๋ค.
ex) classpath: ~~ -> ๋ฅผ ๋ณด๊ณ "์~์๋ ClassPathResource๋ก ์ค๋๊ตฌ๋~"
Resource ํ์ ์ฐ์ด๋ณด๊ธฐ
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ApplicationContext resourceLoader;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(resourceLoader.getClass()); //ApplicationContext์ ๋ฆฌ์์ค ํ์
์ ์ฐ์ด๋ณธ๋ค.
Resource resource = resourceLoader.getResource("classpath:test.txt");
System.out.println(resource.getClass());//classpath๋ผ๋ prefix๋ฅผ ์ผ๊ธฐ๋๋ฌธ์, classpathResource๊ฐ ๋์จ๋ค.
System.out.println(resource.exists());
System.out.println(resource.getDescription());
System.out.println(Files.readString(Path.of(resource.getURI())));
}
}
class org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
class org.springframework.core.io.ClassPathResource
true
class path resource [test.txt]
Hello Spring
AnnotationConfigServletWebServerApplicationContext ๋ฅผ ๋ฐ๋ผ๊ฐ๋ค ๋ณด๋ฉด,..
public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext implements AnnotationConfigRegistry
public class ServletWebServerApplicationContext extends GenericWebApplicationContext implements ConfigurableWebServerApplicationContext
public class GenericWebApplicationContext extends GenericApplicationContext implements ConfigurableWebApplicationContext, ThemeSource
public interface ConfigurableWebApplicationContext extends WebApplicationContext, ConfigurableApplicationContext
๊ฒฐ๊ตญ ๋์๋ WebApplicationContext๊ฐ ์๋ค.
public interface WebApplicationContext extends ApplicationContext
์๋๋ ApplicationContext resourceLoader ์ด๋ฉด WebApplicationContext๊ฐ ์ฐํ์ผํ๋๋ฐ,
classpath: ๋ผ๋ prefix ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์! ClassPathResource ๊ฐ ์ฐํ๊ฒ์ด๋ค.
๋ง์ฝ classpath: ๋ฅผ ์ง์ด๋ค๋ฉด?
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ApplicationContext resourceLoader;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(resourceLoader.getClass()); //ApplicationContext์ ๋ฆฌ์์ค ํ์
์ ์ฐ์ด๋ณธ๋ค.
Resource resource = resourceLoader.getResource("test.txt");//classpath: ์๋ต
System.out.println(resource.getClass()); //๊ธฐ๋ณธ์ ์ผ๋ก WebApplicationContext๊ฐ ์ฐํ๊ฒ ๋๋ค.
System.out.println(resource.exists()); // ์ฐพ์์ ์์ผ๋ flase
System.out.println(resource.getDescription()); //Description์ ๋์ธ์ ์์ด๋..
System.out.println(Files.readString(Path.of(resource.getURI()))); //ใ
}
}
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
ServletContextResource๋ context path๋ถํฐ ์ฐพ๊ฒ ๋๋๋ฐ.. spring์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ๋ด์ฅ๋์ด์๋ tomcat์๋ context path๊ฐ ์ง์ ๋์ด ์์ง ์๋ค. ๊ทธ๋์ Files.readString์์ ์ค๋ฅ๊ฐ ๋๊ฒ ๋๋ค. (ํ์ผ์ ์ฝ์์๊ฐ ์๊ธฐ ๋๋ฌธ)
์ ๋์ด๋ฅผ ์ ์จ์ฃผ์