EEALL@ONCE

๐ŸŒฑ BeanFactory๔€งฌ& ApplicationContext ๋ณธ๋ฌธ

Spring๐ŸŒฑ

๐ŸŒฑ BeanFactory๔€งฌ& ApplicationContext

์˜ฌ์—ฃ์›์Šค 2023. 7. 29. 22:41
728x90

๋‘ ๊ฐœ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธด ์ข€ ์–ด๋ ต๋‹ค. ์™œ๋ƒ๋ฉด, ๋ณดํ†ต BeanFactory๋ฅผ ๋ฐ”๋กœ ์“ฐ๊ธฐ๋ณด๋‹จ, BeanFactory๋ฅผ ์ƒ์†ํ•œ ApplicationContext๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋‘˜ ๋‹ค ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ๋ถ€๋ฅด๊ธด ํ•œ๋‹ค. 

 

1. BeanFactory 

(1) ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ์ตœ์ƒ์œ„ ์ธํ„ฐํŽ˜์ด์Šค

(2) ์Šคํ”„๋ง ๋นˆ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์กฐํšŒํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹น.

(3) getBean()์„ ์ œ๊ณต

 

2. Application Context

(1) BeanFactory ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ƒ์†๋ฐ›์•„์„œ ์ œ๊ณต

(2) ๋นˆ ํŽ™ํ† ๋ฆฌ๊ฐ€ ์ œ๊ณต ํ•˜์ง€ ์•Š๋Š” ๋ถ€๊ฐ€๊ธฐ๋Šฅ์„ ์ œ๊ณต 

๊น€์˜ํ•œ ๊ฐ•์‚ฌ๋‹˜ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ž๋ฃŒ ์‚ฌ์ง„

   - MessageSource : ๊ฐ ์–ธ์–ด๋ฅผ ๋“ค์–ด์˜ค๋Š” ๊ฐ’๋Œ€๋กœ ์ถœ๋ ฅ

๊ตญ์ œํ™”(i18n)์™€ ์ง€์—ญํ™”(l10n)๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ, ์‚ฌ์šฉ์ž์˜ ๋กœ์ผ€์ผ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์คŒ 

   - EnviromentCapable: ํ™˜๊ฒฝ๋ณ€์ˆ˜ - ๋กœ์ปฌ/๊ฐœ๋ฐœ/์šด์˜ ๋“ฑ์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ฒ˜๋ฆฌํ•ด์คŒ

   - ApplicationEventPublisher: ์ด๋ฒคํŠธ๋ฅผ ๋ฐœํ–‰ํ•˜๊ณ  ๊ตฌ๋…ํ•˜๋Š” ๋ชจ๋ธ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ง€์›

์ด๋ฒคํŠธ(Events)๋ฅผ ๋ฐœํ–‰ํ•˜๊ณ  ์ด๋ฅผ ๊ตฌ๋…ํ•˜๋Š” ๋ฆฌ์Šค๋„ˆ(Listener)๋“ค์—๊ฒŒ ์ „๋‹ฌ 

์ด๋ฒคํŠธ (Events): ์ด๋ฒคํŠธ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ํŠน์ • ์‚ฌ๊ฑด์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด

๋ฆฌ์Šค๋„ˆ (Listeners): ๋ฆฌ์Šค๋„ˆ๋Š” ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ด๋ฅผ ์ˆ˜์‹ ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ์ฒด

   - ResourceLoader : ํŒŒ์ผ, ํด๋ž˜์ŠคํŒจ์Šค, ์™ธ๋ถ€ ๋“ฑ์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒ 

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

@Component
public class MyResourceLoader implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void loadResource(String location) {
        // ์ฃผ์–ด์ง„ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ๋ฆฌ์†Œ์Šค ๋กœ๋“œ
        Resource resource = resourceLoader.getResource(location);

        // ๋ฆฌ์†Œ์Šค๋ฅผ ํ†ตํ•ด ์ฝ๊ธฐ ์ž‘์—… ์ˆ˜ํ–‰
        try {
            // ์˜ˆ์‹œ: ํŒŒ์ผ ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฝ์–ด์˜ด
            File file = resource.getFile();
            // ํŒŒ์ผ์„ ์ฝ๊ฑฐ๋‚˜ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง ์ž‘์„ฑ ๊ฐ€๋Šฅ
        } catch (IOException e) {
            // ํŒŒ์ผ ์ฝ๊ธฐ ์‹คํŒจ ์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
        }
    }
}
728x90