Spring Resource

java.net.URL์˜ ํ•œ๊ณ„๋ฅผ (classpath ๋‚ด๋ถ€ ์ ‘๊ทผ์ด๋‚˜ ์ƒ๋Œ€๊ฒฝ๋กœ ๋“ฑ) ๋„˜์–ด์„œ๊ธฐ ์œ„ํ•ด ์Šคํ”„๋ง์—์„œ ์ถ”๊ฐ€๋กœ ๊ตฌํ˜„

Resource Interface์™€ ๊ทธ ๊ตฌํ˜„์ฒด๋“ค

public interface Resource extends InputStreamSource {

    boolean exists();

    boolean isReadable();

    boolean isOpen();

    boolean isFile();

    URL getURL() throws IOException;

    URI getURI() throws IOException;

    File getFile() throws IOException;

    ReadableByteChannel readableChannel() throws IOException;

    long contentLength() throws IOException;

    long lastModified() throws IOException;

    Resource createRelative(String relativePath) throws IOException;

    String getFilename();

    String getDescription();
}

Resource ๊ตฌํ˜„์ฒด ๋ชฉ๋ก

Spring ๋‚ด๋ถ€ Resource ๊ตฌํ˜„์ฒด ์ค‘ ๋Œ€ํ‘œ์ ์ธ ๋ช‡๊ฐ€์ง€

UrlResource

java.net.URL ์„ ๋ž˜ํ•‘ํ•œ ๋ฒ„์ „, ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ Resource ์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” http(s) ๋กœ ์›๊ฒฉ ์ ‘๊ทผ (ftp:, file:, http:, ๋“ฑ์˜ prefix ๋กœ ์ ‘๊ทผ์œ ํ˜• ํŒ๋‹จ)

ClassPathResource

classpath ํ•˜์œ„์˜ ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ ์‹œ ์‚ฌ์šฉ (์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋นŒ๋“œํ•œ ๊ฒฐ๊ณผ(๊ธฐ๋ณธ์ ์œผ๋กœ target/classes ํด๋”))

FileSystemResource

File ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๋ฆฌ์†Œ์Šค ๊ตฌํ˜„์ฒด

SevletContextResource, InputStreamResource, ByteArrayResource

Servlet ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฃจํŠธ ํ•˜์œ„ ํŒŒ์ผ, InputStream, ByteArrayInput ์ŠคํŠธ๋ฆผ์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ๊ตฌํ˜„์ฒด

Spring ResourceLoader

์Šคํ”„๋ง ํ”„๋กœ์ ํŠธ ๋‚ด Resource ์— ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ

  • ๊ธฐ๋ณธ์ ์œผ๋กœ applicationContext ์—์„œ ๊ตฌํ˜„์ด ๋˜์–ด ์žˆ์Œ

  • ํ”„๋กœ์ ํŠธ ๋‚ด ํŒŒ์ผ์— ์ ‘๊ทผํ•  ์ผ์ด ์žˆ์„ ๊ฒฝ์šฐ ํ™œ์šฉ (์ฃผ๋กœ classpath ํ•˜์œ„ ํŒŒ์ผ)

  • ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ์ „์ •์˜๋œ ํŒŒ์ผ๋“ค์€ ์ž๋™์œผ๋กœ ๋กœ๋”ฉ๋˜๋„๋ก ๋˜์–ด ์žˆ์œผ๋‚˜, ์ถ”๊ฐ€๋กœ ํ•„์š”ํ•œ ํŒŒ์ผ์ด ์žˆ์„ ๋•Œ ํ™œ์šฉ ๊ฐ€๋Šฅ

@Service
public class ResourceService {
	@Autowired
	ApplicationContext ctx;

	public void setResource() {
		Resource myTemplate = 
			ctx.getResource("classpath:some/resource/path/myTemplate.txt");
			// ctx.getResource("file:/some/resource/path/myTemplate.txt");
			// ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");
		// use myTemplate...
	}
}

ResourcePatternResolver

์Šคํ”„๋ง ApplicationContext ์—์„œ ResourceLoader ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Interface ์œ„์น˜ ์ง€์ •์ž ํŒจํ„ด์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ Resouce ๋กœ๋” ๊ตฌํ˜„์ฒด๋ฅผ ์„ ํƒ ("classpath:", "file:", "http:")

public interface ApplicationContext extends EnvironmentCapable, 
		ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, **ResourcePatternResolver** {
		// Spring ApplicationContext interface
}

Application Contexts & Resource Paths

applicationContext ๋ฅผ ์ด๋ฃจ๋Š” ์„ค์ • ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•๋“ค

// let's create an applicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");

ApplicationContext ctx =
    new FileSystemXmlApplicationContext("conf/appContext.xml");

ApplicationContext ctx =
    new FileSystemXmlApplicationContext("classpath:conf/appContext.xml");

// then you can use ctx as a Spring
Bear bear = (Bear) ctx.getBean("bear");

Last updated